1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Controller\MembersArea; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Application\Form\Type\UserType; |
9
|
|
|
use Application\Entity\UserEntity; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Borut Balažek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class UsersController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param Application $app |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function listAction(Request $request, Application $app) |
23
|
|
|
{ |
24
|
|
|
if ( |
25
|
|
|
!$app['security']->isGranted('ROLE_USERS_EDITOR') && |
26
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
27
|
|
|
) { |
28
|
|
|
$app->abort(403); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$limitPerPage = $request->query->get('limit_per_page', 20); |
32
|
|
|
$currentPage = $request->query->get('page'); |
33
|
|
|
|
34
|
|
|
$userResults = $app['orm.em'] |
35
|
|
|
->createQueryBuilder() |
36
|
|
|
->select('u') |
37
|
|
|
->from('Application\Entity\UserEntity', 'u') |
38
|
|
|
->leftJoin('u.profile', 'p') |
39
|
|
|
; |
40
|
|
|
|
41
|
|
|
$pagination = $app['paginator']->paginate( |
42
|
|
|
$userResults, |
43
|
|
|
$currentPage, |
44
|
|
|
$limitPerPage, |
45
|
|
|
array( |
46
|
|
|
'route' => 'members-area.users', |
47
|
|
|
'defaultSortFieldName' => 'u.email', |
48
|
|
|
'defaultSortDirection' => 'asc', |
49
|
|
|
'searchFields' => array( |
50
|
|
|
'u.username', |
51
|
|
|
'u.email', |
52
|
|
|
'u.roles', |
53
|
|
|
'p.firstName', |
54
|
|
|
'p.lastName', |
55
|
|
|
), |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return new Response( |
60
|
|
|
$app['twig']->render( |
61
|
|
|
'contents/members-area/users/list.html.twig', |
62
|
|
|
array( |
63
|
|
|
'pagination' => $pagination, |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Request $request |
71
|
|
|
* @param Application $app |
72
|
|
|
* |
73
|
|
|
* @return Response |
74
|
|
|
*/ |
75
|
|
|
public function newAction(Request $request, Application $app) |
76
|
|
|
{ |
77
|
|
|
if ( |
78
|
|
|
!$app['security']->isGranted('ROLE_USERS_EDITOR') && |
79
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
80
|
|
|
) { |
81
|
|
|
$app->abort(403); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$form = $app['form.factory']->create( |
85
|
|
|
new UserType($app), |
86
|
|
|
new UserEntity() |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
if ($request->getMethod() == 'POST') { |
90
|
|
|
$form->handleRequest($request); |
91
|
|
|
|
92
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
93
|
|
|
$userEntity = $form->getData(); |
94
|
|
|
|
95
|
|
|
/*** Image ***/ |
96
|
|
|
$userEntity |
97
|
|
|
->getProfile() |
98
|
|
|
->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
99
|
|
|
->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
100
|
|
|
->imageUpload() |
101
|
|
|
; |
102
|
|
|
|
103
|
|
|
/*** Password ***/ |
104
|
|
|
$userEntity->setPlainPassword( |
105
|
|
|
$userEntity->getPlainPassword(), // This getPassword() is here just the plain password. That's why we need to convert it |
106
|
|
|
$app['security.encoder_factory'] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$app['orm.em']->persist($userEntity); |
110
|
|
|
$app['orm.em']->flush(); |
111
|
|
|
|
112
|
|
|
$app['flashbag']->add( |
113
|
|
|
'success', |
114
|
|
|
$app['translator']->trans( |
115
|
|
|
'members-area.users.new.successText' |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return $app->redirect( |
120
|
|
|
$app['url_generator']->generate( |
121
|
|
|
'members-area.users.edit', |
122
|
|
|
array( |
123
|
|
|
'id' => $userEntity->getId(), |
124
|
|
|
) |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return new Response( |
131
|
|
|
$app['twig']->render( |
132
|
|
|
'contents/members-area/users/new.html.twig', |
133
|
|
|
array( |
134
|
|
|
'form' => $form->createView(), |
135
|
|
|
) |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $id |
142
|
|
|
* @param Application $app |
143
|
|
|
* |
144
|
|
|
* @return Response |
145
|
|
|
*/ |
146
|
|
|
public function detailAction($id, Application $app) |
147
|
|
|
{ |
148
|
|
|
if ( |
149
|
|
|
!$app['security']->isGranted('ROLE_USERS_EDITOR') && |
150
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
151
|
|
|
) { |
152
|
|
|
$app->abort(403); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$user = $app['orm.em']->find('Application\Entity\UserEntity', $id); |
156
|
|
|
|
157
|
|
|
if (!$user) { |
158
|
|
|
$app->abort(404); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return new Response( |
162
|
|
|
$app['twig']->render( |
163
|
|
|
'contents/members-area/users/detail.html.twig', |
164
|
|
|
array( |
165
|
|
|
'user' => $user, |
166
|
|
|
) |
167
|
|
|
) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function editAction($id, Request $request, Application $app) |
172
|
|
|
{ |
173
|
|
|
if ( |
174
|
|
|
!$app['security']->isGranted('ROLE_USERS_EDITOR') && |
175
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
176
|
|
|
) { |
177
|
|
|
$app->abort(403); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$user = $app['orm.em']->find( |
181
|
|
|
'Application\Entity\UserEntity', |
182
|
|
|
$id |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
if (!$user) { |
186
|
|
|
$app->abort(404); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$form = $app['form.factory']->create( |
190
|
|
|
new UserType($app), |
191
|
|
|
$user |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
if ($request->getMethod() == 'POST') { |
195
|
|
|
$form->handleRequest($request); |
196
|
|
|
|
197
|
|
|
if ($form->isValid()) { |
198
|
|
|
$userEntity = $form->getData(); |
199
|
|
|
|
200
|
|
|
if ( |
201
|
|
|
$userEntity->isLocked() && |
202
|
|
|
$userEntity->hasRole('ROLE_SUPER_ADMIN') |
203
|
|
|
) { |
204
|
|
|
$app['flashbag']->add( |
205
|
|
|
'danger', |
206
|
|
|
$app['translator']->trans( |
207
|
|
|
'A super admin user can not be locked!' |
208
|
|
|
) |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
return $app->redirect( |
212
|
|
|
$app['url_generator']->generate( |
213
|
|
|
'members-area.users.edit', |
214
|
|
|
array( |
215
|
|
|
'id' => $userEntity->getId(), |
216
|
|
|
) |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/*** Image ***/ |
222
|
|
|
$userEntity |
223
|
|
|
->getProfile() |
224
|
|
|
->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
225
|
|
|
->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
226
|
|
|
->imageUpload() |
227
|
|
|
; |
228
|
|
|
|
229
|
|
|
/*** Password ***/ |
230
|
|
|
if ($userEntity->getPlainPassword()) { |
231
|
|
|
$userEntity->setPlainPassword( |
232
|
|
|
$userEntity->getPlainPassword(), // This getPassword() is here just the plain password. That's why we need to convert it |
233
|
|
|
$app['security.encoder_factory'] |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$app['orm.em']->persist($userEntity); |
238
|
|
|
$app['orm.em']->flush(); |
239
|
|
|
|
240
|
|
|
$app['flashbag']->add( |
241
|
|
|
'success', |
242
|
|
|
$app['translator']->trans( |
243
|
|
|
'members-area.users.edit.successText' |
244
|
|
|
) |
245
|
|
|
); |
246
|
|
|
|
247
|
|
|
return $app->redirect( |
248
|
|
|
$app['url_generator']->generate( |
249
|
|
|
'members-area.users.edit', |
250
|
|
|
array( |
251
|
|
|
'id' => $userEntity->getId(), |
252
|
|
|
) |
253
|
|
|
) |
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return new Response( |
259
|
|
|
$app['twig']->render( |
260
|
|
|
'contents/members-area/users/edit.html.twig', |
261
|
|
|
array( |
262
|
|
|
'form' => $form->createView(), |
263
|
|
|
'user' => $user, |
264
|
|
|
) |
265
|
|
|
) |
266
|
|
|
); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
View Code Duplication |
public function removeAction($id, Request $request, Application $app) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
if ( |
272
|
|
|
!$app['security']->isGranted('ROLE_USERS_EDITOR') && |
273
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
274
|
|
|
) { |
275
|
|
|
$app->abort(403); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$users = array(); |
279
|
|
|
$ids = $request->query->get('ids', false); |
280
|
|
|
$idsExploded = explode(',', $ids); |
281
|
|
|
foreach ($idsExploded as $singleId) { |
282
|
|
|
$singleEntity = $app['orm.em']->find( |
283
|
|
|
'Application\Entity\UserEntity', |
284
|
|
|
$singleId |
285
|
|
|
); |
286
|
|
|
|
287
|
|
|
if ($singleEntity) { |
288
|
|
|
$users[] = $singleEntity; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$user = $app['orm.em']->find('Application\Entity\UserEntity', $id); |
293
|
|
|
|
294
|
|
|
if ( |
295
|
|
|
( |
296
|
|
|
!$user && |
297
|
|
|
$ids === false |
298
|
|
|
) || |
299
|
|
|
( |
300
|
|
|
empty($users) && |
301
|
|
|
$ids !== false |
302
|
|
|
) |
303
|
|
|
) { |
304
|
|
|
$app->abort(404); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$confirmAction = $app['request']->query->has('action') && $app['request']->query->get('action') == 'confirm'; |
308
|
|
|
|
309
|
|
|
if ($confirmAction) { |
310
|
|
|
try { |
311
|
|
|
if (!empty($users)) { |
312
|
|
|
foreach ($users as $user) { |
313
|
|
|
$app['orm.em']->remove($user); |
314
|
|
|
} |
315
|
|
|
} else { |
316
|
|
|
$app['orm.em']->remove($user); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$app['orm.em']->flush(); |
320
|
|
|
|
321
|
|
|
$app['flashbag']->add( |
322
|
|
|
'success', |
323
|
|
|
$app['translator']->trans( |
324
|
|
|
'members-area.users.remove.successText' |
325
|
|
|
) |
326
|
|
|
); |
327
|
|
|
} catch (\Exception $e) { |
328
|
|
|
$app['flashbag']->add( |
329
|
|
|
'danger', |
330
|
|
|
$app['translator']->trans( |
331
|
|
|
$e->getMessage() |
332
|
|
|
) |
333
|
|
|
); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $app->redirect( |
337
|
|
|
$app['url_generator']->generate('members-area.users') |
338
|
|
|
); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return new Response( |
342
|
|
|
$app['twig']->render( |
343
|
|
|
'contents/members-area/users/remove.html.twig', |
344
|
|
|
array( |
345
|
|
|
'user' => $user, |
346
|
|
|
'users' => $users, |
347
|
|
|
'ids' => $ids, |
348
|
|
|
) |
349
|
|
|
) |
350
|
|
|
); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.