| Conditions | 14 |
| Paths | 16 |
| Total Lines | 104 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 93 | #[Route('/change-password', name: 'chamilo_core_account_change_password', methods: ['GET', 'POST'])] |
||
| 94 | public function changePassword( |
||
| 95 | Request $request, |
||
| 96 | UserRepository $userRepository, |
||
| 97 | CsrfTokenManagerInterface $csrfTokenManager, |
||
| 98 | SettingsManager $settingsManager, |
||
| 99 | UserPasswordHasherInterface $passwordHasher |
||
| 100 | ): Response { |
||
| 101 | /** @var User $user */ |
||
| 102 | $user = $this->getUser(); |
||
| 103 | |||
| 104 | // Ensure user is authenticated and has proper interface |
||
| 105 | if (!\is_object($user) || !$user instanceof UserInterface) { |
||
| 106 | throw $this->createAccessDeniedException('This user does not have access to this section'); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Build the form and inject user-related options |
||
| 110 | $form = $this->createForm(ChangePasswordType::class, [ |
||
| 111 | 'enable2FA' => $user->getMfaEnabled(), |
||
| 112 | ], [ |
||
| 113 | 'user' => $user, |
||
| 114 | 'portal_name' => $settingsManager->getSetting('platform.institution'), |
||
| 115 | 'password_hasher' => $passwordHasher, |
||
| 116 | ]); |
||
| 117 | |||
| 118 | $form->handleRequest($request); |
||
| 119 | $session = $request->getSession(); |
||
| 120 | $qrCodeBase64 = null; |
||
| 121 | $showQRCode = false; |
||
| 122 | |||
| 123 | // Generate TOTP secret and QR code for 2FA activation |
||
| 124 | if ($form->get('enable2FA')->getData() && !$user->getMfaSecret()) { |
||
| 125 | if (!$session->has('temporary_mfa_secret')) { |
||
| 126 | $totp = TOTP::create(); |
||
| 127 | $secret = $totp->getSecret(); |
||
| 128 | $session->set('temporary_mfa_secret', $secret); |
||
| 129 | } else { |
||
| 130 | $secret = $session->get('temporary_mfa_secret'); |
||
| 131 | } |
||
| 132 | |||
| 133 | $totp = TOTP::create($secret); |
||
| 134 | $portalName = $settingsManager->getSetting('platform.institution'); |
||
| 135 | $totp->setLabel($portalName . ' - ' . $user->getEmail()); |
||
| 136 | |||
| 137 | // Build QR code image |
||
| 138 | $qrCodeResult = Builder::create() |
||
| 139 | ->writer(new PngWriter()) |
||
| 140 | ->data($totp->getProvisioningUri()) |
||
| 141 | ->encoding(new Encoding('UTF-8')) |
||
| 142 | ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||
| 143 | ->size(300) |
||
| 144 | ->margin(10) |
||
| 145 | ->build(); |
||
| 146 | |||
| 147 | $qrCodeBase64 = base64_encode($qrCodeResult->getString()); |
||
| 148 | $showQRCode = true; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Handle form submission |
||
| 152 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 153 | $newPassword = $form->get('newPassword')->getData(); |
||
| 154 | $enable2FA = $form->get('enable2FA')->getData(); |
||
| 155 | |||
| 156 | // Enable 2FA and store encrypted secret |
||
| 157 | if ($enable2FA && !$user->getMfaSecret() && $session->has('temporary_mfa_secret')) { |
||
| 158 | $secret = $session->get('temporary_mfa_secret'); |
||
| 159 | $encryptedSecret = $this->encryptTOTPSecret($secret, $_ENV['APP_SECRET']); |
||
| 160 | |||
| 161 | $user->setMfaSecret($encryptedSecret); |
||
| 162 | $user->setMfaEnabled(true); |
||
| 163 | $user->setMfaService('TOTP'); |
||
| 164 | |||
| 165 | $userRepository->updateUser($user); |
||
| 166 | $session->remove('temporary_mfa_secret'); |
||
| 167 | |||
| 168 | $this->addFlash('success', '2FA activated successfully.'); |
||
| 169 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Disable 2FA if it was previously enabled |
||
| 173 | if (!$enable2FA && $user->getMfaEnabled()) { |
||
| 174 | $user->setMfaEnabled(false); |
||
| 175 | $user->setMfaSecret(null); |
||
| 176 | |||
| 177 | $userRepository->updateUser($user); |
||
| 178 | $this->addFlash('success', '2FA disabled successfully.'); |
||
| 179 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Update password if provided |
||
| 183 | if (!empty($newPassword)) { |
||
| 184 | $user->setPlainPassword($newPassword); |
||
| 185 | $userRepository->updateUser($user); |
||
| 186 | $this->addFlash('success', 'Password updated successfully.'); |
||
| 187 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | // Render form with optional QR code for 2FA |
||
| 192 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
| 193 | 'form' => $form->createView(), |
||
| 194 | 'qrCode' => $qrCodeBase64, |
||
| 195 | 'user' => $user, |
||
| 196 | 'showQRCode' => $showQRCode, |
||
| 197 | ]); |
||
| 261 |
This check looks for private methods that have been defined, but are not used inside the class.