Conditions | 34 |
Paths | 686 |
Total Lines | 161 |
Code Lines | 102 |
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 |
||
103 | #[Route('/change-password', name: 'chamilo_core_account_change_password', methods: ['GET', 'POST'])] |
||
104 | public function changePassword( |
||
105 | Request $request, |
||
106 | UserRepository $userRepository, |
||
107 | CsrfTokenManagerInterface $csrfTokenManager, |
||
108 | SettingsManager $settingsManager, |
||
109 | UserPasswordHasherInterface $passwordHasher, |
||
110 | TokenStorageInterface $tokenStorage, |
||
111 | ): Response { |
||
112 | /** @var ?User $user */ |
||
113 | $user = $this->getUser(); |
||
114 | |||
115 | if (!$user || !$user instanceof UserInterface) { |
||
116 | $userId = $request->query->get('userId'); |
||
117 | |||
118 | if (!$userId || !ctype_digit($userId)) { |
||
119 | throw $this->createAccessDeniedException('This user does not have access to this section.'); |
||
120 | } |
||
121 | |||
122 | $user = $userRepository->find((int) $userId); |
||
123 | |||
124 | if (!$user || !$user instanceof UserInterface) { |
||
125 | throw $this->createAccessDeniedException('User not found or invalid.'); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // Global 2FA toggle: read either "security.2fa_enable" or fallback "2fa_enable" |
||
130 | $twoFaEnabledGlobally = 'true' === $settingsManager->getSetting('security.2fa_enable', true); |
||
131 | |||
132 | // When rotating password (forced update), we also hide the 2FA widget |
||
133 | $isRotation = $request->query->getBoolean('rotate', false); |
||
134 | |||
135 | // Build the form; expose 2FA fields only if globally enabled and not rotating the password |
||
136 | $form = $this->createForm(ChangePasswordType::class, [ |
||
137 | 'enable2FA' => $user->getMfaEnabled(), |
||
138 | ], [ |
||
139 | 'user' => $user, |
||
140 | 'portal_name' => $settingsManager->getSetting('platform.institution'), |
||
141 | 'password_hasher' => $passwordHasher, |
||
142 | 'enable_2fa_field' => $twoFaEnabledGlobally && !$isRotation, |
||
143 | 'global_2fa_enabled' => $twoFaEnabledGlobally, |
||
144 | ]); |
||
145 | $form->handleRequest($request); |
||
146 | |||
147 | $session = $request->getSession(); |
||
148 | $qrCodeBase64 = null; |
||
149 | $showQRCode = false; |
||
150 | |||
151 | // Pre-generate QR preview only when 2FA is globally enabled and user opted-in but hasn't saved yet |
||
152 | if ( |
||
153 | $twoFaEnabledGlobally |
||
154 | && $form->isSubmitted() |
||
155 | && $form->has('enable2FA') |
||
156 | && $form->get('enable2FA')->getData() |
||
157 | && !$user->getMfaSecret() |
||
158 | ) { |
||
159 | if (!$session->has('temporary_mfa_secret')) { |
||
160 | $totp = TOTP::create(); |
||
161 | $secret = $totp->getSecret(); |
||
162 | $session->set('temporary_mfa_secret', $secret); |
||
163 | } else { |
||
164 | $secret = $session->get('temporary_mfa_secret'); |
||
165 | } |
||
166 | |||
167 | $totp = TOTP::create($secret); |
||
168 | $portalName = $settingsManager->getSetting('platform.institution'); |
||
169 | $totp->setLabel($portalName.' - '.$user->getEmail()); |
||
170 | |||
171 | $qrCodeResult = Builder::create() |
||
172 | ->writer(new PngWriter()) |
||
173 | ->data($totp->getProvisioningUri()) |
||
174 | ->encoding(new Encoding('UTF-8')) |
||
175 | ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||
176 | ->size(300) |
||
177 | ->margin(10) |
||
178 | ->build() |
||
179 | ; |
||
180 | |||
181 | $qrCodeBase64 = base64_encode($qrCodeResult->getString()); |
||
182 | $showQRCode = true; |
||
183 | } |
||
184 | |||
185 | if ($form->isSubmitted()) { |
||
186 | if ($form->isValid()) { |
||
187 | $submittedToken = $request->request->get('_token'); |
||
188 | if (!$csrfTokenManager->isTokenValid(new CsrfToken('change_password', $submittedToken))) { |
||
189 | $form->addError(new FormError($this->translator->trans('CSRF token is invalid. Please try again.'))); |
||
190 | } else { |
||
191 | $currentPassword = $form->get('currentPassword')->getData(); |
||
192 | $newPassword = $form->get('newPassword')->getData(); |
||
193 | $confirmPassword = $form->get('confirmPassword')->getData(); |
||
194 | |||
195 | // Only consider the user's 2FA intent if the global toggle is ON and not rotating |
||
196 | $enable2FA = $twoFaEnabledGlobally && !$isRotation && $form->has('enable2FA') |
||
197 | ? (bool) $form->get('enable2FA')->getData() |
||
198 | : false; |
||
199 | |||
200 | // Handle 2FA activation (only when globally enabled) |
||
201 | if ($twoFaEnabledGlobally && $enable2FA && !$user->getMfaSecret()) { |
||
202 | $secret = $session->get('temporary_mfa_secret'); |
||
203 | if ($secret) { |
||
204 | $encryptedSecret = $this->encryptTOTPSecret($secret, $_ENV['APP_SECRET']); |
||
205 | $user->setMfaSecret($encryptedSecret); |
||
206 | $user->setMfaEnabled(true); |
||
207 | $user->setMfaService('TOTP'); |
||
208 | $userRepository->updateUser($user); |
||
209 | $session->remove('temporary_mfa_secret'); |
||
210 | |||
211 | $this->addFlash('success', $this->translator->trans('2FA activated successfully.')); |
||
212 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | // Handle 2FA deactivation from the form (only visible if global is ON; safe to guard too) |
||
217 | if ($twoFaEnabledGlobally && !$isRotation && !$enable2FA && $user->getMfaEnabled()) { |
||
218 | $user->setMfaEnabled(false); |
||
219 | $user->setMfaSecret(null); |
||
220 | $userRepository->updateUser($user); |
||
221 | $this->addFlash('success', $this->translator->trans('2FA disabled successfully.')); |
||
222 | } |
||
223 | |||
224 | // Password change flow (unchanged) |
||
225 | if ($newPassword || $confirmPassword || $currentPassword) { |
||
226 | if (!$userRepository->isPasswordValid($user, $currentPassword)) { |
||
227 | $form->get('currentPassword')->addError(new FormError( |
||
228 | $this->translator->trans('The current password is incorrect') |
||
229 | )); |
||
230 | } elseif ($newPassword !== $confirmPassword) { |
||
231 | $form->get('confirmPassword')->addError(new FormError( |
||
232 | $this->translator->trans('Passwords do not match') |
||
233 | )); |
||
234 | } else { |
||
235 | $user->setPlainPassword($newPassword); |
||
236 | $user->setPasswordUpdatedAt(new DateTimeImmutable()); |
||
237 | $userRepository->updateUser($user); |
||
238 | $this->addFlash('success', $this->translator->trans('Password updated successfully.')); |
||
239 | |||
240 | // Re-login if the user was not logged in (edge case when rotating from link) |
||
241 | if (!$this->getUser()) { |
||
242 | $token = new UsernamePasswordToken($user, 'main', $user->getRoles()); |
||
243 | $tokenStorage->setToken($token); |
||
244 | $request->getSession()->set('_security_main', serialize($token)); |
||
245 | } |
||
246 | |||
247 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | } else { |
||
252 | error_log('Form is NOT valid.'); |
||
253 | } |
||
254 | } else { |
||
255 | error_log('Form NOT submitted yet.'); |
||
256 | } |
||
257 | |||
258 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
259 | 'form' => $form->createView(), |
||
260 | 'qrCode' => $qrCodeBase64, |
||
261 | 'user' => $user, |
||
262 | 'showQRCode' => $showQRCode, |
||
263 | 'password_requirements' => Security::getPasswordRequirements()['min'], |
||
264 | ]); |
||
317 |
This check looks for private methods that have been defined, but are not used inside the class.