Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Controller/ResetPasswordController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ChangePasswordFormType;
  5. use App\Form\ResetPasswordRequestFormType;
  6. use App\Service\NotificationService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  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. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  16. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  17. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  18. #[Route('{_locale<%app.supported_locales%>}/reinitialisation-mot-de-passe')]
  19. class ResetPasswordController extends AbstractController
  20. {
  21.     use ResetPasswordControllerTrait;
  22.     private EntityManagerInterface $entityManager;
  23.     private ResetPasswordHelperInterface $resetPasswordHelper;
  24.     private NotificationService $notificationService;
  25.     /**
  26.      * @param EntityManagerInterface $entityManager
  27.      * @param ResetPasswordHelperInterface $resetPasswordHelper
  28.      * @param NotificationService $notificationService
  29.      */
  30.     public function __construct(EntityManagerInterface $entityManagerResetPasswordHelperInterface $resetPasswordHelperNotificationService $notificationService)
  31.     {
  32.         $this->entityManager $entityManager;
  33.         $this->resetPasswordHelper $resetPasswordHelper;
  34.         $this->notificationService $notificationService;
  35.     }
  36.     /**
  37.      * Display & process form to request a password reset.
  38.      */
  39.     #[Route(''name'app_forgot_password_request')]
  40.     public function request(Request $request): Response
  41.     {
  42.         $form $this->createForm(ResetPasswordRequestFormType::class);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             return $this->processSendingPasswordResetEmail(
  46.                 $form->get('email')->getData()
  47.             );
  48.         }
  49.         return $this->render('reset_password/request.html.twig', [
  50.             'requestForm' => $form->createView(),
  51.         ]);
  52.     }
  53.     /**
  54.      * Confirmation page after a user has requested a password reset.
  55.      */
  56.     #[Route('/verification-email'name'app_check_email')]
  57.     public function checkEmail(): Response
  58.     {
  59.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  60.             $resetToken $this->resetPasswordHelper->generateFakeResetToken();
  61.         }
  62.         return $this->render('reset_password/check_email.html.twig', [
  63.             'resetToken' => $resetToken,
  64.         ]);
  65.     }
  66.     /**
  67.      * Validates and process the reset URL that the user clicked in their email.
  68.      */
  69.     #[Route('/reinitialisation/{token}'name'app_reset_password')]
  70.     public function reset(Request $requestUserPasswordHasherInterface $passwordHasherTranslatorInterface $translatorstring $token null): Response
  71.     {
  72.         if ($token) {
  73.             $this->storeTokenInSession($token);
  74.             return $this->redirectToRoute('app_reset_password');
  75.         }
  76.         $token $this->getTokenFromSession();
  77.         if (null === $token) {
  78.             throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  79.         }
  80.         try {
  81.             /** @var User|null $user */
  82.             $user $this->resetPasswordHelper->validateTokenAndFetchUser($token);
  83.         } catch (ResetPasswordExceptionInterface $e) {
  84.             $this->addFlash('reset_password_error'sprintf(
  85.                 '%s - %s',
  86.                 $translator->trans(ResetPasswordExceptionInterface::MESSAGE_PROBLEM_VALIDATE, [], 'ResetPasswordBundle'),
  87.                 $translator->trans($e->getReason(), [], 'ResetPasswordBundle')
  88.             ));
  89.             return $this->redirectToRoute('app_forgot_password_request');
  90.         }
  91.         $form $this->createForm(ChangePasswordFormType::class);
  92.         $form->handleRequest($request);
  93.         if ($form->isSubmitted() && $form->isValid()) {
  94.             $this->resetPasswordHelper->removeResetRequest($token);
  95.             $encodedPassword $passwordHasher->hashPassword(
  96.                 $user,
  97.                 $form->get('plainPassword')->getData()
  98.             );
  99.             $user->setRegisterToken(NULL);
  100.             $user->setPassword($encodedPassword);
  101.             $this->entityManager->flush();
  102.             $this->cleanSessionAfterReset();
  103.             return $this->redirectToRoute('app_login');
  104.         }
  105.         return $this->render('reset_password/reset.html.twig', [
  106.             'resetForm' => $form->createView(),
  107.         ]);
  108.     }
  109.     private function processSendingPasswordResetEmail(string $emailFormData): RedirectResponse
  110.     {
  111.         $user $this->entityManager->getRepository(User::class)->findOneBy([
  112.             'email' => $emailFormData,
  113.         ]);
  114.         if (!$user) {
  115.             return $this->redirectToRoute('app_check_email');
  116.         }
  117.         try {
  118.             $resetToken $this->resetPasswordHelper->generateResetToken($user);
  119.         } catch (ResetPasswordExceptionInterface) {
  120.             return $this->redirectToRoute('app_check_email');
  121.         }
  122.         $this->notificationService->sendForgotPasswordEmail($user$resetToken);
  123.         $this->setTokenObjectInSession($resetToken);
  124.         return $this->redirectToRoute('app_check_email');
  125.     }
  126. }