src/Controller/ResetPasswordController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Provider;
  4. use App\Features\Core\ILogger;
  5. use App\Form\ChangePasswordFormType;
  6. use App\Form\ResetPasswordRequestFormType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  20. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  21. use SymfonyCasts\Bundle\ResetPassword\Exception\TooManyPasswordRequestsException;
  22. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  23. #[Route([
  24.     "fr" => "/reinitialiser-mot-de-passe",
  25.     "en" => '/reset-password',
  26. ])]
  27. class ResetPasswordController extends AbstractController
  28. {
  29.     use ResetPasswordControllerTrait;
  30.     private $resetPasswordHelper;
  31.     private $entityManager;
  32.     private string $mailFrom;
  33.     public function __construct(ResetPasswordHelperInterface $resetPasswordHelperEntityManagerInterface $entityManagerstring $mailFrom)
  34.     {
  35.         $this->resetPasswordHelper $resetPasswordHelper;
  36.         $this->entityManager $entityManager;
  37.         $this->mailFrom $mailFrom;
  38.     }
  39.     /**
  40.      * Display & process form to request a password reset.
  41.      */
  42.     #[Route(''name'app_forgot_password_request')]
  43.     public function request(Request $requestMailerInterface $mailerTranslatorInterface $translator): Response
  44.     {
  45.         $form $this->createForm(ResetPasswordRequestFormType::class);
  46.         $form->handleRequest($request);
  47.         if ($form->isSubmitted() && $form->isValid()) {
  48.             return $this->processSendingPasswordResetEmail(
  49.                 $form->get('email')->getData(),
  50.                 $mailer,
  51.                 $translator
  52.             );
  53.         }
  54.         return $this->render('reset_password/request.html.twig', [
  55.             'requestForm' => $form->createView(),
  56.         ]);
  57.     }
  58.     /**
  59.      * Confirmation page after a user has requested a password reset.
  60.      */
  61.     #[Route([
  62.         "fr" => "/confirmation",
  63.         "en" => '/check-email',
  64.     ], name'app_check_email')]
  65.     public function checkEmail(): Response
  66.     {
  67.         // Generate a fake token if the user does not exist or someone hit this page directly.
  68.         // This prevents exposing whether or not a user was found with the given email address or not
  69.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  70.             $resetToken $this->resetPasswordHelper->generateFakeResetToken();
  71.         }
  72.         return $this->render('reset_password/check_email.html.twig', [
  73.             'resetToken' => $resetToken,
  74.         ]);
  75.     }
  76.     /**
  77.      * Validates and process the reset URL that the user clicked in their email.
  78.      */
  79.     #[Route([
  80.         "fr" => "/changer/{token}",
  81.         "en" => '/reset/{token}',
  82.     ], name'app_reset_password')]
  83.     public function reset(Request $requestUserPasswordHasherInterface $userPasswordHasherTranslatorInterface $translatorILogger $loggerstring $token null): Response
  84.     {
  85.         if ($token) {
  86.             // We store the token in session and remove it from the URL, to avoid the URL being
  87.             // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
  88.             $this->storeTokenInSession($token);
  89.             return $this->redirectToRoute('app_reset_password');
  90.         }
  91.         $token $this->getTokenFromSession();
  92.         if (null === $token) {
  93.             throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  94.         }
  95.         try {
  96.             $user $this->resetPasswordHelper->validateTokenAndFetchUser($token);
  97.         } catch (ResetPasswordExceptionInterface $e) {
  98.             $this->addFlash('error'sprintf(
  99.                 '%s - %s',
  100.                 $translator->trans(ResetPasswordExceptionInterface::MESSAGE_PROBLEM_VALIDATE, [], 'ResetPasswordBundle'),
  101.                 $translator->trans($e->getReason(), [], 'ResetPasswordBundle')
  102.             ));
  103.             return $this->redirectToRoute('app_forgot_password_request');
  104.         }
  105.         // The token is valid; allow the user to change their password.
  106.         $form $this->createForm(ChangePasswordFormType::class);
  107.         $form->handleRequest($request);
  108.         if ($form->isSubmitted()) {
  109.             if($form->isValid()){
  110.                 // A password reset token should be used only once, remove it.
  111.                 $this->resetPasswordHelper->removeResetRequest($token);
  112.                 // Encode(hash) the plain password, and set it.
  113.                 $encodedPassword $userPasswordHasher->hashPassword(
  114.                     $user,
  115.                     $form->get('plainPassword')->getData()
  116.                 );
  117.                 $user->setHashPassword($encodedPassword);
  118.                 $this->entityManager->flush();
  119.                 // The session is cleaned up after the password has been changed.
  120.                 $this->cleanSessionAfterReset();
  121.                 $logger->log($user"Mot de passe modifié""Vous avez modifié votre mot de passe"$user);
  122.                 $this->addFlash('notice'"Votre mot de passe a été modifié avec succès.");
  123.                 return $this->redirectToRoute('provider_login');
  124.             }else{
  125.                 $this->addFlash('error'"Veuillez remplir des mots de passe valides.");
  126.             }
  127.         }
  128.         return $this->render('reset_password/reset.html.twig', [
  129.             'resetForm' => $form->createView(),
  130.         ]);
  131.     }
  132.     private function processSendingPasswordResetEmail(string $emailFormDataMailerInterface $mailerTranslatorInterface $translator): RedirectResponse
  133.     {
  134.         $user $this->entityManager->getRepository(Provider::class)->findOneBy([
  135.             'email' => $emailFormData,
  136.         ]);
  137.         // Do not reveal whether a user account was found or not.
  138.         if (!$user) {
  139.             return $this->redirectToRoute('app_check_email');
  140.         }
  141.         try {
  142.             $resetToken $this->resetPasswordHelper->generateResetToken($user);
  143.         } catch (ResetPasswordExceptionInterface $e) {
  144.             if($e instanceof TooManyPasswordRequestsException){
  145.                 $this->addFlash('error'"Vous avez déjà fait une demande de réinitialisation de votre mot de passe. Veuillez réessayer à ".$e->getAvailableAt()->format('d M Y H:i:s')." .");
  146.             }else{
  147.                 $this->addFlash('error'"Un problème est survenu. Veuillez réessayer plus tard.");
  148.             }
  149.             return $this->redirectToRoute('app_forgot_password_request');
  150.         }
  151.         $email = (new TemplatedEmail())
  152.             ->from(new Address($this->mailFrom'NSIA BANQUE Bénin'))
  153.             ->to($user->getEmail())
  154.             ->subject("Réinitialisation de mot de passe")
  155.             ->htmlTemplate('reset_password/email.html.twig')
  156.             ->context([
  157.                 'resetToken' => $resetToken,
  158.             ])
  159.         ;
  160.         try {
  161.             $mailer->send($email);
  162.         } catch (TransportExceptionInterface $e) {
  163.             $this->setTokenObjectInSession($resetToken);
  164.             $this->addFlash('error'"Un problème est survenu lors de l'envoi de l'email. Veuillez réessayer.");
  165.             return $this->redirectToRoute('app_forgot_password_request');
  166.         }
  167.         return $this->redirectToRoute('app_check_email');
  168.     }
  169. }