src/Controller/ResetPasswordController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ForgotPasswordType;
  4. use App\Form\ResetPasswordType;
  5. use App\Repository\UserRepository;
  6. use App\Service\ResetPasswordService;
  7. use App\Service\UserNotificationService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class ResetPasswordController extends AbstractController
  15. {
  16.     private $entityManager;
  17.     private $userRepository;
  18.     private $resetPasswordService;
  19.     private $notificationService;
  20.     private $passwordHasher;
  21.     public function __construct(
  22.         EntityManagerInterface $entityManager,
  23.         UserRepository $userRepository,
  24.         ResetPasswordService $resetPasswordService,
  25.         UserNotificationService $notificationService,
  26.         UserPasswordHasherInterface $passwordHasher
  27.     ) {
  28.         $this->entityManager $entityManager;
  29.         $this->userRepository $userRepository;
  30.         $this->resetPasswordService $resetPasswordService;
  31.         $this->notificationService $notificationService;
  32.         $this->passwordHasher $passwordHasher;
  33.     }
  34.     /**
  35.      * @Route("/forgot-password", name="app_forgot_password")
  36.      */
  37.     public function request(Request $request): Response
  38.     {
  39.         if ($this->getUser()) {
  40.             return $this->redirectToRoute('app_dashboard');
  41.         }
  42.         $form $this->createForm(ForgotPasswordType::class);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             $email $form->get('email')->getData();
  46.             $user $this->userRepository->findByEmail($email);
  47.             if ($user && $user->getIsActive()) {
  48.                 // Créer le token de réinitialisation
  49.                 $resetToken $this->resetPasswordService->createResetToken($user);
  50.                 // Envoyer l'email
  51.                 $this->notificationService->sendResetPasswordEmail($user$resetToken->getToken());
  52.             }
  53.             // Toujours rediriger vers la page de confirmation pour éviter l'énumération des utilisateurs
  54.             return $this->render('security/forgot_password_check_email.html.twig');
  55.         }
  56.         return $this->render('security/forgot_password.html.twig', [
  57.             'form' => $form->createView(),
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/reset-password/{token}", name="app_reset_password")
  62.      */
  63.     public function reset(string $tokenRequest $request): Response
  64.     {
  65.         if ($this->getUser()) {
  66.             return $this->redirectToRoute('app_dashboard');
  67.         }
  68.         $resetToken $this->resetPasswordService->validateToken($token);
  69.         if (!$resetToken) {
  70.             $this->addFlash('error''Ce lien de réinitialisation est invalide ou a expiré. Veuillez faire une nouvelle demande.');
  71.             return $this->redirectToRoute('app_forgot_password');
  72.         }
  73.         $form $this->createForm(ResetPasswordType::class);
  74.         $form->handleRequest($request);
  75.         if ($form->isSubmitted() && $form->isValid()) {
  76.             $newPassword $form->get('newPassword')->getData();
  77.             $user $resetToken->getUser();
  78.             // Hasher et enregistrer le nouveau mot de passe
  79.             $hashedPassword $this->passwordHasher->hashPassword($user$newPassword);
  80.             $user->setPassword($hashedPassword);
  81.             $user->setMustChangePassword(false);
  82.             // Marquer le token comme utilisé
  83.             $this->resetPasswordService->markTokenAsUsed($resetToken);
  84.             $this->entityManager->flush();
  85.             // Envoyer un email de confirmation
  86.             $this->notificationService->sendPasswordChangedNotification($user);
  87.             $this->addFlash('success''Votre mot de passe a été réinitialisé avec succès ! Vous pouvez maintenant vous connecter.');
  88.             return $this->redirectToRoute('app_login');
  89.         }
  90.         return $this->render('security/reset_password.html.twig', [
  91.             'form' => $form->createView(),
  92.             'token' => $token,
  93.         ]);
  94.     }
  95. }