src/Controller/ResetPasswordController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Informationeps;
  4. use App\Entity\User;
  5. use App\Form\ChangePasswordFormType;
  6. use App\Form\ResetPasswordRequestFormType;
  7. use App\Repository\InformationepsRepository;
  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 SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  19. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  20. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  21. /**
  22.  * @Route("/reset-password")
  23.  */
  24. class ResetPasswordController extends AbstractController
  25. {
  26.     use ResetPasswordControllerTrait;
  27.     private $resetPasswordHelper;
  28.     public function __construct(ResetPasswordHelperInterface $resetPasswordHelper)
  29.     {
  30.         $this->resetPasswordHelper $resetPasswordHelper;
  31.     }
  32.     /**
  33.      * Display & process form to request a password reset.
  34.      *
  35.      * @Route("", name="app_forgot_password_request")
  36.      */
  37.     public function request(Request $requestMailerInterface $mailer,InformationepsRepository $informationepsRepository): Response
  38.     {
  39.       //  dd("fffff");
  40.         $form $this->createForm(ResetPasswordRequestFormType::class);
  41.         $form->handleRequest($request);
  42.         if ($form->isSubmitted() && $form->isValid()) {
  43.             return $this->processSendingPasswordResetEmail(
  44.                 $form->get('email')->getData(),
  45.                 $mailer
  46.             );
  47.         }
  48.         $logo $informationepsRepository->find(1)->getImageFiligrame();
  49.         return $this->render('admin/reset_password/request.html.twig', [
  50.             'requestForm' => $form->createView(),
  51.             'logo'=>$logo
  52.         ]);
  53.     }
  54.     /**
  55.      * Confirmation page after a user has requested a password reset.
  56.      *
  57.      * @Route("/check-email", name="app_check_email")
  58.      */
  59.     public function checkEmail(): Response
  60.     {
  61.         // Generate a fake token if the user does not exist or someone hit this page directly.
  62.         // This prevents exposing whether or not a user was found with the given email address or not
  63.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  64.             $resetToken $this->resetPasswordHelper->generateFakeResetToken();
  65.         }
  66.         //dd($resetToken);
  67.         $eps $this->getDoctrine()->getRepository(Informationeps::class)->find(1);
  68.         return $this->render('admin/reset_password/check_email.html.twig', [
  69.             'resetToken' => $resetToken,
  70.             'eps'=>$eps
  71.         ]);
  72.     }
  73.     /**
  74.      * Validates and process the reset URL that the user clicked in their email.
  75.      *
  76.      * @Route("/reset/{token}", name="app_reset_password")
  77.      */
  78.     public function reset(Request $requestUserPasswordHasherInterface $userPasswordHasherInterfacestring $token null): Response
  79.     {
  80.         if ($token) {
  81.             // We store the token in session and remove it from the URL, to avoid the URL being
  82.             // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
  83.             $this->storeTokenInSession($token);
  84.             return $this->redirectToRoute('app_reset_password');
  85.         }
  86.         $token $this->getTokenFromSession();
  87.         if (null === $token) {
  88.             throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  89.         }
  90.         try {
  91.             $user $this->resetPasswordHelper->validateTokenAndFetchUser($token);
  92.         } catch (ResetPasswordExceptionInterface $e) {
  93.             $this->addFlash('reset_password_error'sprintf(
  94.                 'There was a problem validating your reset request - %s',
  95.                 $e->getReason()
  96.             ));
  97.             return $this->redirectToRoute('app_forgot_password_request');
  98.         }
  99.         // The token is valid; allow the user to change their password.
  100.         $form $this->createForm(ChangePasswordFormType::class);
  101.         $form->handleRequest($request);
  102.         if ($form->isSubmitted() && $form->isValid()) {
  103.             // A password reset token should be used only once, remove it.
  104.             $this->resetPasswordHelper->removeResetRequest($token);
  105.             // Encode(hash) the plain password, and set it.
  106.             $encodedPassword $userPasswordHasherInterface->hashPassword(
  107.                 $user,
  108.                 $form->get('plainPassword')->getData()
  109.             );
  110.             $user->setPassword($encodedPassword);
  111.             $this->getDoctrine()->getManager()->flush();
  112.             // The session is cleaned up after the password has been changed.
  113.             $this->cleanSessionAfterReset();
  114.             return $this->redirectToRoute('app_home_admin');
  115.         }
  116.         return $this->render('admin/reset_password/reset.html.twig', [
  117.             'resetForm' => $form->createView(),
  118.         ]);
  119.     }
  120.     private function processSendingPasswordResetEmail(string $emailFormDataMailerInterface $mailer): RedirectResponse
  121.     {
  122.         // get eps information
  123.         //$eps = $this->getDoctrine()->getRepository(Informationeps::class)->find(1);
  124.         $user $this->getDoctrine()->getRepository(User::class)->findOneBy([
  125.             'email' => $emailFormData,
  126.         ]);
  127.         
  128.         // Do not reveal whether a user account was found or not.
  129.         if (!$user) {
  130.             return $this->redirectToRoute('app_check_email');
  131.         }
  132.         
  133.         try {
  134.             $resetToken $this->resetPasswordHelper->generateResetToken($user);
  135.             //dd($resetToken);
  136.         } catch (ResetPasswordExceptionInterface $e) {
  137.             // If you want to tell the user why a reset email was not sent, uncomment
  138.             // the lines below and change the redirect to 'app_forgot_password_request'.
  139.             // Caution: This may reveal if a user is registered or not.
  140.             //
  141.             // $this->addFlash('reset_password_error', sprintf(
  142.             //     'There was a problem handling your password reset request - %s',
  143.             //     $e->getReason()
  144.             // ));
  145.             
  146.             return $this->redirectToRoute('app_check_email');
  147.         }
  148.         $email = (new TemplatedEmail())
  149.             ->from(new Address(urldecode($_ENV['MAILER_FROM']), 'EPS'))
  150.             ->to($user->getEmail())
  151.             ->subject('RĂ©initialisation de votre mot de passe')
  152.             ->htmlTemplate('admin/reset_password/email.html.twig')
  153.             ->context([
  154.                 'resetToken' => $resetToken,
  155.             ])
  156.         ;
  157.         try{
  158.             $mailer->send($email);
  159.         } catch (TransportExceptionInterface $e) {
  160.             return dd($e);
  161.         }
  162.         // Store the token object in session for retrieval in check-email route.
  163.         $this->setTokenObjectInSession($resetToken);
  164.         
  165.         return $this->redirectToRoute('app_check_email');
  166.     }
  167. }