src/Controller/RegistrationController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Utils\Toast;
  5. use App\Service\ToastService;
  6. use App\Security\EmailVerifier;
  7. use App\Form\RegistrationFormType;
  8. use App\Repository\UserRepository;
  9. use Symfony\Component\Mime\Address;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  18. class RegistrationController extends AbstractController
  19. {
  20.     private EmailVerifier $emailVerifier;
  21.     /**
  22.      * @var ToastService
  23.      */
  24.     private $toastService;
  25.     public function __construct(EmailVerifier $emailVerifierToastService $toastService)
  26.     {
  27.         $this->emailVerifier $emailVerifier;
  28.         $this->toastService $toastService;
  29.     }
  30.     /**
  31.      * @Route("register", name="app_register", options = { "expose" = true })
  32.      */
  33.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  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.             $user->setStatus('Inactif');
  47.             $user->setRoles(['ROLE_USER']);
  48.             $user->setIsVerified(false);
  49.             $entityManager->persist($user);
  50.             $entityManager->flush();
  51.             // generate a signed url and email it to the user
  52.             $this->emailVerifier->sendEmailConfirmation(
  53.                 'app_verify_email',
  54.                 $user,
  55.                 (new TemplatedEmail())
  56.                     ->from(new Address($this->getParameter('mail_from'), 'Plateforme Q-Sqor'))
  57.                     ->to($user->getEmail())
  58.                     ->subject('Q-Sqor - Merci de confirmer votre adresse email')
  59.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  60.             );
  61.             // do anything else you need here, like send an email
  62.             $this->toastService->toast(Toast::TYPE_SUCCESS'Inscription réussie''Vérifier vos emails pour valider votre compte.');
  63.             return $this->redirectToRoute('app_login');
  64.         } else {
  65.             $errors '';
  66.             foreach ($form->getErrors(true) as $error) {
  67.                 $errors .= $error->getMessage() . "<br />";
  68.             }
  69.             if ($form->isSubmitted())
  70.                 $this->toastService->toast(Toast::TYPE_ERROR'Erreur lors de votre inscription'$errors);
  71.         }
  72.         return $this->render('registration/register.html.twig', [
  73.             'registrationForm' => $form->createView(),
  74.         ]);
  75.     }
  76.     /**
  77.      * @Route("/verify/email", name="app_verify_email", options = { "expose" = true })
  78.      */
  79.     public function verifyUserEmail(Request $requestUserRepository $userRepository): Response
  80.     {
  81.         $id $request->get('id');
  82.         if (null === $id) {
  83.             $this->toastService->toast(Toast::TYPE_ERROR'Erreur lors de la vérification de votre compte',  'Id manquant');
  84.             return $this->redirectToRoute('app_register');
  85.         }
  86.         $user $userRepository->find($id);
  87.         if (null === $user) {
  88.             $this->toastService->toast(Toast::TYPE_ERROR'Erreur lors de la vérification de votre compte',  'Utilisateur introuvable');
  89.             return $this->redirectToRoute('app_register');
  90.         }
  91.         // validate email confirmation link, sets User::isVerified=true and persists
  92.         try {
  93.             $this->emailVerifier->handleEmailConfirmation($request$user);
  94.         } catch (VerifyEmailExceptionInterface $exception) {
  95.             $this->toastService->toast(Toast::TYPE_ERROR'Erreur lors de la vérification de votre compte',  $exception->getReason());
  96.             return $this->redirectToRoute('app_register');
  97.         }
  98.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  99.         $this->toastService->toast(Toast::TYPE_SUCCESS'Votre email a été vérifiée.''Un administrateur va vérifier et activer voter compte.');
  100.         return $this->redirectToRoute('client_tdb');
  101.     }
  102. }