1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\UserBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
6
|
|
|
use Overwatch\UserBundle\Entity\User; |
7
|
|
|
use Overwatch\UserBundle\Enum\AlertSetting; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ApiController |
20
|
|
|
* Handles API requests made for Users |
21
|
|
|
* @Route("/api") |
22
|
|
|
*/ |
23
|
|
|
class ApiController extends Controller |
24
|
|
|
{ |
25
|
|
|
private $em; |
26
|
|
|
|
27
|
18 |
|
public function setContainer(ContainerInterface $container = null) |
28
|
|
|
{ |
29
|
18 |
|
parent::setContainer($container); |
30
|
18 |
|
$this->em = $this->getDoctrine()->getManager(); |
31
|
18 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the list of possible alert settings |
35
|
|
|
* |
36
|
|
|
* @Route("/alertSettings") |
37
|
|
|
* @Method({"GET"}) |
38
|
|
|
* @ApiDoc( |
39
|
|
|
* resource=true, |
40
|
|
|
* tags={ |
41
|
|
|
* "Super Admin" = "#ff1919", |
42
|
|
|
* "Admin" = "#ffff33", |
43
|
|
|
* "User" = "#75ff47" |
44
|
|
|
* } |
45
|
|
|
* ) |
46
|
|
|
*/ |
47
|
1 |
|
public function getAlertSettingsAction() |
48
|
|
|
{ |
49
|
1 |
|
return new JsonResponse(AlertSetting::getAll()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns a list of all users |
54
|
|
|
* |
55
|
|
|
* @Route("/users") |
56
|
|
|
* @Method({"GET"}) |
57
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
58
|
|
|
* @ApiDoc( |
59
|
|
|
* resource=true, |
60
|
|
|
* tags={ |
61
|
|
|
* "Super Admin" = "#ff1919" |
62
|
|
|
* } |
63
|
|
|
* ) |
64
|
|
|
*/ |
65
|
1 |
|
public function getAllUsersAction() |
66
|
|
|
{ |
67
|
1 |
|
$users = $this->em->getRepository('OverwatchUserBundle:User')->findAll(); |
68
|
1 |
|
return new JsonResponse($users); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Creates a new user with the given e-mail address |
73
|
|
|
* |
74
|
|
|
* @Route("/users/{email}") |
75
|
|
|
* @Method({"POST"}) |
76
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
77
|
|
|
* @ApiDoc( |
78
|
|
|
* requirements={ |
79
|
|
|
* {"name"="email", "description"="The e-mail address of the user to create", "dataType"="email", "requirement"="Valid e-mail address"} |
80
|
|
|
* }, |
81
|
|
|
* tags={ |
82
|
|
|
* "Super Admin" = "#ff1919" |
83
|
|
|
* } |
84
|
|
|
* ) |
85
|
|
|
*/ |
86
|
1 |
|
public function createUserAction($email) |
87
|
|
|
{ |
88
|
1 |
|
$password = substr(preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(openssl_random_pseudo_bytes(9))), 0, 8); |
89
|
1 |
|
$user = $this->get('fos_user.util.user_manipulator')->create($email, $password, $email, true, false); |
90
|
|
|
|
91
|
|
|
//send user e-mail with their pass |
92
|
1 |
|
$message = \Swift_Message::newInstance() |
93
|
1 |
|
->setSubject('You have been invited to Overwatch') |
94
|
1 |
|
->setFrom($this->getUser()->getEmail()) |
95
|
1 |
|
->setTo($email) |
96
|
1 |
|
->setBody( |
97
|
1 |
|
$this->renderView( |
98
|
1 |
|
'OverwatchUserBundle:Email:invited.txt.twig', |
99
|
|
|
[ |
100
|
1 |
|
'inviter' => $this->getUser()->getEmail(), |
101
|
1 |
|
'email' => $email, |
102
|
|
|
'password' => $password |
103
|
1 |
|
] |
104
|
1 |
|
) |
105
|
1 |
|
); |
106
|
1 |
|
$this->get('mailer')->send($message); |
107
|
|
|
|
108
|
1 |
|
return new JsonResponse($user, JsonResponse::HTTP_CREATED); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the user associated with the given e-mail address |
113
|
|
|
* |
114
|
|
|
* @Route("/users/{email}") |
115
|
|
|
* @Method({"GET"}) |
116
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
117
|
|
|
* @ParamConverter("user", class="OverwatchUserBundle:User") |
118
|
|
|
* @ApiDoc( |
119
|
|
|
* requirements={ |
120
|
|
|
* {"name"="email", "description"="The e-mail address to search by", "dataType"="email", "requirement"="Valid e-mail address"} |
121
|
|
|
* }, |
122
|
|
|
* tags={ |
123
|
|
|
* "Super Admin" = "#ff1919" |
124
|
|
|
* } |
125
|
|
|
* ) |
126
|
|
|
*/ |
127
|
1 |
|
public function findUserAction(User $user) |
128
|
|
|
{ |
129
|
1 |
|
return new JsonResponse($user); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* |
134
|
|
|
* @Route("/users") |
135
|
|
|
* @Method({"PUT"}) |
136
|
|
|
* @ApiDoc( |
137
|
|
|
* parameters={ |
138
|
|
|
* {"name"="alertSetting", "description"="The new alert setting for the user", "required"=true, "dataType"="integer", "requirement"="[0-4]"}, |
139
|
|
|
* {"name"="telephoneNumber", "description"="The new telephone number for the user", "required"=true, "dataType"="string"}, |
140
|
|
|
* }, |
141
|
|
|
* tags={ |
142
|
|
|
* "Super Admin" = "#ff1919", |
143
|
|
|
* "Admin" = "#ffff33", |
144
|
|
|
* "User" = "#75ff47" |
145
|
|
|
* } |
146
|
|
|
* ) |
147
|
|
|
*/ |
148
|
1 |
|
public function updateUserAction(Request $request) |
149
|
|
|
{ |
150
|
1 |
|
$user = $this->getUser(); |
151
|
|
|
$user |
152
|
1 |
|
->setAlertSetting($request->request->get('alertSetting', $user->getAlertSetting())) |
153
|
1 |
|
->setTelephoneNumber($request->request->get('telephoneNumber', $user->getTelephoneNumber())); |
154
|
|
|
|
155
|
1 |
|
$this->em->flush(); |
156
|
1 |
|
return new JsonResponse($this->getUser()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Locks or unlocks the given user |
161
|
|
|
* |
162
|
|
|
* @Route("/users/{id}/lock") |
163
|
|
|
* @Method({"PUT","POST"}) |
164
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
165
|
|
|
* @ApiDoc( |
166
|
|
|
* requirements={ |
167
|
|
|
* {"name"="id", "description"="The ID of the user to lock", "dataType"="integer", "requirement"="\d+"} |
168
|
|
|
* }, |
169
|
|
|
* tags={ |
170
|
|
|
* "Super Admin" = "#ff1919" |
171
|
|
|
* } |
172
|
|
|
* ) |
173
|
|
|
*/ |
174
|
2 |
|
public function toggleLockUserAction(User $user) |
175
|
|
|
{ |
176
|
2 |
|
if ($user->getId() === $this->getUser()->getId()) { |
177
|
1 |
|
throw new AccessDeniedHttpException('You may not toggle locks on yourself.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
$user->setLocked(!$user->isLocked()); |
181
|
1 |
|
$this->em->flush(); |
182
|
|
|
|
183
|
1 |
|
return new JsonResponse($user); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Updates the given user to the given role |
188
|
|
|
* |
189
|
|
|
* @Route("/users/{id}/role/{role}") |
190
|
|
|
* @Method({"PUT","POST"}) |
191
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
192
|
|
|
* @ApiDoc( |
193
|
|
|
* requirements={ |
194
|
|
|
* {"name"="id", "description"="The ID of the user to update", "dataType"="integer", "requirement"="\d+"}, |
195
|
|
|
* {"name"="role", "description"="The new role for the user", "dataType"="role", "requirement"="ROLE_USER|ROLE_ADMIN|ROLE_SUPER_ADMIN"} |
196
|
|
|
* }, |
197
|
|
|
* tags={ |
198
|
|
|
* "Super Admin" = "#ff1919" |
199
|
|
|
* } |
200
|
|
|
* ) |
201
|
|
|
*/ |
202
|
2 |
|
public function setUserRoleAction(User $user, $role) |
203
|
|
|
{ |
204
|
2 |
|
if ($user->getId() === $this->getUser()->getId()) { |
205
|
1 |
|
throw new AccessDeniedHttpException('You may not set roles on yourself.'); |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
if (in_array($role, ['ROLE_USER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN'])) { |
209
|
1 |
|
$user->setRoles([$role]); |
210
|
1 |
|
} |
211
|
|
|
|
212
|
1 |
|
$this->em->flush(); |
213
|
|
|
|
214
|
1 |
|
return new JsonResponse($user); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Deletes the given user |
219
|
|
|
* |
220
|
|
|
* @Route("/users/{id}") |
221
|
|
|
* @Method({"DELETE"}) |
222
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
223
|
|
|
* @ApiDoc( |
224
|
|
|
* requirements={ |
225
|
|
|
* {"name"="id", "description"="The ID of the user to delete", "dataType"="integer", "requirement"="\d+"} |
226
|
|
|
* }, |
227
|
|
|
* tags={ |
228
|
|
|
* "Super Admin" = "#ff1919" |
229
|
|
|
* } |
230
|
|
|
* ) |
231
|
|
|
*/ |
232
|
2 |
|
public function deleteUserAction(User $user) |
233
|
|
|
{ |
234
|
2 |
|
if ($user->getId() === $this->getUser()->getId()) { |
235
|
1 |
|
throw new AccessDeniedHttpException('You may not delete yourself.'); |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
$this->em->remove($user); |
239
|
1 |
|
$this->em->flush(); |
240
|
|
|
|
241
|
1 |
|
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|