src/Controller/ResetPasswordController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\CoreController;
  4. use App\Entity\User;
  5. use App\Form\ChangePasswordFormType;
  6. use App\Form\ResetPasswordRequestFormType;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  13. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  14. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  15. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  16. /**
  17.  * @Route("/password/reset-password")
  18.  */
  19. class ResetPasswordController extends CoreController
  20. {
  21.     use ResetPasswordControllerTrait;
  22.     /**
  23.      * Display & process form to request a password reset.
  24.      *
  25.      * @Route("", name="app_forgot_password_request")
  26.      * @Route("/front/{myflix}", name="app_forgot_password_request_myflix")
  27.      */
  28.     public function request(ResetPasswordHelperInterface $resetPasswordHelper): Response
  29.     {
  30.         $myflix $this->requestStack->getCurrentRequest()->attributes->get('myflix');
  31.         if(!is_null($myflix)) {
  32.             $getMyflix $this->repository->MyflixRepository->findOneBy(['slug' => $myflix]);
  33.             if (!$getMyflix) {
  34.                 return $this->redirectToRoute('error_404');
  35.             }
  36.         }
  37.         $form $this->createForm(ResetPasswordRequestFormType::class);
  38.         $form->handleRequest($this->requestStack->getCurrentRequest());
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             return $this->processSendingPasswordResetEmail(
  41.                 $this->requestStack->getCurrentRequest(),
  42.                 $form->get('email')->getData(),
  43.                 $myflix,
  44.                 $resetPasswordHelper
  45.             );
  46.         }
  47.         if(!is_null($myflix)) {
  48.             return $this->render('security/reset_password/request_myflix.html.twig', [
  49.                 'requestForm' => $form->createView(),
  50.                 'myflix' => $myflix
  51.             ]);
  52.         }else{
  53.             return $this->render('security/reset_password/request.html.twig', [
  54.                 'requestForm' => $form->createView(),
  55.             ]);
  56.         }
  57.     }
  58.     /**
  59.      * Confirmation page after a user has requested a password reset.
  60.      *
  61.      * @Route("/check-email", name="app_check_email")
  62.      * @Route("/check-email/{myflix}", name="app_check_email_myflix")
  63.      */
  64.     public function checkEmail(): Response
  65.     {
  66.         $myflix $this->requestStack->getCurrentRequest()->attributes->get('myflix');
  67.         if(!is_null($myflix)) {
  68.             $getMyflix $this->repository->MyflixRepository->findOneBy(['slug' => $myflix]);
  69.             if (!$getMyflix) {
  70.                 return $this->redirectToRoute('error_404');
  71.             }
  72.         }
  73.         // We prevent users from directly accessing this page
  74.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  75.             if(!is_null($myflix)) {
  76.                 return $this->redirectToRoute('app_forgot_password_request_myflix', ['myflix' => $myflix]);
  77.             }else {
  78.                 return $this->redirectToRoute('app_forgot_password_request');
  79.             }
  80.         }
  81.         if(!is_null($myflix)) {
  82.             return $this->render('security/reset_password/check_email_myflix.html.twig', [
  83.                 'resetToken' => $resetToken,
  84.                 'myflix' => $myflix
  85.             ]);
  86.         }else{
  87.             return $this->render('security/reset_password/check_email.html.twig', [
  88.                 'resetToken' => $resetToken,
  89.             ]);
  90.         }
  91.     }
  92.     /**
  93.      * Validates and process the reset URL that the user clicked in their email.
  94.      *
  95.      * @Route("/reset/{token}", name="app_reset_password")
  96.      * @Route("/reset/{token}/{myflix}", name="app_reset_password_myflix")
  97.      */
  98.     public function reset(string $token nullResetPasswordHelperInterface $resetPasswordHelper): Response
  99.     {
  100.         $session $this->requestStack->getSession();
  101.         $myflix $this->requestStack->getCurrentRequest()->attributes->get('myflix');
  102.         if(!is_null($myflix)) {
  103.             $getMyflix $this->repository->MyflixRepository->findOneBy(['slug' => $myflix]);
  104.             if (!$getMyflix) {
  105.                 return $this->redirectToRoute('error_404');
  106.             }
  107.         }
  108.         if ($token) {
  109.             // We store the token in session and remove it from the URL, to avoid the URL being
  110.             // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
  111.             $this->storeTokenInSession($token);
  112.             $myflix_reset_password = !is_null($myflix) ? $myflix '';
  113.             $session->set('myflix_reset_password'$myflix_reset_password);
  114.             return $this->redirectToRoute('app_reset_password');
  115.         }
  116.         $token $this->getTokenFromSession();
  117.         if (null === $token) {
  118.             throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  119.         }
  120.         try {
  121.             $user $resetPasswordHelper->validateTokenAndFetchUser($token);
  122.         } catch (ResetPasswordExceptionInterface $e) {
  123.             $pos strpos($e->getReason(), 'The reset password link is invalid');
  124.             if ($pos === false) {
  125.                 $msg $e->getReason();
  126.             } else {
  127.                 $msg $this->translator->trans('text.msg_eroor_lien_reset_request');
  128.             }
  129.             $this->addFlash('reset_password_error'sprintf(
  130.                 $this->translator->trans('text.msg_eroor_validating_reset_request').' - %s',
  131.                 $msg
  132.             ));
  133.             if($session->get('myflix_reset_password') != '') {
  134.                 $session->set('myflix_reset_password'null);
  135.                 return $this->redirectToRoute('app_forgot_password_request_myflix', ['myflix' => $session->get('myflix_reset_password')]);
  136.             }else {
  137.                 return $this->redirectToRoute('app_forgot_password_request');
  138.             }
  139.         }
  140.         // The token is valid; allow the user to change their password.
  141.         $form $this->createForm(ChangePasswordFormType::class);
  142.         $form->handleRequest($this->requestStack->getCurrentRequest());
  143.         if ($form->isSubmitted() && $form->isValid()) {
  144.             // A password reset token should be used only once, remove it.
  145.             $resetPasswordHelper->removeResetRequest($token);
  146.             // Encode the plain password, and set it.
  147.             $hashedPassword $this->passwordHasher->hashPassword(
  148.                 $user,
  149.                 $form->get('plainPassword')->getData()
  150.             );
  151.             $user->setPassword($hashedPassword);
  152.             $passwordEncrypt $this->encryptPassword($form->get('plainPassword')->getData());
  153.             $user->setPasswordEncrypt($passwordEncrypt);
  154.             $myflix_slug $this->requestStack->getCurrentRequest()->get('slug_myflix');
  155.             $this->update();
  156.             // The session is cleaned up after the password has been changed.
  157.             $this->cleanSessionAfterReset();
  158.             $this->addFlash('success'$this->translator->trans('text.msg_success_password_successfully_changed'));
  159.             if(!empty($myflix_slug)){
  160.                 return $this->redirectToRoute('app_login_membre', ['myflix' => $myflix_slug]);
  161.             }else{
  162.                 return $this->redirectToRoute('app_login');
  163.             }
  164.         }
  165.         if(!empty($session->get('myflix_reset_password'))){
  166.             return $this->render('security/reset_password/reset_myflix.html.twig', [
  167.                 'resetForm' => $form->createView(),
  168.                 'myflix' => $session->get('myflix_reset_password')
  169.             ]);
  170.         }else{
  171.             return $this->render('security/reset_password/reset.html.twig', [
  172.                 'resetForm' => $form->createView(),
  173.                 'myflix' => $session->get('myflix_reset_password')
  174.             ]);
  175.         }
  176.     }
  177.     private function processSendingPasswordResetEmail($requeststring $emailFormData$myflixResetPasswordHelperInterface $resetPasswordHelper): RedirectResponse
  178.     {
  179.         $user $this->repository->UserRepository->findOneBy([
  180.             'email' => $emailFormData,
  181.         ]);
  182.         // Do not reveal whether a user account was found or not.
  183.         if (!$user) {
  184.             if(!is_null($myflix)) {
  185.                 return $this->redirectToRoute('app_check_email_myflix', ['myflix' => $myflix]);
  186.             }else {
  187.                 return $this->redirectToRoute('app_check_email');
  188.             }
  189.         }
  190.         try {
  191.             $resetToken $resetPasswordHelper->generateResetToken($user);
  192.             /** send mail to partner **/
  193.             $data_mess = [
  194.                 'resetToken' => $resetToken,
  195.                 'myflix' => $myflix
  196.             ];
  197.             $mailerSender $this->getParameter('mailer.support_email');
  198.             $template 'back/default/emails/notif_reset_password.html.twig';
  199.             $subject $this->translator->trans('text.msg_eroor_lien_reset_request');
  200.             $sendingMailOperation $this->mailer->send($mailerSender,
  201.                 trim($emailFormData),
  202.                 $template,
  203.                 $subject,
  204.                 null,
  205.                 $data_mess
  206.             );
  207.         } catch (ResetPasswordExceptionInterface $e) {
  208.             // If you want to tell the user why a reset email was not sent, uncomment
  209.             // the lines below and change the redirect to 'app_forgot_password_request'.
  210.             // Caution: This may reveal if a user is registered or not.
  211.             //
  212.             // $this->addFlash('reset_password_error', sprintf(
  213.             //     'There was a problem handling your password reset request - %s',
  214.             //     $e->getReason()
  215.             // ));
  216.             if(!is_null($myflix)) {
  217.                 return $this->redirectToRoute('app_check_email_myflix', ['myflix' => $myflix]);
  218.             }else {
  219.                 return $this->redirectToRoute('app_check_email');
  220.             }
  221.         }
  222.         // Store the token object in session for retrieval in check-email route.
  223.         $this->setTokenObjectInSession($resetToken);
  224.         if(!is_null($myflix)) {
  225.             return $this->redirectToRoute('app_check_email_myflix', ['myflix' => $myflix]);
  226.         }else {
  227.             return $this->redirectToRoute('app_check_email');
  228.         }
  229.     }
  230. }