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