1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
4
|
|
|
|
5
|
|
|
use App\Document\User; |
6
|
|
|
use App\Mailer; |
7
|
|
|
use App\Security\ResetPasswordTokenManager; |
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
10
|
|
|
use Nelmio\ApiDocBundle\Annotation\Security; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
12
|
|
|
use Swagger\Annotations as SWG; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
18
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Provide routes for User API. |
22
|
|
|
* |
23
|
|
|
* @Route("/api/users", name="api_user_", defaults={"_format": "json"}) |
24
|
|
|
*/ |
25
|
|
|
class UserController extends AbstractController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* List users. |
29
|
|
|
* |
30
|
|
|
* @Route("", name="index", methods={"GET"}) |
31
|
|
|
* @SWG\Response( |
32
|
|
|
* response=200, |
33
|
|
|
* description="Returns users", |
34
|
|
|
* @SWG\Schema( |
35
|
|
|
* type="array", |
36
|
|
|
* @SWG\Items(ref=@Model(type=User::class, groups={"user_default"})) |
37
|
|
|
* ) |
38
|
|
|
* ) |
39
|
|
|
* @SWG\Parameter( |
40
|
|
|
* name="page", |
41
|
|
|
* in="query", |
42
|
|
|
* type="integer", |
43
|
|
|
* description="Page number", |
44
|
|
|
* default="1" |
45
|
|
|
* ) |
46
|
|
|
* @SWG\Parameter( |
47
|
|
|
* name="query", |
48
|
|
|
* in="query", |
49
|
|
|
* type="string", |
50
|
|
|
* description="Search query", |
51
|
|
|
* ) |
52
|
|
|
* @SWG\Tag(name="users") |
53
|
|
|
* @Security(name="Bearer") |
54
|
|
|
* |
55
|
|
|
* @IsGranted("ROLE_ADMIN") |
56
|
|
|
*/ |
57
|
|
|
public function list(DocumentManager $dm, Request $request): Response |
58
|
|
|
{ |
59
|
|
|
$page = $request->query->getInt('page', 1); |
60
|
|
|
$query = $request->query->get('query'); |
61
|
|
|
|
62
|
|
|
$rs = [ |
63
|
|
|
'total' => $dm->getRepository('App:User')->count($query), |
64
|
|
|
'items' => $dm->getRepository('App:User')->search($page, $query, 20), |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
return $this->json($rs, Response::HTTP_OK, [], ['groups' => ['user_default']]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* autocomplete users. |
72
|
|
|
* |
73
|
|
|
* @Route("/autocomplete", name="autocomplete", methods={"GET"}) |
74
|
|
|
* @SWG\Response( |
75
|
|
|
* response=200, |
76
|
|
|
* description="Returns users", |
77
|
|
|
* @SWG\Schema( |
78
|
|
|
* type="array", |
79
|
|
|
* @SWG\Items(ref=@Model(type=User::class, groups={"user_autocomplete"})) |
80
|
|
|
* ) |
81
|
|
|
* ) |
82
|
|
|
* @SWG\Parameter( |
83
|
|
|
* name="query", |
84
|
|
|
* in="query", |
85
|
|
|
* type="string", |
86
|
|
|
* description="Search query", |
87
|
|
|
* ) |
88
|
|
|
* @SWG\Tag(name="users") |
89
|
|
|
* @Security(name="Bearer") |
90
|
|
|
* |
91
|
|
|
* @IsGranted("ROLE_USER") |
92
|
|
|
*/ |
93
|
|
|
public function autocomplete(DocumentManager $dm, Request $request): Response |
94
|
|
|
{ |
95
|
|
|
$query = $request->query->get('query'); |
96
|
|
|
|
97
|
|
|
$rs = [ |
98
|
|
|
'items' => $dm->getRepository('App:User')->search(1, $query, 20), |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
return $this->json($rs, Response::HTTP_OK, [], ['groups' => ['user_autocomplete']]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get user. |
106
|
|
|
* |
107
|
|
|
* @Route("/{id}", name="get", methods={"GET"}) |
108
|
|
|
* @SWG\Response( |
109
|
|
|
* response=200, |
110
|
|
|
* description="Return user", |
111
|
|
|
* @SWG\Schema(ref=@Model(type=User::class, groups={"user_full"}) |
112
|
|
|
* ) |
113
|
|
|
* ) |
114
|
|
|
* @SWG\Tag(name="users") |
115
|
|
|
* @Security(name="Bearer") |
116
|
|
|
* |
117
|
|
|
* @IsGranted("ROLE_ADMIN") |
118
|
|
|
*/ |
119
|
|
|
public function find(User $user): Response |
120
|
|
|
{ |
121
|
|
|
return $this->json($user, Response::HTTP_OK, [], ['groups' => ['user_full']]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create user. |
126
|
|
|
* |
127
|
|
|
* @Route("", name="create", methods={"POST"}) |
128
|
|
|
* @SWG\Response( |
129
|
|
|
* response=201, |
130
|
|
|
* description="Create user", |
131
|
|
|
* @SWG\Schema( |
132
|
|
|
* type="object", |
133
|
|
|
* ref=@Model(type=User::class, groups={"user_full"}) |
134
|
|
|
* ) |
135
|
|
|
* ) |
136
|
|
|
* @SWG\Response( |
137
|
|
|
* response=400, |
138
|
|
|
* description="Invalid user" |
139
|
|
|
* ) |
140
|
|
|
* @SWG\Parameter( |
141
|
|
|
* name="body", |
142
|
|
|
* in="body", |
143
|
|
|
* description="JSON Payload", |
144
|
|
|
* required=true, |
145
|
|
|
* format="application/json", |
146
|
|
|
* @SWG\Schema( |
147
|
|
|
* type="object", |
148
|
|
|
* ref=@Model(type=User::class, groups={"user_write", "user_create"}) |
149
|
|
|
* ) |
150
|
|
|
* ) |
151
|
|
|
* @SWG\Tag(name="users") |
152
|
|
|
* @Security(name="Bearer") |
153
|
|
|
* |
154
|
|
|
* @IsGranted("ROLE_ADMIN") |
155
|
|
|
*/ |
156
|
|
|
public function create( |
157
|
|
|
DocumentManager $dm, |
158
|
|
|
SerializerInterface $serializer, |
159
|
|
|
ValidatorInterface $validator, |
160
|
|
|
Request $request, |
161
|
|
|
Mailer $mailer, |
162
|
|
|
ResetPasswordTokenManager $resetPasswordTokenManager): Response |
163
|
|
|
{ |
164
|
|
|
$data = $request->getContent(); |
165
|
|
|
|
166
|
|
|
/** @var User $user */ |
167
|
|
|
$user = $serializer->deserialize($data, User::class, 'json', ['groups' => ['user_write', 'user_create']]); |
168
|
|
|
|
169
|
|
|
$violations = $validator->validate($user); |
170
|
|
|
|
171
|
|
|
if ($violations->count() > 0) { |
172
|
|
|
return $this->json($violations, Response::HTTP_BAD_REQUEST); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$user->setResetPasswordToken($resetPasswordTokenManager->generate()); |
176
|
|
|
$mailer->sendWelcomeEmail($user); |
177
|
|
|
|
178
|
|
|
$dm->persist($user); |
179
|
|
|
$dm->flush(); |
180
|
|
|
|
181
|
|
|
return $this->json($user, Response::HTTP_CREATED, [], ['groups' => ['user_full']]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* update user. |
186
|
|
|
* |
187
|
|
|
* @Route("/{id}", name="update", methods={"PUT"}) |
188
|
|
|
* @SWG\Response( |
189
|
|
|
* response=204, |
190
|
|
|
* description="Update user" |
191
|
|
|
* ) |
192
|
|
|
* @SWG\Response( |
193
|
|
|
* response=400, |
194
|
|
|
* description="Invalid user" |
195
|
|
|
* ) |
196
|
|
|
* @SWG\Parameter( |
197
|
|
|
* name="body", |
198
|
|
|
* in="body", |
199
|
|
|
* description="JSON Payload", |
200
|
|
|
* required=true, |
201
|
|
|
* format="application/json", |
202
|
|
|
* @SWG\Schema( |
203
|
|
|
* type="object", |
204
|
|
|
* ref=@Model(type=User::class, groups={"user_write"}) |
205
|
|
|
* ) |
206
|
|
|
* ) |
207
|
|
|
* @SWG\Tag(name="users") |
208
|
|
|
* @Security(name="Bearer") |
209
|
|
|
* |
210
|
|
|
* @IsGranted("ROLE_ADMIN") |
211
|
|
|
*/ |
212
|
|
|
public function update( |
213
|
|
|
User $user, |
214
|
|
|
DocumentManager $dm, |
215
|
|
|
SerializerInterface $serializer, |
216
|
|
|
ValidatorInterface $validator, |
217
|
|
|
Request $request): Response |
218
|
|
|
{ |
219
|
|
|
$data = $request->getContent(); |
220
|
|
|
|
221
|
|
|
/* @var User $user */ |
222
|
|
|
$serializer->deserialize($data, User::class, 'json', [ |
223
|
|
|
'groups' => ['user_write'], |
224
|
|
|
'object_to_populate' => $user, |
225
|
|
|
]); |
226
|
|
|
|
227
|
|
|
$violations = $validator->validate($user); |
228
|
|
|
|
229
|
|
|
if ($violations->count() > 0) { |
230
|
|
|
return $this->json($violations, Response::HTTP_BAD_REQUEST); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$dm->flush(); |
234
|
|
|
|
235
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Delete user. |
240
|
|
|
* |
241
|
|
|
* @Route("/{id}", name="delete", methods={"DELETE"}) |
242
|
|
|
* @SWG\Response( |
243
|
|
|
* response=204, |
244
|
|
|
* description="Delete user" |
245
|
|
|
* ) |
246
|
|
|
* @SWG\Tag(name="users") |
247
|
|
|
* @Security(name="Bearer") |
248
|
|
|
* |
249
|
|
|
* @IsGranted("ROLE_ADMIN") |
250
|
|
|
*/ |
251
|
|
|
public function delete(DocumentManager $dm, User $user): Response |
252
|
|
|
{ |
253
|
|
|
$dm->remove($user); |
254
|
|
|
$dm->flush(); |
255
|
|
|
|
256
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|