1 | <?php |
||
23 | class ApiController extends Controller |
||
24 | { |
||
25 | private $em; |
||
26 | |||
27 | 18 | public function setContainer(ContainerInterface $container = null) |
|
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() |
|
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() |
|
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) |
|
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) |
|
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) |
|
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) |
|
243 | } |
||
244 |