src/Controller/RegistrationController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\EmailVerifier;
  6. use App\Security\LoginFormAuthenticator;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mime\Address;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  18. class RegistrationController extends AbstractController
  19. {
  20.     private EmailVerifier $emailVerifier;
  21.     public function __construct(EmailVerifier $emailVerifier)
  22.     {
  23.         $this->emailVerifier $emailVerifier;
  24.     }
  25.     /**
  26.      * @Route("/creation-compte", name="app_register")
  27.      */
  28.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorLoginFormAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  29.     {
  30.         $user = new User();
  31.         $form $this->createForm(RegistrationFormType::class, $user);
  32.         $form->handleRequest($request);
  33.         if ($form->isSubmitted() && $form->isValid()) {
  34.             $roles = ["ROLE_CUSTOMER""ROLE_EMPLOYEE"];
  35.             $user->setRoles($roles);
  36.             // encode the plain password
  37.             $user->setPassword(
  38.             $userPasswordHasher->hashPassword(
  39.                     $user,
  40.                     $form->get('plainPassword')->getData()
  41.                 )
  42.             );
  43.             $entityManager->persist($user);
  44.             $entityManager->flush();
  45.             // generate a signed url and email it to the user
  46.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  47.                 (new TemplatedEmail())
  48.                     ->from(new Address('no-reply@zoebusinessplan.com''ZOE BUSINESS'))
  49.                     ->to($user->getEmail())
  50.                     ->subject('Please Confirm your Email')
  51.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  52.             );
  53.             // do anything else you need here, like send an email
  54.             return $userAuthenticator->authenticateUser(
  55.                 $user,
  56.                 $authenticator,
  57.                 $request
  58.             );
  59.         }
  60.         return $this->render('registration/register.html.twig', [
  61.             'form' => $form->createView(),
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/verify/email", name="app_verify_email")
  66.      */
  67.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  68.     {
  69.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  70.         // validate email confirmation link, sets User::isVerified=true and persists
  71.         try {
  72.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  73.         } catch (VerifyEmailExceptionInterface $exception) {
  74.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  75.             return $this->redirectToRoute('app_register');
  76.         }
  77.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  78.         $this->addFlash('success''Your email address has been verified.');
  79.         return $this->redirectToRoute('app_register');
  80.     }
  81. }