forked from chamilo/chamilo-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountController.php
More file actions
320 lines (274 loc) · 13.9 KB
/
Copy pathAccountController.php
File metadata and controls
320 lines (274 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Controller;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Form\ChangePasswordType;
use Chamilo\CoreBundle\Form\ProfileType;
use Chamilo\CoreBundle\Helpers\UserHelper;
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CoreBundle\Settings\SettingsManager;
use Chamilo\CoreBundle\Traits\ControllerTrait;
use DateTimeImmutable;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Writer\PngWriter;
use OTPHP\TOTP;
use Security;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Julio Montoya <gugli100@gmail.com>
*/
#[Route('/account')]
class AccountController extends BaseController
{
use ControllerTrait;
public function __construct(
private readonly UserHelper $userHelper,
private readonly TranslatorInterface $translator
) {}
#[IsGranted('ROLE_USER')]
#[Route('/edit', name: 'chamilo_core_account_edit', methods: ['GET', 'POST'])]
public function edit(
Request $request,
UserRepository $userRepository,
IllustrationRepository $illustrationRepo,
SettingsManager $settingsManager
): Response {
$user = $this->userHelper->getCurrent();
/** @var User $user */
$form = $this->createForm(ProfileType::class, $user);
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->has('illustration')) {
$illustration = $form['illustration']->getData();
if ($illustration) {
$illustrationRepo->deleteIllustration($user);
$illustrationRepo->addIllustration($user, $user, $illustration);
}
}
if ($form->has('password')) {
$password = $form['password']->getData();
if ($password) {
$user->setPlainPassword($password);
$user->setPasswordUpdatedAt(new DateTimeImmutable());
}
}
$showTermsIfProfileCompleted = ('true' === $settingsManager->getSetting('profile.show_terms_if_profile_completed'));
$user->setProfileCompleted($showTermsIfProfileCompleted);
$userRepository->updateUser($user);
$this->addFlash('success', $this->trans('Updated'));
$url = $this->generateUrl('chamilo_core_account_home');
$request->getSession()->set('_locale_user', $user->getLocale());
return new RedirectResponse($url);
}
// Legacy Chamilo CSRF token (sec_token) used by old endpoints (extra fields/tags, etc).
// This prevents "CSRF error" warnings from legacy flows while the Symfony form still saves correctly.
$legacyToken = Security::get_token();
return $this->render('@ChamiloCore/Account/edit.html.twig', [
'form' => $form->createView(),
'user' => $user,
'legacy_token' => $legacyToken,
]);
}
#[Route('/change-password', name: 'chamilo_core_account_change_password', methods: ['GET', 'POST'])]
public function changePassword(
Request $request,
UserRepository $userRepository,
CsrfTokenManagerInterface $csrfTokenManager,
SettingsManager $settingsManager,
UserPasswordHasherInterface $passwordHasher,
TokenStorageInterface $tokenStorage,
): Response {
/** @var ?User $user */
$user = $this->getUser();
if (!$user || !$user instanceof UserInterface) {
$userId = $request->query->get('userId');
if (!$userId || !ctype_digit($userId)) {
throw $this->createAccessDeniedException('This user does not have access to this section.');
}
$user = $userRepository->find((int) $userId);
if (!$user || !$user instanceof UserInterface) {
throw $this->createAccessDeniedException('User not found or invalid.');
}
}
// Global 2FA toggle: read either "security.2fa_enable" or fallback "2fa_enable"
$twoFaEnabledGlobally = 'true' === $settingsManager->getSetting('security.2fa_enable', true);
// When rotating password (forced update), we also hide the 2FA widget
$isRotation = $request->query->getBoolean('rotate', false);
// Build the form; expose 2FA fields only if globally enabled and not rotating the password
$form = $this->createForm(ChangePasswordType::class, [
'enable2FA' => $user->getMfaEnabled(),
], [
'user' => $user,
'portal_name' => $settingsManager->getSetting('platform.institution'),
'password_hasher' => $passwordHasher,
'enable_2fa_field' => $twoFaEnabledGlobally && !$isRotation,
'global_2fa_enabled' => $twoFaEnabledGlobally,
]);
$form->handleRequest($request);
$session = $request->getSession();
$qrCodeBase64 = null;
$showQRCode = false;
// Pre-generate QR preview only when 2FA is globally enabled and user opted-in but hasn't saved yet
if (
$twoFaEnabledGlobally
&& $form->isSubmitted()
&& $form->has('enable2FA')
&& $form->get('enable2FA')->getData()
&& !$user->getMfaSecret()
) {
if (!$session->has('temporary_mfa_secret')) {
$totp = TOTP::create();
$secret = $totp->getSecret();
$session->set('temporary_mfa_secret', $secret);
} else {
$secret = $session->get('temporary_mfa_secret');
}
$totp = TOTP::create($secret);
$portalName = $settingsManager->getSetting('platform.institution');
$totp->setLabel($portalName.' - '.$user->getEmail());
$qrCodeResult = Builder::create()
->writer(new PngWriter())
->data($totp->getProvisioningUri())
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
->size(300)
->margin(10)
->build()
;
$qrCodeBase64 = base64_encode($qrCodeResult->getString());
$showQRCode = true;
}
if ($form->isSubmitted()) {
if ($form->isValid()) {
$submittedToken = $request->request->get('_token');
if (!$csrfTokenManager->isTokenValid(new CsrfToken('change_password', $submittedToken))) {
$form->addError(new FormError($this->translator->trans('CSRF token is invalid. Please try again.')));
} else {
$currentPassword = $form->get('currentPassword')->getData();
$newPassword = $form->get('newPassword')->getData();
$confirmPassword = $form->get('confirmPassword')->getData();
// Only consider the user's 2FA intent if the global toggle is ON and not rotating
$enable2FA = $twoFaEnabledGlobally && !$isRotation && $form->has('enable2FA')
? (bool) $form->get('enable2FA')->getData()
: false;
// Handle 2FA activation (only when globally enabled)
if ($twoFaEnabledGlobally && $enable2FA && !$user->getMfaSecret()) {
$secret = $session->get('temporary_mfa_secret');
if ($secret) {
$encryptedSecret = $this->encryptTOTPSecret($secret, $_ENV['APP_SECRET']);
$user->setMfaSecret($encryptedSecret);
$user->setMfaEnabled(true);
$user->setMfaService('TOTP');
$userRepository->updateUser($user);
$session->remove('temporary_mfa_secret');
$this->addFlash('success', $this->translator->trans('2FA activated successfully.'));
return $this->redirectToRoute('chamilo_core_account_home');
}
}
// Handle 2FA deactivation from the form (only visible if global is ON; safe to guard too)
if ($twoFaEnabledGlobally && !$isRotation && !$enable2FA && $user->getMfaEnabled()) {
$user->setMfaEnabled(false);
$user->setMfaSecret(null);
$userRepository->updateUser($user);
$this->addFlash('success', $this->translator->trans('2FA disabled successfully.'));
}
// Password change flow (unchanged)
if ($newPassword || $confirmPassword || $currentPassword) {
if (!$userRepository->isPasswordValid($user, $currentPassword)) {
$form->get('currentPassword')->addError(new FormError(
$this->translator->trans('The current password is incorrect')
));
} elseif ($newPassword !== $confirmPassword) {
$form->get('confirmPassword')->addError(new FormError(
$this->translator->trans('Passwords do not match')
));
} else {
$user->setPlainPassword($newPassword);
$user->setPasswordUpdatedAt(new DateTimeImmutable());
$userRepository->updateUser($user);
$this->addFlash('success', $this->translator->trans('Password updated successfully'));
// Re-login if the user was not logged in (edge case when rotating from link)
if (!$this->getUser()) {
$token = new UsernamePasswordToken($user, 'main', $user->getRoles());
$tokenStorage->setToken($token);
$request->getSession()->set('_security_main', serialize($token));
}
return $this->redirectToRoute('chamilo_core_account_home');
}
}
}
} else {
error_log('Form is NOT valid.');
}
} else {
error_log('Form NOT submitted yet.');
}
return $this->render('@ChamiloCore/Account/change_password.html.twig', [
'form' => $form->createView(),
'qrCode' => $qrCodeBase64,
'user' => $user,
'showQRCode' => $showQRCode,
'password_requirements' => Security::getPasswordRequirements()['min'],
]);
}
/**
* Encrypts the TOTP secret using AES-256-CBC encryption.
*/
private function encryptTOTPSecret(string $secret, string $encryptionKey): string
{
$cipherMethod = 'aes-256-cbc';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod));
$encryptedSecret = openssl_encrypt($secret, $cipherMethod, $encryptionKey, 0, $iv);
return base64_encode($iv.'::'.$encryptedSecret);
}
/**
* Decrypts the TOTP secret using AES-256-CBC decryption.
*/
private function decryptTOTPSecret(string $encryptedSecret, string $encryptionKey): string
{
$cipherMethod = 'aes-256-cbc';
list($iv, $encryptedData) = explode('::', base64_decode($encryptedSecret), 2);
return openssl_decrypt($encryptedData, $cipherMethod, $encryptionKey, 0, $iv);
}
/**
* Validate the password against the same requirements as the client-side validation.
*/
private function validatePassword(string $password): array
{
$errors = [];
$minRequirements = Security::getPasswordRequirements()['min'];
if (\strlen($password) < $minRequirements['length']) {
$errors[] = $this->translator->trans('Password must be at least %length% characters long.', ['%length%' => $minRequirements['length']]);
}
if ($minRequirements['lowercase'] > 0 && !preg_match('/[a-z]/', $password)) {
$errors[] = $this->translator->trans('Password must contain at least %count% lowercase characters.', ['%count%' => $minRequirements['lowercase']]);
}
if ($minRequirements['uppercase'] > 0 && !preg_match('/[A-Z]/', $password)) {
$errors[] = $this->translator->trans('Password must contain at least %count% uppercase characters.', ['%count%' => $minRequirements['uppercase']]);
}
if ($minRequirements['numeric'] > 0 && !preg_match('/[0-9]/', $password)) {
$errors[] = $this->translator->trans('Password must contain at least %count% numerical (0-9) characters.', ['%count%' => $minRequirements['numeric']]);
}
if ($minRequirements['specials'] > 0 && !preg_match('/[\W]/', $password)) {
$errors[] = $this->translator->trans('Password must contain at least %count% special characters.', ['%count%' => $minRequirements['specials']]);
}
return $errors;
}
}