|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Starkerxp\UserBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
7
|
|
|
use Starkerxp\StructureBundle\Controller\StructureController; |
|
8
|
|
|
use Starkerxp\UserBundle\Entity\User; |
|
9
|
|
|
use Starkerxp\UserBundle\Events; |
|
10
|
|
|
use Starkerxp\UserBundle\Form\Type\UserType; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
class UserController extends StructureController |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @ApiDoc( |
|
19
|
|
|
* resource=true, |
|
20
|
|
|
* description="Liste les users.", |
|
21
|
|
|
* section="User", |
|
22
|
|
|
* parameters={ |
|
23
|
|
|
* { |
|
24
|
|
|
* "name"="offset", |
|
25
|
|
|
* "dataType"="integer", |
|
26
|
|
|
* "requirement"="\d+", |
|
27
|
|
|
* "description"="starkerxp_structure.doc.offset.result", |
|
28
|
|
|
* "required"="false" |
|
29
|
|
|
* }, |
|
30
|
|
|
* { |
|
31
|
|
|
* "name"="limit", |
|
32
|
|
|
* "dataType"="integer", |
|
33
|
|
|
* "requirement"="\d+", |
|
34
|
|
|
* "description"="starkerxp_structure.doc.limit.result", |
|
35
|
|
|
* "required"="false" |
|
36
|
|
|
* }, |
|
37
|
|
|
* { |
|
38
|
|
|
* "name"="fields", |
|
39
|
|
|
* "dataType"="string", |
|
40
|
|
|
* "requirement"="\w+", |
|
41
|
|
|
* "description"="starkerxp_structure.doc.list_field.entity", |
|
42
|
|
|
* "required"="false" |
|
43
|
|
|
* }, |
|
44
|
|
|
* { |
|
45
|
|
|
* "name"="sort", |
|
46
|
|
|
* "dataType"="string", |
|
47
|
|
|
* "requirement"="\w+", |
|
48
|
|
|
* "description"="starkerxp_structure.doc.sort.result", |
|
49
|
|
|
* "required"="false" |
|
50
|
|
|
* } |
|
51
|
|
|
* }, |
|
52
|
|
|
* views = { "default" } |
|
53
|
|
|
* ) |
|
54
|
|
|
*/ |
|
55
|
|
|
public function cgetAction(Request $request) |
|
56
|
|
|
{ |
|
57
|
|
|
$manager = $this->get("starkerxp_user.manager.user"); |
|
58
|
|
|
try { |
|
59
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
60
|
|
|
$orderBy = $this->getOrderBy($options['sort']); |
|
61
|
|
|
$resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
64
|
|
|
} |
|
65
|
|
|
if (empty($resultSets)) { |
|
66
|
|
|
return new JsonResponse([]); |
|
67
|
|
|
} |
|
68
|
|
|
$retour = array_map( |
|
69
|
|
|
function ($element) use ($manager, $options) { |
|
70
|
|
|
return $manager->toArray($element, $this->getFields($options['fields'])); |
|
71
|
|
|
}, |
|
72
|
|
|
$resultSets |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return new JsonResponse($retour); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @ApiDoc( |
|
81
|
|
|
* resource=true, |
|
82
|
|
|
* description="Affiche un user.", |
|
83
|
|
|
* section="User", |
|
84
|
|
|
* requirements={ |
|
85
|
|
|
* { |
|
86
|
|
|
* "name"="user_id", |
|
87
|
|
|
* "dataType"="integer", |
|
88
|
|
|
* "requirement"="\d+", |
|
89
|
|
|
* "description"="Show an element" |
|
90
|
|
|
* } |
|
91
|
|
|
* }, |
|
92
|
|
|
* parameters={ |
|
93
|
|
|
* { |
|
94
|
|
|
* "name"="fields", |
|
95
|
|
|
* "dataType"="string", |
|
96
|
|
|
* "requirement"="\w+", |
|
97
|
|
|
* "description"="starkerxp_structure.doc.list_field.entity", |
|
98
|
|
|
* "required"="false" |
|
99
|
|
|
* } |
|
100
|
|
|
* }, |
|
101
|
|
|
* views = { "default" } |
|
102
|
|
|
* ) |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getAction(Request $request) |
|
105
|
|
|
{ |
|
106
|
|
|
$manager = $this->get("starkerxp_user.manager.user"); |
|
107
|
|
|
try { |
|
108
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
109
|
|
|
/** @var User $entite */ |
|
110
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('user_id')])) { |
|
111
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "user")], 404); |
|
112
|
|
|
} |
|
113
|
|
|
} catch (\Exception $e) { |
|
114
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
115
|
|
|
} |
|
116
|
|
|
$retour = $manager->toArray($entite, $this->getFields($options['fields'])); |
|
117
|
|
|
|
|
118
|
|
|
return new JsonResponse($retour); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @ApiDoc( |
|
123
|
|
|
* resource=true, |
|
124
|
|
|
* description="Ajoute un user.", |
|
125
|
|
|
* section="User", |
|
126
|
|
|
* requirements={ |
|
127
|
|
|
* { |
|
128
|
|
|
* "name"="email", |
|
129
|
|
|
* "dataType"="string", |
|
130
|
|
|
* "requirement"="\w+", |
|
131
|
|
|
* "description"="Définit l'identifiant de connexion" |
|
132
|
|
|
* }, |
|
133
|
|
|
* { |
|
134
|
|
|
* "name"="type", |
|
135
|
|
|
* "dataType"="integer", |
|
136
|
|
|
* "requirement"="\d+", |
|
137
|
|
|
* "description"="1 - User / 2 - Api" |
|
138
|
|
|
* }, |
|
139
|
|
|
* }, |
|
140
|
|
|
* views = { "default" } |
|
141
|
|
|
* ) |
|
142
|
|
|
*/ |
|
143
|
|
|
public function postAction(Request $request) |
|
144
|
|
|
{ |
|
145
|
|
|
$manager = $this->get("starkerxp_user.manager.user"); |
|
146
|
|
|
try { |
|
147
|
|
|
$form = $this->createForm(UserType::class, [], ['method' => 'POST']); |
|
148
|
|
|
$form->submit($this->getRequestData($request)); |
|
149
|
|
|
if ($form->isValid()) { |
|
150
|
|
|
$user = $form->getData(); |
|
151
|
|
|
$manager->insert($user); |
|
152
|
|
|
$this->dispatch(Events::USER_CREATED, new GenericEvent($user)); |
|
153
|
|
|
|
|
154
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.created", "user")], 201); |
|
155
|
|
|
} |
|
156
|
|
|
} catch (\Exception $e) { |
|
157
|
|
|
$manager->rollback(); |
|
158
|
|
|
|
|
159
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @ApiDoc( |
|
167
|
|
|
* resource=true, |
|
168
|
|
|
* description="Modifie un user.", |
|
169
|
|
|
* section="User", |
|
170
|
|
|
* requirements={ |
|
171
|
|
|
* { |
|
172
|
|
|
* "name"="user_id", |
|
173
|
|
|
* "dataType"="integer", |
|
174
|
|
|
* "requirement"="\d+", |
|
175
|
|
|
* "description"="Edit an element." |
|
176
|
|
|
* } |
|
177
|
|
|
* }, |
|
178
|
|
|
* views = { "default" } |
|
179
|
|
|
* ) |
|
180
|
|
|
*/ |
|
181
|
|
|
public function putAction(Request $request) |
|
182
|
|
|
{ |
|
183
|
|
|
$manager = $this->get("starkerxp_user.manager.company"); |
|
184
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('user_id')])) { |
|
185
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "user")], 404); |
|
186
|
|
|
} |
|
187
|
|
|
// Un user ne peut modifier un autre user sauf si ce dernier est un super admin. |
|
188
|
|
|
if ($this->getUser()->getId() != $entite->getId() && !$this->isGranted("ROLE_SUPER_ADMIN")) { |
|
189
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_updated_is_not_admin", "user")], 400); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$manager->beginTransaction(); |
|
193
|
|
|
try { |
|
194
|
|
|
$form = $this->createForm(UserType::class, $entite, ['method' => 'PUT']); |
|
195
|
|
|
$form->submit($this->getRequestData($request), false); |
|
196
|
|
|
if ($form->isValid()) { |
|
197
|
|
|
$entite = $form->getData(); |
|
198
|
|
|
$manager->update($entite); |
|
199
|
|
|
$this->dispatch(Events::USER_UPDATED, new GenericEvent($entite)); |
|
200
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.updated", "user")], 204); |
|
201
|
|
|
} |
|
202
|
|
|
} catch (\Exception $e) { |
|
203
|
|
|
$manager->rollback(); |
|
204
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
205
|
|
|
} |
|
206
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @ApiDoc( |
|
214
|
|
|
* resource=true, |
|
215
|
|
|
* description="Delete a user.", |
|
216
|
|
|
* section="User", |
|
217
|
|
|
* requirements={ |
|
218
|
|
|
* { |
|
219
|
|
|
* "name"="user_id", |
|
220
|
|
|
* "dataType"="integer", |
|
221
|
|
|
* "requirement"="\d+", |
|
222
|
|
|
* "description"="Delete an element." |
|
223
|
|
|
* } |
|
224
|
|
|
* }, |
|
225
|
|
|
* views = { "default" } |
|
226
|
|
|
* ) |
|
227
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
|
228
|
|
|
*/ |
|
229
|
|
|
public function deleteAction(Request $request) |
|
230
|
|
|
{ |
|
231
|
|
|
$manager = $this->get("starkerxp_user.manager.user"); |
|
232
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('user_id')])) { |
|
233
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "user")], 404); |
|
234
|
|
|
} |
|
235
|
|
|
try { |
|
236
|
|
|
$manager->delete($entite); |
|
237
|
|
|
} catch (\Exception $e) { |
|
238
|
|
|
$manager->rollback(); |
|
239
|
|
|
|
|
240
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
241
|
|
|
} |
|
242
|
|
|
$this->dispatch(Events::COMPANY_DELETED, new GenericEvent($request->get('user_id'))); |
|
243
|
|
|
|
|
244
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.deleted", "user")], 204); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
} |
|
|
|
|
|
|
248
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.