1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\ZAuthModule\Controller; |
15
|
|
|
|
16
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
22
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
23
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
24
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
25
|
|
|
use Zikula\Bundle\CoreBundle\Controller\AbstractController; |
26
|
|
|
use Zikula\Bundle\CoreBundle\Filter\AlphaFilter; |
27
|
|
|
use Zikula\Bundle\CoreBundle\Response\PlainResponse; |
28
|
|
|
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface; |
29
|
|
|
use Zikula\Bundle\HookBundle\Hook\ProcessHook; |
30
|
|
|
use Zikula\Bundle\HookBundle\Hook\ValidationHook; |
31
|
|
|
use Zikula\Bundle\HookBundle\Hook\ValidationProviders; |
32
|
|
|
use Zikula\Component\SortableColumns\Column; |
33
|
|
|
use Zikula\Component\SortableColumns\SortableColumns; |
34
|
|
|
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface; |
35
|
|
|
use Zikula\PermissionsModule\Annotation\PermissionCheck; |
36
|
|
|
use Zikula\ThemeModule\Engine\Annotation\Theme; |
37
|
|
|
use Zikula\UsersModule\Collector\AuthenticationMethodCollector; |
38
|
|
|
use Zikula\UsersModule\Constant as UsersConstant; |
39
|
|
|
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface; |
40
|
|
|
use Zikula\UsersModule\Entity\UserEntity; |
41
|
|
|
use Zikula\UsersModule\Event\ActiveUserPostUpdatedEvent; |
42
|
|
|
use Zikula\UsersModule\Event\EditUserFormPostCreatedEvent; |
43
|
|
|
use Zikula\UsersModule\Event\EditUserFormPostValidatedEvent; |
44
|
|
|
use Zikula\UsersModule\Event\RegistrationPostDeletedEvent; |
45
|
|
|
use Zikula\UsersModule\Event\RegistrationPostSuccessEvent; |
46
|
|
|
use Zikula\UsersModule\Helper\MailHelper as UsersMailHelper; |
47
|
|
|
use Zikula\UsersModule\Helper\RegistrationHelper; |
48
|
|
|
use Zikula\UsersModule\HookSubscriber\UserManagementUiHooksSubscriber; |
49
|
|
|
use Zikula\ZAuthModule\Entity\AuthenticationMappingEntity; |
50
|
|
|
use Zikula\ZAuthModule\Entity\RepositoryInterface\AuthenticationMappingRepositoryInterface; |
51
|
|
|
use Zikula\ZAuthModule\Form\Type\AdminCreatedUserType; |
52
|
|
|
use Zikula\ZAuthModule\Form\Type\AdminModifyUserType; |
53
|
|
|
use Zikula\ZAuthModule\Form\Type\BatchForcePasswordChangeType; |
54
|
|
|
use Zikula\ZAuthModule\Form\Type\SendVerificationConfirmationType; |
55
|
|
|
use Zikula\ZAuthModule\Form\Type\TogglePasswordConfirmationType; |
56
|
|
|
use Zikula\ZAuthModule\Helper\AdministrationActionsHelper; |
57
|
|
|
use Zikula\ZAuthModule\Helper\BatchPasswordChangeHelper; |
58
|
|
|
use Zikula\ZAuthModule\Helper\LostPasswordVerificationHelper; |
59
|
|
|
use Zikula\ZAuthModule\Helper\MailHelper; |
60
|
|
|
use Zikula\ZAuthModule\Helper\RegistrationVerificationHelper; |
61
|
|
|
use Zikula\ZAuthModule\ZAuthConstant; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Class UserAdministrationController |
65
|
|
|
* |
66
|
|
|
* @Route("/admin") |
67
|
|
|
*/ |
68
|
|
|
class UserAdministrationController extends AbstractController |
69
|
|
|
{ |
70
|
|
|
/** |
71
|
|
|
* @Route("/list/{sort}/{sortdir}/{letter}/{page}", methods = {"GET"}, requirements={"page" = "\d+"}) |
72
|
|
|
* @PermissionCheck("moderate") |
73
|
|
|
* @Theme("admin") |
74
|
|
|
* @Template("@ZikulaZAuthModule/UserAdministration/list.html.twig") |
75
|
|
|
*/ |
76
|
|
|
public function listMappings( |
77
|
|
|
Request $request, |
78
|
|
|
AuthenticationMappingRepositoryInterface $authenticationMappingRepository, |
79
|
|
|
RouterInterface $router, |
80
|
|
|
AdministrationActionsHelper $actionsHelper, |
81
|
|
|
string $sort = 'uid', |
82
|
|
|
string $sortdir = 'DESC', |
83
|
|
|
string $letter = 'all', |
84
|
|
|
int $page = 1 |
85
|
|
|
): array { |
86
|
|
|
$sortableColumns = new SortableColumns($router, 'zikulazauthmodule_useradministration_list', 'sort', 'sortdir'); |
87
|
|
|
$sortableColumns->addColumns([new Column('uname'), new Column('uid')]); |
88
|
|
|
$sortableColumns->setOrderByFromRequest($request); |
89
|
|
|
$sortableColumns->setAdditionalUrlParameters([ |
90
|
|
|
'letter' => $letter, |
91
|
|
|
'page' => $page |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$filter = []; |
95
|
|
|
if (!empty($letter) && 'all' !== $letter) { |
96
|
|
|
$filter['uname'] = ['operator' => 'like', 'operand' => "${letter}%"]; |
97
|
|
|
} |
98
|
|
|
$pageSize = $this->getVar(ZAuthConstant::MODVAR_ITEMS_PER_PAGE, ZAuthConstant::DEFAULT_ITEMS_PER_PAGE); |
99
|
|
|
$paginator = $authenticationMappingRepository->query($filter, [$sort => $sortdir], 'and', $page, $pageSize); |
100
|
|
|
$paginator->setRoute('zikulazauthmodule_useradministration_list'); |
101
|
|
|
$routeParameters = [ |
102
|
|
|
'sort' => $sort, |
103
|
|
|
'sortdir' => $sortdir, |
104
|
|
|
'letter' => $letter, |
105
|
|
|
]; |
106
|
|
|
$paginator->setRouteParameters($routeParameters); |
107
|
|
|
|
108
|
|
|
return [ |
109
|
|
|
'sort' => $sortableColumns->generateSortableColumns(), |
110
|
|
|
'actionsHelper' => $actionsHelper, |
111
|
|
|
'alpha' => new AlphaFilter('zikulazauthmodule_useradministration_list', $routeParameters, $letter), |
112
|
|
|
'paginator' => $paginator |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Called from UsersModule/Resources/public/js/Zikula.Users.Admin.View.js |
118
|
|
|
* to populate a username search |
119
|
|
|
* |
120
|
|
|
* @Route("/getusersbyfragmentastable", methods = {"POST"}, options={"expose"=true, "i18n"=false}) |
121
|
|
|
*/ |
122
|
|
|
public function getUsersByFragmentAsTable( |
123
|
|
|
Request $request, |
124
|
|
|
AuthenticationMappingRepositoryInterface $authenticationMappingRepository, |
125
|
|
|
AdministrationActionsHelper $actionsHelper |
126
|
|
|
): Response { |
127
|
|
|
if (!$this->hasPermission('ZikulaZAuthModule', '::', ACCESS_MODERATE)) { |
128
|
|
|
return new PlainResponse(''); |
129
|
|
|
} |
130
|
|
|
$fragment = $request->request->get('fragment'); |
131
|
|
|
$filter = [ |
132
|
|
|
'uname' => ['operator' => 'like', 'operand' => $fragment . '%'] |
133
|
|
|
]; |
134
|
|
|
$mappings = $authenticationMappingRepository->query($filter); |
135
|
|
|
|
136
|
|
|
return $this->render('@ZikulaZAuthModule/UserAdministration/userlist.html.twig', [ |
137
|
|
|
'mappings' => $mappings->getResults(), |
138
|
|
|
'actionsHelper' => $actionsHelper |
139
|
|
|
], new PlainResponse()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @Route("/user/create") |
144
|
|
|
* @PermissionCheck("admin") |
145
|
|
|
* @Theme("admin") |
146
|
|
|
* @Template("@ZikulaZAuthModule/UserAdministration/create.html.twig") |
147
|
|
|
* |
148
|
|
|
* @return array|RedirectResponse |
149
|
|
|
*/ |
150
|
|
|
public function create( |
151
|
|
|
Request $request, |
152
|
|
|
VariableApiInterface $variableApi, |
153
|
|
|
AuthenticationMethodCollector $authenticationMethodCollector, |
154
|
|
|
UserRepositoryInterface $userRepository, |
155
|
|
|
RegistrationHelper $registrationHelper, |
156
|
|
|
UsersMailHelper $mailHelper, |
157
|
|
|
EventDispatcherInterface $eventDispatcher, |
158
|
|
|
HookDispatcherInterface $hookDispatcher |
159
|
|
|
) { |
160
|
|
|
$mapping = new AuthenticationMappingEntity(); |
161
|
|
|
$form = $this->createForm(AdminCreatedUserType::class, $mapping, [ |
162
|
|
|
'minimumPasswordLength' => $variableApi->get('ZikulaZAuthModule', ZAuthConstant::MODVAR_PASSWORD_MINIMUM_LENGTH, ZAuthConstant::PASSWORD_MINIMUM_LENGTH) |
163
|
|
|
]); |
164
|
|
|
$editUserFormPostCreatedEvent = new EditUserFormPostCreatedEvent($form); |
165
|
|
|
$eventDispatcher->dispatch($editUserFormPostCreatedEvent); |
166
|
|
|
$form->handleRequest($request); |
167
|
|
|
|
168
|
|
|
$hook = new ValidationHook(new ValidationProviders()); |
169
|
|
|
$hookDispatcher->dispatch(UserManagementUiHooksSubscriber::EDIT_VALIDATE, $hook); |
170
|
|
|
$validators = $hook->getValidators(); |
171
|
|
|
|
172
|
|
|
if ($form->isSubmitted() && $form->isValid() && !$validators->hasErrors()) { |
173
|
|
|
if ($form->get('submit')->isClicked()) { |
|
|
|
|
174
|
|
|
$mapping = $form->getData(); |
175
|
|
|
$passToSend = $form['sendpass']->getData() ? $mapping->getPass() : ''; |
176
|
|
|
$authMethodName = (ZAuthConstant::AUTHENTICATION_METHOD_EITHER === $mapping->getMethod()) ? ZAuthConstant::AUTHENTICATION_METHOD_UNAME : $mapping->getMethod(); |
177
|
|
|
$authMethod = $authenticationMethodCollector->get($authMethodName); |
178
|
|
|
|
179
|
|
|
if ($request->hasSession() && ($session = $request->getSession())) { |
180
|
|
|
$session->set(ZAuthConstant::MODVAR_EMAIL_VERIFICATION_REQUIRED, ($form['usermustverify']->getData() ? 'Y' : 'N')); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$userData = $mapping->getUserEntityData(); |
184
|
|
|
if (null === $userData['uid']) { |
185
|
|
|
unset($userData['uid']); |
186
|
|
|
} |
187
|
|
|
$user = new UserEntity(); |
188
|
|
|
$user->merge($userData); |
189
|
|
|
$user->setAttribute(UsersConstant::AUTHENTICATION_METHOD_ATTRIBUTE_KEY, $mapping->getMethod()); |
190
|
|
|
$registrationHelper->registerNewUser($user); |
191
|
|
|
if (UsersConstant::ACTIVATED_PENDING_REG === $user->getActivated()) { |
192
|
|
|
$notificationErrors = $mailHelper->createAndSendRegistrationMail($user, $form['usernotification']->getData(), $form['adminnotification']->getData(), $passToSend); |
193
|
|
|
} else { |
194
|
|
|
$notificationErrors = $mailHelper->createAndSendUserMail($user, $form['usernotification']->getData(), $form['adminnotification']->getData(), $passToSend); |
195
|
|
|
} |
196
|
|
|
if (!empty($notificationErrors)) { |
197
|
|
|
$this->addFlash('error', 'Errors creating user!'); |
198
|
|
|
$this->addFlash('error', implode('<br />', $notificationErrors)); |
199
|
|
|
} |
200
|
|
|
$mapping->setUid($user->getUid()); |
201
|
|
|
$mapping->setVerifiedEmail(!$form['usermustverify']->getData()); |
202
|
|
|
if (!$authMethod->register($mapping->toArray())) { |
203
|
|
|
$this->addFlash('error', 'The create process failed for an unknown reason.'); |
204
|
|
|
$userRepository->removeAndFlush($user); |
205
|
|
|
$eventDispatcher->dispatch(new RegistrationPostDeletedEvent($user)); |
206
|
|
|
|
207
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
208
|
|
|
} |
209
|
|
|
$eventDispatcher->dispatch(new EditUserFormPostValidatedEvent($form, $user)); |
210
|
|
|
$hook = new ProcessHook($user->getUid()); |
211
|
|
|
$hookDispatcher->dispatch(UserManagementUiHooksSubscriber::EDIT_PROCESS, $hook); |
212
|
|
|
$eventDispatcher->dispatch(new RegistrationPostSuccessEvent($user)); |
213
|
|
|
|
214
|
|
|
if (UsersConstant::ACTIVATED_PENDING_REG === $user->getActivated()) { |
215
|
|
|
$this->addFlash('status', 'Done! Created new registration application.'); |
216
|
|
|
} elseif (null !== $user->getActivated()) { |
|
|
|
|
217
|
|
|
$this->addFlash('status', 'Done! Created new user account.'); |
218
|
|
|
} else { |
219
|
|
|
$this->addFlash('error', 'Warning! New user information has been saved, however there may have been an issue saving it properly.'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
223
|
|
|
} |
224
|
|
|
if ($form->get('cancel')->isClicked()) { |
225
|
|
|
$this->addFlash('status', 'Operation cancelled.'); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return [ |
230
|
|
|
'form' => $form->createView(), |
231
|
|
|
'additionalTemplates' => isset($editUserFormPostCreatedEvent) ? $editUserFormPostCreatedEvent->getTemplates() : [] |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @Route("/user/modify/{mapping}", requirements={"mapping" = "^[1-9]\d*$"}) |
237
|
|
|
* @Theme("admin") |
238
|
|
|
* @Template("@ZikulaZAuthModule/UserAdministration/modify.html.twig") |
239
|
|
|
* |
240
|
|
|
* @return array|RedirectResponse |
241
|
|
|
* @throws AccessDeniedException Thrown if the user hasn't edit permissions for the mapping record |
242
|
|
|
*/ |
243
|
|
|
public function modify( |
244
|
|
|
Request $request, |
245
|
|
|
AuthenticationMappingEntity $mapping, |
246
|
|
|
VariableApiInterface $variableApi, |
247
|
|
|
EncoderFactoryInterface $encoderFactory, |
248
|
|
|
UserRepositoryInterface $userRepository, |
249
|
|
|
AuthenticationMappingRepositoryInterface $authenticationMappingRepository, |
250
|
|
|
EventDispatcherInterface $eventDispatcher, |
251
|
|
|
HookDispatcherInterface $hookDispatcher |
252
|
|
|
) { |
253
|
|
|
if (!$this->hasPermission('ZikulaZAuthModule::', $mapping->getUname() . '::' . $mapping->getUid(), ACCESS_EDIT)) { |
254
|
|
|
throw new AccessDeniedException(); |
255
|
|
|
} |
256
|
|
|
if (1 === $mapping->getUid()) { |
257
|
|
|
throw new AccessDeniedException($this->trans("Error! You can't edit the guest account.")); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$form = $this->createForm(AdminModifyUserType::class, $mapping, [ |
261
|
|
|
'minimumPasswordLength' => $variableApi->get('ZikulaZAuthModule', ZAuthConstant::MODVAR_PASSWORD_MINIMUM_LENGTH, ZAuthConstant::PASSWORD_MINIMUM_LENGTH) |
262
|
|
|
]); |
263
|
|
|
$originalMapping = clone $mapping; |
264
|
|
|
$editUserFormPostCreatedEvent = new EditUserFormPostCreatedEvent($form); |
265
|
|
|
$eventDispatcher->dispatch($editUserFormPostCreatedEvent); |
266
|
|
|
$form->handleRequest($request); |
267
|
|
|
|
268
|
|
|
$hook = new ValidationHook(new ValidationProviders()); |
269
|
|
|
$hookDispatcher->dispatch(UserManagementUiHooksSubscriber::EDIT_VALIDATE, $hook); |
270
|
|
|
$validators = $hook->getValidators(); |
271
|
|
|
|
272
|
|
|
$originalUser = clone $userRepository->find($mapping->getUid()); |
273
|
|
|
|
274
|
|
|
if ($form->isSubmitted() && $form->isValid() && !$validators->hasErrors()) { |
275
|
|
|
if ($form->get('submit')->isClicked()) { |
276
|
|
|
/** @var AuthenticationMappingEntity $mapping */ |
277
|
|
|
$mapping = $form->getData(); |
278
|
|
|
if ($form->get('setpass')->getData()) { |
279
|
|
|
$mapping->setPass($encoderFactory->getEncoder($mapping)->encodePassword($mapping->getPass(), null)); |
280
|
|
|
} else { |
281
|
|
|
$mapping->setPass($originalMapping->getPass()); |
282
|
|
|
} |
283
|
|
|
$authenticationMappingRepository->persistAndFlush($mapping); |
284
|
|
|
/** @var UserEntity $user */ |
285
|
|
|
$user = $userRepository->find($mapping->getUid()); |
286
|
|
|
$user->merge($mapping->getUserEntityData()); |
287
|
|
|
$userRepository->persistAndFlush($user); |
288
|
|
|
|
289
|
|
|
$eventDispatcher->dispatch(new ActiveUserPostUpdatedEvent($user, $originalUser)); |
290
|
|
|
|
291
|
|
|
$eventDispatcher->dispatch(new EditUserFormPostValidatedEvent($form, $user)); |
292
|
|
|
$hookDispatcher->dispatch(UserManagementUiHooksSubscriber::EDIT_PROCESS, new ProcessHook($mapping->getUid())); |
293
|
|
|
|
294
|
|
|
$this->addFlash('status', "Done! Saved user's account information."); |
295
|
|
|
} elseif ($form->get('cancel')->isClicked()) { |
296
|
|
|
$this->addFlash('status', 'Operation cancelled.'); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return [ |
303
|
|
|
'form' => $form->createView(), |
304
|
|
|
'additionalTemplates' => isset($editUserFormPostCreatedEvent) ? $editUserFormPostCreatedEvent->getTemplates() : [] |
305
|
|
|
]; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @Route("/verify/{mapping}", requirements={"mapping" = "^[1-9]\d*$"}) |
310
|
|
|
* @PermissionCheck("moderate") |
311
|
|
|
* @Theme("admin") |
312
|
|
|
* @Template("@ZikulaZAuthModule/UserAdministration/verify.html.twig") |
313
|
|
|
* |
314
|
|
|
* @return array|RedirectResponse |
315
|
|
|
*/ |
316
|
|
|
public function verify( |
317
|
|
|
Request $request, |
318
|
|
|
AuthenticationMappingEntity $mapping, |
319
|
|
|
AuthenticationMappingRepositoryInterface $authenticationMappingRepository, |
320
|
|
|
RegistrationVerificationHelper $registrationVerificationHelper |
321
|
|
|
) { |
322
|
|
|
$form = $this->createForm(SendVerificationConfirmationType::class, [ |
323
|
|
|
'mapping' => $mapping->getId() |
324
|
|
|
]); |
325
|
|
|
|
326
|
|
|
$form->handleRequest($request); |
327
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
328
|
|
|
if ($form->get('confirm')->isClicked()) { |
329
|
|
|
/** @var AuthenticationMappingEntity $modifiedMapping */ |
330
|
|
|
$modifiedMapping = $authenticationMappingRepository->find($form->get('mapping')->getData()); |
331
|
|
|
$verificationSent = $registrationVerificationHelper->sendVerificationCode($modifiedMapping); |
332
|
|
|
if (!$verificationSent) { |
333
|
|
|
$this->addFlash('error', $this->trans('Sorry! There was a problem sending a verification code to %sub%.', ['%sub%' => $modifiedMapping->getUname()])); |
334
|
|
|
} else { |
335
|
|
|
$this->addFlash('status', $this->trans('Done! Verification code sent to %sub%.', ['%sub%' => $modifiedMapping->getUname()])); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
if ($form->get('cancel')->isClicked()) { |
339
|
|
|
$this->addFlash('status', 'Operation cancelled.'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return [ |
346
|
|
|
'form' => $form->createView(), |
347
|
|
|
'mapping' => $mapping |
348
|
|
|
]; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @Route("/send-confirmation/{mapping}", requirements={"mapping" = "^[1-9]\d*$"}) |
353
|
|
|
* |
354
|
|
|
* @throws AccessDeniedException Thrown if the user hasn't moderate permissions for the mapping record |
355
|
|
|
*/ |
356
|
|
|
public function sendConfirmation( |
357
|
|
|
AuthenticationMappingEntity $mapping, |
358
|
|
|
LostPasswordVerificationHelper $lostPasswordVerificationHelper, |
359
|
|
|
MailHelper $mailHelper |
360
|
|
|
): RedirectResponse { |
361
|
|
|
if (!$this->hasPermission('ZikulaZAuthModule', $mapping->getUname() . '::' . $mapping->getUid(), ACCESS_MODERATE)) { |
362
|
|
|
throw new AccessDeniedException(); |
363
|
|
|
} |
364
|
|
|
$changePasswordExpireDays = $this->getVar(ZAuthConstant::MODVAR_EXPIRE_DAYS_CHANGE_PASSWORD, ZAuthConstant::DEFAULT_EXPIRE_DAYS_CHANGE_PASSWORD); |
365
|
|
|
$lostPasswordId = $lostPasswordVerificationHelper->createLostPasswordId($mapping); |
366
|
|
|
$mailSent = $mailHelper->sendNotification($mapping->getEmail(), 'lostpassword', [ |
367
|
|
|
'uname' => $mapping->getUname(), |
368
|
|
|
'validDays' => $changePasswordExpireDays, |
369
|
|
|
'lostPasswordId' => $lostPasswordId, |
370
|
|
|
'requestedByAdmin' => true |
371
|
|
|
]); |
372
|
|
|
if ($mailSent) { |
373
|
|
|
$this->addFlash('status', $this->trans('Done! The password recovery verification link for %userName% has been sent via e-mail.', ['%userName%' => $mapping->getUname()])); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @Route("/send-username/{mapping}", requirements={"mapping" = "^[1-9]\d*$"}) |
381
|
|
|
* |
382
|
|
|
* @throws AccessDeniedException Thrown if the user hasn't moderate permissions for the mapping record |
383
|
|
|
*/ |
384
|
|
|
public function sendUserName( |
385
|
|
|
AuthenticationMappingEntity $mapping, |
386
|
|
|
MailHelper $mailHelper |
387
|
|
|
): RedirectResponse { |
388
|
|
|
if (!$this->hasPermission('ZikulaZAuthModule', $mapping->getUname() . '::' . $mapping->getUid(), ACCESS_MODERATE)) { |
389
|
|
|
throw new AccessDeniedException(); |
390
|
|
|
} |
391
|
|
|
$mailSent = $mailHelper->sendNotification($mapping->getEmail(), 'lostuname', [ |
392
|
|
|
'uname' => $mapping->getUname(), |
393
|
|
|
'requestedByAdmin' => true, |
394
|
|
|
]); |
395
|
|
|
|
396
|
|
|
if ($mailSent) { |
397
|
|
|
$this->addFlash('status', $this->trans('Done! The user name for %userName% has been sent via e-mail.', ['%userName%' => $mapping->getUname()])); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @Route("/toggle-password-change/{user}", requirements={"user" = "^[1-9]\d*$"}) |
405
|
|
|
* @Theme("admin") |
406
|
|
|
* @Template("@ZikulaZAuthModule/UserAdministration/togglePasswordChange.html.twig") |
407
|
|
|
* |
408
|
|
|
* @param UserEntity $user // note: this is intentionally left as UserEntity instead of mapping because of need to access attributes |
409
|
|
|
* |
410
|
|
|
* @return array|RedirectResponse |
411
|
|
|
* @throws AccessDeniedException Thrown if the user hasn't moderate permissions for the user record |
412
|
|
|
*/ |
413
|
|
|
public function togglePasswordChange(Request $request, UserEntity $user) |
414
|
|
|
{ |
415
|
|
|
if (!$this->hasPermission('ZikulaZAuthModule', $user->getUname() . '::' . $user->getUid(), ACCESS_MODERATE)) { |
416
|
|
|
throw new AccessDeniedException(); |
417
|
|
|
} |
418
|
|
|
if ($user->getAttributes()->containsKey(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY)) { |
419
|
|
|
$mustChangePass = $user->getAttributes()->get(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY); |
420
|
|
|
} else { |
421
|
|
|
$mustChangePass = false; |
422
|
|
|
} |
423
|
|
|
$form = $this->createForm(TogglePasswordConfirmationType::class, [ |
424
|
|
|
'uid' => $user->getUid() |
425
|
|
|
], [ |
426
|
|
|
'mustChangePass' => $mustChangePass |
427
|
|
|
]); |
428
|
|
|
$form->handleRequest($request); |
429
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
430
|
|
|
if ($form->get('toggle')->isClicked()) { |
431
|
|
|
if ($user->getAttributes()->containsKey(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY) && (bool)$user->getAttributes()->get(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY)) { |
432
|
|
|
$user->getAttributes()->remove(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY); |
433
|
|
|
$this->addFlash('success', $this->trans('Done! A password change will no longer be required for %userName%.', ['%userName%' => $user->getUname()])); |
434
|
|
|
} else { |
435
|
|
|
$user->setAttribute(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY, true); |
436
|
|
|
$this->addFlash('success', $this->trans('Done! A password change will be required the next time %userName% logs in.', ['%userName%' => $user->getUname()])); |
437
|
|
|
} |
438
|
|
|
$this->getDoctrine()->getManager()->flush(); |
439
|
|
|
} elseif ($form->get('cancel')->isClicked()) { |
440
|
|
|
$this->addFlash('info', 'Operation cancelled.'); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return [ |
447
|
|
|
'form' => $form->createView(), |
448
|
|
|
'mustChangePass' => $mustChangePass, |
449
|
|
|
'user' => $user |
450
|
|
|
]; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* @Route("/batch-force-password-change") |
455
|
|
|
* @PermissionCheck("admin") |
456
|
|
|
* @Theme("admin") |
457
|
|
|
* @Template("@ZikulaZAuthModule/UserAdministration/batchForcePasswordChange.html.twig") |
458
|
|
|
* |
459
|
|
|
* @return array|RedirectResponse |
460
|
|
|
*/ |
461
|
|
|
public function batchForcePasswordChange( |
462
|
|
|
BatchPasswordChangeHelper $batchPasswordChangeHelper, |
463
|
|
|
Request $request |
464
|
|
|
) { |
465
|
|
|
$form = $this->createForm(BatchForcePasswordChangeType::class); |
466
|
|
|
$form->handleRequest($request); |
467
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
468
|
|
|
if ($form->get('submit')->isClicked()) { |
469
|
|
|
$count = $batchPasswordChangeHelper->requirePasswordChangeByGroup($form->get('group')->getData()); |
470
|
|
|
$this->addFlash('success', $this->trans('Operation complete. %count% user(s) changed.', ['%count%' => $count])); |
471
|
|
|
} elseif ($form->get('cancel')->isClicked()) { |
472
|
|
|
$this->addFlash('info', 'Operation cancelled.'); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
return $this->redirectToRoute('zikulazauthmodule_useradministration_list'); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return [ |
479
|
|
|
'form' => $form->createView(), |
480
|
|
|
]; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|