src/Controller/RegistrationController.php line 35

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  17. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  18. class RegistrationController extends AbstractController
  19. {
  20.     private $verifyEmailHelper;
  21.     private $requestStack;
  22.     private $mailer;
  23.     public function __construct(RequestStack $requestStackVerifyEmailHelperInterface $helperMailerInterface $mailer)
  24.     {
  25.         $this->requestStack $requestStack;
  26.         $this->verifyEmailHelper $helper;
  27.         $this->mailer $mailer;
  28.     }
  29.     #[Route('/register'name'app_register')]
  30.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  31.     {
  32.         if ($this->getUser()) {
  33.             return $this->redirectToRoute('home');
  34.         }
  35.         $user = new User();
  36.         $form $this->createForm(RegistrationFormType::class, $user);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             // encode the plain password
  40.             $user->setPassword(
  41.                 $userPasswordHasher->hashPassword(
  42.                     $user,
  43.                     $form->get('plainPassword')->getData()
  44.                 )
  45.             );
  46.             $entityManager->persist($user);
  47.             $entityManager->flush();
  48.             // generate a signed url and email it to the user
  49.             $signatureComponents $this->verifyEmailHelper->generateSignature(
  50.                 'registration_confirmation_route',
  51.                 $user->getId(),
  52.                 $user->getEmail(),
  53.                 ['id' => $user->getId()] // add the user's id as an extra query param
  54.             );
  55.             $email = new TemplatedEmail();
  56.             $email->from(new Address('metalead@preprod.my-site-web.com''Contact Metalead'));
  57.             $email->to($user->getEmail());
  58.             $email->subject('Confirmer votre email');
  59.             $email->htmlTemplate('emails/confirm-account.html.twig');
  60.             $email->context(['signedUrl' => $signatureComponents->getSignedUrl(), 'user' => $user]);
  61.             $this->mailer->send($email);
  62.             // do anything else you need here, like send an email
  63.             $this->addFlash('success''Votre inscription a bien été enregistrée. Vous devez valider votre email avant de vous connecter. Si besoin, pensez à consulter vos spams.');
  64.             return $this->redirectToRoute('app_login');
  65.         }
  66.         return $this->render('security/register.html.twig', [
  67.             'registrationForm' => $form->createView(),
  68.         ]);
  69.     }
  70.     #[Route('/verify'name'registration_confirmation_route')]
  71.     public function verifyUserEmail(Request $requestUserRepository $userRepositoryEntityManagerInterface $entityManager): Response
  72.     {
  73.         if ($this->getUser()) {
  74.             return $this->redirectToRoute('home');
  75.         }
  76.         $id $request->get('id'); // retrieve the user id from the url
  77.         // Verify the user id exists and is not null
  78.         if (null === $id) {
  79.             return $this->redirectToRoute('home');
  80.         }
  81.         $user $userRepository->find($id);
  82.         // Ensure the user exists in persistence
  83.         if (null === $user) {
  84.             return $this->redirectToRoute('home');
  85.         }
  86.         // Do not get the User's Id or Email Address from the Request object
  87.         try {
  88.             $this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
  89.         } catch (VerifyEmailExceptionInterface $e) {
  90.             $this->addFlash('verify_email_error'$e->getReason());
  91.             return $this->render('vitrine/confirmEmailFailed.html.twig', [
  92.                 'content' => $e->getReason(),
  93.                 'id' => $user->getId()
  94.             ]);
  95.         }
  96.         // Mark your user as verified. e.g. switch a User::verified property to true
  97.         $user->setIsVerified(true);
  98.         $entityManager->persist($user);
  99.         $entityManager->flush();
  100.         $this->addFlash('success''Votre email a bien été vérifié.');
  101.         return $this->redirectToRoute('home');
  102.     }
  103.     #[Route('/resend/{id}'name'registration_resend_route')]
  104.     public function resend(User $user): Response
  105.     {
  106.         if ($this->getUser()) {
  107.             return $this->redirectToRoute('home');
  108.         }
  109.         $signatureComponents $this->verifyEmailHelper->generateSignature(
  110.             'registration_confirmation_route',
  111.             $user->getId(),
  112.             $user->getEmail(),
  113.             ['id' => $user->getId()] // add the user's id as an extra query param
  114.         );
  115.         $email = new TemplatedEmail();
  116.         $email->from(new Address('metalead@preprod.my-site-web.com''Contact Metalead'));
  117.         $email->to($user->getEmail());
  118.         $email->subject('Confirmer votre email');
  119.         $email->htmlTemplate('emails/confirm-account.html.twig');
  120.         $email->context(['signedUrl' => $signatureComponents->getSignedUrl(), 'user' => $user]);
  121.         $this->mailer->send($email);
  122.         $this->addFlash('success''Un nouveau mail pour confirmer votre compte vient de vous être envoyé');
  123.         return $this->redirectToRoute('app_login');
  124.     }
  125. }