src/Controller/ForgotPasswordController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ForgotPasswordType;
  5. use App\Form\ResetPasswordType;
  6. use App\Repository\UserRepository;
  7. use App\Security\EmailVerifier;
  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. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  15. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  16. use DateTime;
  17. class ForgotPasswordController extends AbstractController
  18. {
  19.     private EmailVerifier $emailVerifier;
  20.     private $entityManager;
  21.     private $userRepository;
  22.     private $verifyEmail;
  23.     public function __construct(EmailVerifier $emailVerifierEntityManagerInterface $entityManagerVerifyEmailHelperInterface $verifyEmail )
  24.     {
  25.         $this->entityManager $entityManager;
  26.         $this->verifyEmail $verifyEmail;
  27.         $this->emailVerifier $emailVerifier;
  28.     }
  29.     #[Route('/forgot/password'name'forgot_password')]
  30.     public function sendRecoveryLink(Request $request): Response
  31.     {
  32.         $form $this->createForm(ForgotPasswordType::class);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted()){
  35.             $user $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy([
  36.                 'email' => $form['email']->getData()
  37.             ]
  38.             );
  39.             $this->addFlash('success''Un email vous a été envoyé pour redéfinir votre mot de passe.');
  40.             if ($user) {
  41.                 //envoie du mail
  42.                 $this->emailVerifier->sendEmailForgotPassword($user);
  43.             }
  44.             return $this->redirectToRoute('login');
  45.         }
  46.         return $this->render('acoa/security/forgot_password.html.twig', [
  47.             'form' => $form->createView()
  48.         ]);
  49.     }
  50.     #[Route('/change'name'change_password_route')]
  51.     public function verifyUserEmail(Request $requestUserRepository $userRepositoryUserPasswordHasherInterface $encoder): Response
  52.     {
  53.        $id $request->get('id'); // retrieve the user id from the url
  54.        // Verify the user id exists and is not null
  55.        if (null === $id) {
  56.            return $this->redirectToRoute('login');
  57.        }
  58.        $user $userRepository->find($id);
  59.        // Ensure the user exists in persistence
  60.        if (null === $user) {
  61.            return $this->redirectToRoute('login');
  62.        }
  63.         // Do not get the User's Id or Email Address from the Request object
  64.         try {
  65.             $this->verifyEmail->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
  66.         } catch (VerifyEmailExceptionInterface $e) {
  67.             $this->addFlash('verify_email_error'$e->getReason());
  68.             return $this->redirectToRoute('app_register');
  69.         }
  70.         $form $this->createForm(ResetPasswordType::class);
  71.        
  72.         $form->handleRequest($request);
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             // Mark your user as verified. e.g. switch a User::verified property to true
  75.             $data $form->getData();
  76.             $password $encoder->hashPassword($user$data->getPassword());
  77.             $user->setPassword($password);
  78.             $user->setIsVerified(1);
  79.             $this->entityManager->persist($user);
  80.             $this->entityManager->flush();
  81.             $this->addFlash('success''Votre mot de passe a bien été modifié.');
  82.             return $this->redirectToRoute('login');
  83.         }
  84.         return $this->render('acoa/security/change_password.html.twig', [
  85.             'form' => $form->createView()
  86.         ]);
  87.     }
  88. }