Conditions | 22 |
Paths | 209 |
Total Lines | 182 |
Code Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
214 | public function userSettings(Request $request, EntityManagerInterface $em, UserPasswordHasherInterface $passwordEncoder, GoogleAuthenticator $googleAuthenticator, BackupCodeManager $backupCodeManager, FormFactoryInterface $formFactory, UserAvatarHelper $avatarHelper) |
||
215 | { |
||
216 | /** @var User */ |
||
217 | $user = $this->getUser(); |
||
218 | |||
219 | $page_need_reload = false; |
||
220 | |||
221 | //When user change its settings, he should be logged in fully. |
||
222 | $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
||
223 | |||
224 | if (!$user instanceof User) { |
||
225 | throw new RuntimeException('This controller only works only for Part-DB User objects!'); |
||
226 | } |
||
227 | |||
228 | $security_event = new SecurityEvent($user); |
||
229 | |||
230 | /*************************** |
||
231 | * User settings form |
||
232 | ***************************/ |
||
233 | |||
234 | $form = $this->createForm(UserSettingsType::class, $user); |
||
235 | |||
236 | $form->handleRequest($request); |
||
237 | |||
238 | if (!$this->demo_mode && $form->isSubmitted() && $form->isValid()) { |
||
239 | //Check if user theme setting has changed |
||
240 | if ($user->getTheme() !== $em->getUnitOfWork()->getOriginalEntityData($user)['theme']) { |
||
241 | $page_need_reload = true; |
||
242 | } |
||
243 | |||
244 | if ($form['avatar_file']->getData() !== null) { |
||
245 | $attachment = $avatarHelper->handleAvatarUpload($user, $form['avatar_file']->getData()); |
||
246 | //$em->flush(); |
||
247 | //For some reason the avatar is not set as master picture attachment, so we do it again here |
||
248 | $user->setMasterPictureAttachment($attachment); |
||
249 | $page_need_reload = true; |
||
250 | } |
||
251 | |||
252 | /** @var Form $form We need an form implementation for the next calls */ |
||
253 | if ($form->getClickedButton() && 'remove_avatar' === $form->getClickedButton()->getName()) { |
||
254 | //Remove the avatar attachment from the user if requested |
||
255 | if ($user->getMasterPictureAttachment() !== null) { |
||
256 | $em->remove($user->getMasterPictureAttachment()); |
||
257 | $user->setMasterPictureAttachment(null); |
||
258 | $page_need_reload = true; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | $em->flush(); |
||
263 | $this->addFlash('success', 'user.settings.saved_flash'); |
||
264 | } |
||
265 | |||
266 | /***************************** |
||
267 | * Password change form |
||
268 | ****************************/ |
||
269 | |||
270 | $pw_form = $this->createFormBuilder() |
||
271 | //Username field for autocomplete |
||
272 | ->add('username', TextType::class, [ |
||
273 | 'data' => $user->getName(), |
||
274 | 'attr' => [ |
||
275 | 'autocomplete' => 'username', |
||
276 | ], |
||
277 | 'disabled' => true, |
||
278 | 'row_attr' => [ |
||
279 | 'class' => 'd-none', |
||
280 | ], |
||
281 | ]) |
||
282 | ->add('old_password', PasswordType::class, [ |
||
283 | 'label' => 'user.settings.pw_old.label', |
||
284 | 'disabled' => $this->demo_mode, |
||
285 | 'attr' => [ |
||
286 | 'autocomplete' => 'current-password', |
||
287 | ], |
||
288 | 'constraints' => [new UserPassword()], |
||
289 | ]) //This constraint checks, if the current user pw was inputted. |
||
290 | ->add('new_password', RepeatedType::class, [ |
||
291 | 'disabled' => $this->demo_mode, |
||
292 | 'type' => PasswordType::class, |
||
293 | 'first_options' => [ |
||
294 | 'label' => 'user.settings.pw_new.label', |
||
295 | ], |
||
296 | 'second_options' => [ |
||
297 | 'label' => 'user.settings.pw_confirm.label', |
||
298 | ], |
||
299 | 'invalid_message' => 'password_must_match', |
||
300 | 'options' => [ |
||
301 | 'attr' => [ |
||
302 | 'autocomplete' => 'new-password', |
||
303 | ], |
||
304 | ], |
||
305 | 'constraints' => [new Length([ |
||
306 | 'min' => 6, |
||
307 | 'max' => 128, |
||
308 | ])], |
||
309 | ]) |
||
310 | ->add('submit', SubmitType::class, ['label' => 'save']) |
||
311 | ->getForm(); |
||
312 | |||
313 | $pw_form->handleRequest($request); |
||
314 | |||
315 | //Check if password if everything was correct, then save it to User and DB |
||
316 | if (!$this->demo_mode && $pw_form->isSubmitted() && $pw_form->isValid()) { |
||
317 | $password = $passwordEncoder->hashPassword($user, $pw_form['new_password']->getData()); |
||
318 | $user->setPassword($password); |
||
319 | |||
320 | //After the change reset the password change needed setting |
||
321 | $user->setNeedPwChange(false); |
||
322 | |||
323 | $em->persist($user); |
||
324 | $em->flush(); |
||
325 | $this->addFlash('success', 'user.settings.pw_changed_flash'); |
||
326 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::PASSWORD_CHANGED); |
||
327 | } |
||
328 | |||
329 | //Handle 2FA things |
||
330 | $google_form = $this->createForm(TFAGoogleSettingsType::class, $user); |
||
331 | $google_enabled = $user->isGoogleAuthenticatorEnabled(); |
||
332 | if (!$google_enabled && !$form->isSubmitted()) { |
||
333 | $user->setGoogleAuthenticatorSecret($googleAuthenticator->generateSecret()); |
||
334 | $google_form->get('googleAuthenticatorSecret')->setData($user->getGoogleAuthenticatorSecret()); |
||
335 | } |
||
336 | $google_form->handleRequest($request); |
||
337 | |||
338 | if (!$this->demo_mode && $google_form->isSubmitted() && $google_form->isValid()) { |
||
339 | if (!$google_enabled) { |
||
340 | //Save 2FA settings (save secrets) |
||
341 | $user->setGoogleAuthenticatorSecret($google_form->get('googleAuthenticatorSecret')->getData()); |
||
342 | $backupCodeManager->enableBackupCodes($user); |
||
343 | |||
344 | $em->flush(); |
||
345 | $this->addFlash('success', 'user.settings.2fa.google.activated'); |
||
346 | |||
347 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::GOOGLE_ENABLED); |
||
348 | |||
349 | return $this->redirectToRoute('user_settings'); |
||
350 | } |
||
351 | |||
352 | //Remove secret to disable google authenticator |
||
353 | $user->setGoogleAuthenticatorSecret(null); |
||
354 | $backupCodeManager->disableBackupCodesIfUnused($user); |
||
355 | $em->flush(); |
||
356 | $this->addFlash('success', 'user.settings.2fa.google.disabled'); |
||
357 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::GOOGLE_DISABLED); |
||
358 | |||
359 | return $this->redirectToRoute('user_settings'); |
||
360 | } |
||
361 | |||
362 | |||
363 | $backup_form = $formFactory->createNamedBuilder('backup_codes')->add('reset_codes', SubmitType::class, [ |
||
364 | 'label' => 'tfa_backup.regenerate_codes', |
||
365 | 'attr' => [ |
||
366 | 'class' => 'btn-danger', |
||
367 | ], |
||
368 | 'disabled' => empty($user->getBackupCodes()), |
||
369 | ])->getForm(); |
||
370 | |||
371 | $backup_form->handleRequest($request); |
||
372 | if (!$this->demo_mode && $backup_form->isSubmitted() && $backup_form->isValid()) { |
||
373 | $backupCodeManager->regenerateBackupCodes($user); |
||
374 | $em->flush(); |
||
375 | $this->addFlash('success', 'user.settings.2fa.backup_codes.regenerated'); |
||
376 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::BACKUP_KEYS_RESET); |
||
377 | } |
||
378 | |||
379 | /****************************** |
||
380 | * Output both forms |
||
381 | *****************************/ |
||
382 | |||
383 | return $this->renderForm('users/user_settings.html.twig', [ |
||
384 | 'user' => $user, |
||
385 | 'settings_form' => $form, |
||
386 | 'pw_form' => $pw_form, |
||
387 | 'global_reload_needed' => $page_need_reload, |
||
388 | |||
389 | 'google_form' => $google_form, |
||
390 | 'backup_form' => $backup_form, |
||
391 | 'tfa_google' => [ |
||
392 | 'enabled' => $google_enabled, |
||
393 | 'qrContent' => $googleAuthenticator->getQRContent($user), |
||
394 | 'secret' => $user->getGoogleAuthenticatorSecret(), |
||
395 | 'username' => $user->getGoogleAuthenticatorUsername(), |
||
396 | ], |
||
400 |