| Conditions | 19 |
| Paths | 73 |
| Total Lines | 141 |
| Code Lines | 85 |
| 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 |
||
| 164 | public function userSettings(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, GoogleAuthenticator $googleAuthenticator, BackupCodeManager $backupCodeManager) |
||
| 165 | { |
||
| 166 | /** |
||
| 167 | * @var User |
||
| 168 | */ |
||
| 169 | $user = $this->getUser(); |
||
| 170 | |||
| 171 | $page_need_reload = false; |
||
| 172 | |||
| 173 | //When user change its settings, he should be logged in fully. |
||
| 174 | $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
||
| 175 | |||
| 176 | if (!$user instanceof User) { |
||
| 177 | throw new \RuntimeException('This controller only works only for Part-DB User objects!'); |
||
| 178 | } |
||
| 179 | |||
| 180 | /*************************** |
||
| 181 | * User settings form |
||
| 182 | ***************************/ |
||
| 183 | |||
| 184 | $form = $this->createForm(UserSettingsType::class, $user); |
||
| 185 | |||
| 186 | $form->handleRequest($request); |
||
| 187 | |||
| 188 | if ($form->isSubmitted() && $form->isValid() && !$this->demo_mode) { |
||
| 189 | //Check if user theme setting has changed |
||
| 190 | if ($user->getTheme() !== $em->getUnitOfWork()->getOriginalEntityData($user)['theme']) { |
||
| 191 | $page_need_reload = true; |
||
| 192 | } |
||
| 193 | |||
| 194 | $em->flush(); |
||
| 195 | $this->addFlash('success', 'user.settings.saved_flash'); |
||
| 196 | } |
||
| 197 | |||
| 198 | /***************************** |
||
| 199 | * Password change form |
||
| 200 | ****************************/ |
||
| 201 | |||
| 202 | $pw_form = $this->createFormBuilder() |
||
| 203 | //Username field for autocomplete |
||
| 204 | ->add('username', TextType::class, [ |
||
| 205 | 'data' => $user->getName(), |
||
| 206 | 'attr' => ['autocomplete' => 'username'], |
||
| 207 | 'disabled' => true, |
||
| 208 | 'row_attr' => ['class' => 'd-none'] |
||
| 209 | ]) |
||
| 210 | ->add('old_password', PasswordType::class, [ |
||
| 211 | 'label' => 'user.settings.pw_old.label', |
||
| 212 | 'disabled' => $this->demo_mode, |
||
| 213 | 'attr' => ['autocomplete' => 'current-password'], |
||
| 214 | 'constraints' => [new UserPassword()], ]) //This constraint checks, if the current user pw was inputted. |
||
| 215 | ->add('new_password', RepeatedType::class, [ |
||
| 216 | 'disabled' => $this->demo_mode, |
||
| 217 | 'type' => PasswordType::class, |
||
| 218 | 'first_options' => ['label' => 'user.settings.pw_new.label'], |
||
| 219 | 'second_options' => ['label' => 'user.settings.pw_confirm.label'], |
||
| 220 | 'invalid_message' => 'password_must_match', |
||
| 221 | 'options' => [ |
||
| 222 | 'attr' => ['autocomplete' => 'new-password'] |
||
| 223 | ], |
||
| 224 | 'constraints' => [new Length([ |
||
| 225 | 'min' => 6, |
||
| 226 | 'max' => 128, |
||
| 227 | ])], |
||
| 228 | ]) |
||
| 229 | ->add('submit', SubmitType::class, ['label' => 'save']) |
||
| 230 | ->getForm(); |
||
| 231 | |||
| 232 | $pw_form->handleRequest($request); |
||
| 233 | |||
| 234 | //Check if password if everything was correct, then save it to User and DB |
||
| 235 | if ($pw_form->isSubmitted() && $pw_form->isValid() && !$this->demo_mode) { |
||
| 236 | $password = $passwordEncoder->encodePassword($user, $pw_form['new_password']->getData()); |
||
| 237 | $user->setPassword($password); |
||
| 238 | |||
| 239 | //After the change reset the password change needed setting |
||
| 240 | $user->setNeedPwChange(false); |
||
| 241 | |||
| 242 | $em->persist($user); |
||
| 243 | $em->flush(); |
||
| 244 | $this->addFlash('success', 'user.settings.pw_changed_flash'); |
||
| 245 | } |
||
| 246 | |||
| 247 | //Handle 2FA things |
||
| 248 | $google_form = $this->createForm(TFAGoogleSettingsType::class, $user); |
||
| 249 | $google_enabled = $user->isGoogleAuthenticatorEnabled(); |
||
| 250 | if (!$form->isSubmitted() && !$google_enabled) { |
||
| 251 | $user->setGoogleAuthenticatorSecret($googleAuthenticator->generateSecret()); |
||
| 252 | $google_form->get('googleAuthenticatorSecret')->setData($user->getGoogleAuthenticatorSecret()); |
||
| 253 | } |
||
| 254 | $google_form->handleRequest($request); |
||
| 255 | |||
| 256 | if($google_form->isSubmitted() && $google_form->isValid() && !$this->demo_mode) { |
||
| 257 | if (!$google_enabled) { |
||
| 258 | //Save 2FA settings (save secrets) |
||
| 259 | $user->setGoogleAuthenticatorSecret($google_form->get('googleAuthenticatorSecret')->getData()); |
||
| 260 | $backupCodeManager->enableBackupCodes($user); |
||
| 261 | $em->flush(); |
||
| 262 | $this->addFlash('success', 'user.settings.2fa.google.activated'); |
||
| 263 | return $this->redirectToRoute('user_settings'); |
||
| 264 | } elseif ($google_enabled) { |
||
|
|
|||
| 265 | //Remove secret to disable google authenticator |
||
| 266 | $user->setGoogleAuthenticatorSecret(null); |
||
| 267 | $backupCodeManager->disableBackupCodesIfUnused($user); |
||
| 268 | $em->flush(); |
||
| 269 | $this->addFlash('success', 'user.settings.2fa.google.disabled'); |
||
| 270 | return $this->redirectToRoute('user_settings'); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | $backup_form = $this->get('form.factory')->createNamedBuilder('backup_codes')->add('reset_codes', SubmitType::class,[ |
||
| 275 | 'label' => 'tfa_backup.regenerate_codes', |
||
| 276 | 'attr' => ['class' => 'btn-danger'], |
||
| 277 | 'disabled' => empty($user->getBackupCodes()) |
||
| 278 | ])->getForm(); |
||
| 279 | |||
| 280 | $backup_form->handleRequest($request); |
||
| 281 | if ($backup_form->isSubmitted() && $backup_form->isValid() && !$this->demo_mode) { |
||
| 282 | $backupCodeManager->regenerateBackupCodes($user); |
||
| 283 | $em->flush(); |
||
| 284 | $this->addFlash('success', 'user.settings.2fa.backup_codes.regenerated'); |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /****************************** |
||
| 289 | * Output both forms |
||
| 290 | *****************************/ |
||
| 291 | |||
| 292 | return $this->render('Users/user_settings.html.twig', [ |
||
| 293 | 'user' => $user, |
||
| 294 | 'settings_form' => $form->createView(), |
||
| 295 | 'pw_form' => $pw_form->createView(), |
||
| 296 | 'page_need_reload' => $page_need_reload, |
||
| 297 | |||
| 298 | 'google_form' => $google_form->createView(), |
||
| 299 | 'backup_form' => $backup_form->createView(), |
||
| 300 | 'tfa_google' => [ |
||
| 301 | 'enabled' => $google_enabled, |
||
| 302 | 'qrContent' => $googleAuthenticator->getQRContent($user), |
||
| 303 | 'secret' => $user->getGoogleAuthenticatorSecret(), |
||
| 304 | 'username' => $user->getGoogleAuthenticatorUsername() |
||
| 305 | ] |
||
| 308 | } |