| Conditions | 26 |
| Paths | 154 |
| Total Lines | 143 |
| Code Lines | 98 |
| 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 |
||
| 97 | #[Route('/change-password', name: 'chamilo_core_account_change_password', methods: ['GET', 'POST'])] |
||
| 98 | public function changePassword( |
||
| 99 | Request $request, |
||
| 100 | UserRepository $userRepository, |
||
| 101 | CsrfTokenManagerInterface $csrfTokenManager, |
||
| 102 | TokenStorageInterface $tokenStorage, |
||
| 103 | ): Response { |
||
| 104 | /** @var ?User $user */ |
||
| 105 | $user = $this->getUser(); |
||
| 106 | |||
| 107 | if (!$user || !$user instanceof UserInterface) { |
||
| 108 | $userId = $request->query->get('userId'); |
||
| 109 | error_log("User not logged in. Received userId from query: " . $userId); |
||
| 110 | |||
| 111 | if (!$userId || !ctype_digit($userId)) { |
||
| 112 | error_log("Access denied: Missing or invalid userId."); |
||
| 113 | throw $this->createAccessDeniedException('This user does not have access to this section.'); |
||
| 114 | } |
||
| 115 | |||
| 116 | $user = $userRepository->find((int)$userId); |
||
| 117 | |||
| 118 | if (!$user || !$user instanceof UserInterface) { |
||
| 119 | error_log("Access denied: User not found with ID $userId"); |
||
| 120 | throw $this->createAccessDeniedException('User not found or invalid.'); |
||
| 121 | } |
||
| 122 | |||
| 123 | error_log("Loaded user by ID: " . $user->getId()); |
||
| 124 | } |
||
| 125 | |||
| 126 | $isRotation = $request->query->getBoolean('rotate', false); |
||
| 127 | |||
| 128 | $form = $this->createForm(ChangePasswordType::class, [ |
||
| 129 | 'enable2FA' => $user->getMfaEnabled(), |
||
| 130 | ], [ |
||
| 131 | 'enable_2fa_field' => !$isRotation, |
||
| 132 | ]); |
||
| 133 | $form->handleRequest($request); |
||
| 134 | |||
| 135 | $qrCodeBase64 = null; |
||
| 136 | if ($user->getMfaEnabled() && 'TOTP' === $user->getMfaService() && $user->getMfaSecret()) { |
||
| 137 | $decryptedSecret = $this->decryptTOTPSecret($user->getMfaSecret(), $_ENV['APP_SECRET']); |
||
| 138 | $totp = TOTP::create($decryptedSecret); |
||
| 139 | $totp->setLabel($user->getEmail()); |
||
| 140 | |||
| 141 | $qrCodeResult = Builder::create() |
||
| 142 | ->writer(new PngWriter()) |
||
| 143 | ->data($totp->getProvisioningUri()) |
||
| 144 | ->encoding(new Encoding('UTF-8')) |
||
| 145 | ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||
| 146 | ->size(300) |
||
| 147 | ->margin(10) |
||
| 148 | ->build(); |
||
| 149 | |||
| 150 | $qrCodeBase64 = base64_encode($qrCodeResult->getString()); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($form->isSubmitted()) { |
||
| 154 | if ($form->isValid()) { |
||
| 155 | $submittedToken = $request->request->get('_token'); |
||
| 156 | if (!$csrfTokenManager->isTokenValid(new CsrfToken('change_password', $submittedToken))) { |
||
| 157 | $form->addError(new FormError($this->translator->trans('CSRF token is invalid. Please try again.'))); |
||
| 158 | } else { |
||
| 159 | $currentPassword = $form->get('currentPassword')->getData(); |
||
| 160 | $newPassword = $form->get('newPassword')->getData(); |
||
| 161 | $confirmPassword = $form->get('confirmPassword')->getData(); |
||
| 162 | $enable2FA = !$isRotation && $form->has('enable2FA') |
||
| 163 | ? $form->get('enable2FA')->getData() |
||
| 164 | : false; |
||
| 165 | |||
| 166 | if ($enable2FA && !$user->getMfaSecret()) { |
||
| 167 | $totp = TOTP::create(); |
||
| 168 | $totp->setLabel($user->getEmail()); |
||
| 169 | $encryptedSecret = $this->encryptTOTPSecret($totp->getSecret(), $_ENV['APP_SECRET']); |
||
| 170 | $user->setMfaSecret($encryptedSecret); |
||
| 171 | $user->setMfaEnabled(true); |
||
| 172 | $user->setMfaService('TOTP'); |
||
| 173 | $userRepository->updateUser($user); |
||
| 174 | |||
| 175 | $qrCodeResult = Builder::create() |
||
| 176 | ->writer(new PngWriter()) |
||
| 177 | ->data($totp->getProvisioningUri()) |
||
| 178 | ->encoding(new Encoding('UTF-8')) |
||
| 179 | ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||
| 180 | ->size(300) |
||
| 181 | ->margin(10) |
||
| 182 | ->build(); |
||
| 183 | |||
| 184 | $qrCodeBase64 = base64_encode($qrCodeResult->getString()); |
||
| 185 | |||
| 186 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
| 187 | 'form' => $form->createView(), |
||
| 188 | 'qrCode' => $qrCodeBase64, |
||
| 189 | 'user' => $user, |
||
| 190 | ]); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!$isRotation && !$enable2FA && $user->getMfaEnabled()) { |
||
| 194 | $user->setMfaEnabled(false); |
||
| 195 | $user->setMfaSecret(null); |
||
| 196 | $userRepository->updateUser($user); |
||
| 197 | $this->addFlash('success', '2FA disabled successfully.'); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($newPassword || $confirmPassword || $currentPassword) { |
||
| 201 | if (!$userRepository->isPasswordValid($user, $currentPassword)) { |
||
| 202 | $form->get('currentPassword')->addError(new FormError( |
||
| 203 | $this->translator->trans('The current password is incorrect.') |
||
| 204 | )); |
||
| 205 | } elseif ($newPassword !== $confirmPassword) { |
||
| 206 | $form->get('confirmPassword')->addError(new FormError( |
||
| 207 | $this->translator->trans('Passwords do not match.') |
||
| 208 | )); |
||
| 209 | } else { |
||
| 210 | $user->setPlainPassword($newPassword); |
||
| 211 | $user->setPasswordUpdateAt(new \DateTimeImmutable()); |
||
| 212 | $userRepository->updateUser($user); |
||
| 213 | $this->addFlash('success', 'Password updated successfully'); |
||
| 214 | |||
| 215 | if (!$this->getUser()) { |
||
| 216 | $token = new UsernamePasswordToken( |
||
| 217 | $user, |
||
| 218 | 'main', |
||
| 219 | $user->getRoles() |
||
| 220 | ); |
||
| 221 | $tokenStorage->setToken($token); |
||
| 222 | $request->getSession()->set('_security_main', serialize($token)); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } else { |
||
| 230 | error_log("Form is NOT valid."); |
||
| 231 | } |
||
| 232 | } else { |
||
| 233 | error_log("Form NOT submitted yet."); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
| 237 | 'form' => $form->createView(), |
||
| 238 | 'qrCode' => $qrCodeBase64, |
||
| 239 | 'user' => $user, |
||
| 240 | ]); |
||
| 293 |
This check looks for private methods that have been defined, but are not used inside the class.