| Conditions | 18 |
| Paths | 49 |
| Total Lines | 163 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 202 | public function userSettings(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, GoogleAuthenticator $googleAuthenticator, BackupCodeManager $backupCodeManager) |
||
| 203 | { |
||
| 204 | /** @var User */ |
||
| 205 | $user = $this->getUser(); |
||
| 206 | |||
| 207 | $page_need_reload = false; |
||
| 208 | |||
| 209 | //When user change its settings, he should be logged in fully. |
||
| 210 | $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
||
| 211 | |||
| 212 | if (! $user instanceof User) { |
||
| 213 | throw new RuntimeException('This controller only works only for Part-DB User objects!'); |
||
| 214 | } |
||
| 215 | |||
| 216 | $security_event = new SecurityEvent($user); |
||
| 217 | |||
| 218 | /*************************** |
||
| 219 | * User settings form |
||
| 220 | ***************************/ |
||
| 221 | |||
| 222 | $form = $this->createForm(UserSettingsType::class, $user); |
||
| 223 | |||
| 224 | $form->handleRequest($request); |
||
| 225 | |||
| 226 | if (! $this->demo_mode && $form->isSubmitted() && $form->isValid()) { |
||
| 227 | //Check if user theme setting has changed |
||
| 228 | if ($user->getTheme() !== $em->getUnitOfWork()->getOriginalEntityData($user)['theme']) { |
||
| 229 | $page_need_reload = true; |
||
| 230 | } |
||
| 231 | |||
| 232 | $em->flush(); |
||
| 233 | $this->addFlash('success', 'user.settings.saved_flash'); |
||
| 234 | } |
||
| 235 | |||
| 236 | /***************************** |
||
| 237 | * Password change form |
||
| 238 | ****************************/ |
||
| 239 | |||
| 240 | $pw_form = $this->createFormBuilder() |
||
| 241 | //Username field for autocomplete |
||
| 242 | ->add('username', TextType::class, [ |
||
| 243 | 'data' => $user->getName(), |
||
| 244 | 'attr' => [ |
||
| 245 | 'autocomplete' => 'username', |
||
| 246 | ], |
||
| 247 | 'disabled' => true, |
||
| 248 | 'row_attr' => [ |
||
| 249 | 'class' => 'd-none', |
||
| 250 | ], |
||
| 251 | ]) |
||
| 252 | ->add('old_password', PasswordType::class, [ |
||
| 253 | 'label' => 'user.settings.pw_old.label', |
||
| 254 | 'disabled' => $this->demo_mode, |
||
| 255 | 'attr' => [ |
||
| 256 | 'autocomplete' => 'current-password', |
||
| 257 | ], |
||
| 258 | 'constraints' => [new UserPassword()], |
||
| 259 | ]) //This constraint checks, if the current user pw was inputted. |
||
| 260 | ->add('new_password', RepeatedType::class, [ |
||
| 261 | 'disabled' => $this->demo_mode, |
||
| 262 | 'type' => PasswordType::class, |
||
| 263 | 'first_options' => [ |
||
| 264 | 'label' => 'user.settings.pw_new.label', |
||
| 265 | ], |
||
| 266 | 'second_options' => [ |
||
| 267 | 'label' => 'user.settings.pw_confirm.label', |
||
| 268 | ], |
||
| 269 | 'invalid_message' => 'password_must_match', |
||
| 270 | 'options' => [ |
||
| 271 | 'attr' => [ |
||
| 272 | 'autocomplete' => 'new-password', |
||
| 273 | ], |
||
| 274 | ], |
||
| 275 | 'constraints' => [new Length([ |
||
| 276 | 'min' => 6, |
||
| 277 | 'max' => 128, |
||
| 278 | ])], |
||
| 279 | ]) |
||
| 280 | ->add('submit', SubmitType::class, ['label' => 'save']) |
||
| 281 | ->getForm(); |
||
| 282 | |||
| 283 | $pw_form->handleRequest($request); |
||
| 284 | |||
| 285 | //Check if password if everything was correct, then save it to User and DB |
||
| 286 | if (! $this->demo_mode && $pw_form->isSubmitted() && $pw_form->isValid()) { |
||
| 287 | $password = $passwordEncoder->encodePassword($user, $pw_form['new_password']->getData()); |
||
| 288 | $user->setPassword($password); |
||
| 289 | |||
| 290 | //After the change reset the password change needed setting |
||
| 291 | $user->setNeedPwChange(false); |
||
| 292 | |||
| 293 | $em->persist($user); |
||
| 294 | $em->flush(); |
||
| 295 | $this->addFlash('success', 'user.settings.pw_changed_flash'); |
||
| 296 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::PASSWORD_CHANGED); |
||
| 297 | } |
||
| 298 | |||
| 299 | //Handle 2FA things |
||
| 300 | $google_form = $this->createForm(TFAGoogleSettingsType::class, $user); |
||
| 301 | $google_enabled = $user->isGoogleAuthenticatorEnabled(); |
||
| 302 | if (! $google_enabled && ! $form->isSubmitted()) { |
||
| 303 | $user->setGoogleAuthenticatorSecret($googleAuthenticator->generateSecret()); |
||
| 304 | $google_form->get('googleAuthenticatorSecret')->setData($user->getGoogleAuthenticatorSecret()); |
||
| 305 | } |
||
| 306 | $google_form->handleRequest($request); |
||
| 307 | |||
| 308 | if (! $this->demo_mode && $google_form->isSubmitted() && $google_form->isValid()) { |
||
| 309 | if (! $google_enabled) { |
||
| 310 | //Save 2FA settings (save secrets) |
||
| 311 | $user->setGoogleAuthenticatorSecret($google_form->get('googleAuthenticatorSecret')->getData()); |
||
| 312 | $backupCodeManager->enableBackupCodes($user); |
||
| 313 | |||
| 314 | $em->flush(); |
||
| 315 | $this->addFlash('success', 'user.settings.2fa.google.activated'); |
||
| 316 | |||
| 317 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::GOOGLE_ENABLED); |
||
| 318 | |||
| 319 | return $this->redirectToRoute('user_settings'); |
||
| 320 | } |
||
| 321 | |||
| 322 | //Remove secret to disable google authenticator |
||
| 323 | $user->setGoogleAuthenticatorSecret(null); |
||
| 324 | $backupCodeManager->disableBackupCodesIfUnused($user); |
||
| 325 | $em->flush(); |
||
| 326 | $this->addFlash('success', 'user.settings.2fa.google.disabled'); |
||
| 327 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::GOOGLE_DISABLED); |
||
| 328 | |||
| 329 | return $this->redirectToRoute('user_settings'); |
||
| 330 | } |
||
| 331 | |||
| 332 | $backup_form = $this->get('form.factory')->createNamedBuilder('backup_codes')->add('reset_codes', SubmitType::class, [ |
||
| 333 | 'label' => 'tfa_backup.regenerate_codes', |
||
| 334 | 'attr' => [ |
||
| 335 | 'class' => 'btn-danger', |
||
| 336 | ], |
||
| 337 | 'disabled' => empty($user->getBackupCodes()), |
||
| 338 | ])->getForm(); |
||
| 339 | |||
| 340 | $backup_form->handleRequest($request); |
||
| 341 | if (! $this->demo_mode && $backup_form->isSubmitted() && $backup_form->isValid()) { |
||
| 342 | $backupCodeManager->regenerateBackupCodes($user); |
||
| 343 | $em->flush(); |
||
| 344 | $this->addFlash('success', 'user.settings.2fa.backup_codes.regenerated'); |
||
| 345 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::BACKUP_KEYS_RESET); |
||
| 346 | } |
||
| 347 | |||
| 348 | /****************************** |
||
| 349 | * Output both forms |
||
| 350 | *****************************/ |
||
| 351 | |||
| 352 | return $this->render('Users/user_settings.html.twig', [ |
||
| 353 | 'user' => $user, |
||
| 354 | 'settings_form' => $form->createView(), |
||
| 355 | 'pw_form' => $pw_form->createView(), |
||
| 356 | 'page_need_reload' => $page_need_reload, |
||
| 357 | |||
| 358 | 'google_form' => $google_form->createView(), |
||
| 359 | 'backup_form' => $backup_form->createView(), |
||
| 360 | 'tfa_google' => [ |
||
| 361 | 'enabled' => $google_enabled, |
||
| 362 | 'qrContent' => $googleAuthenticator->getQRContent($user), |
||
| 363 | 'secret' => $user->getGoogleAuthenticatorSecret(), |
||
| 364 | 'username' => $user->getGoogleAuthenticatorUsername(), |
||
| 365 | ], |
||
| 369 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.