Total Complexity | 59 |
Total Lines | 412 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like UserAdministrationController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserAdministrationController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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( |
||
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( |
||
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( |
||
480 | ]; |
||
481 | } |
||
482 | } |
||
483 |