src/Controller/RegistrationController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ClientUser;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\ClientUserRepository;
  6. use App\Security\ClientAuthenticator;
  7. use App\Security\EmailVerifier;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. class RegistrationController extends AbstractController
  20. {
  21.     private $emailVerifier;
  22.     public function __construct(EmailVerifier $emailVerifier)
  23.     {
  24.         $this->emailVerifier $emailVerifier;
  25.     }
  26.     /**
  27.      * @Route("/inscription-particulier", name="app_particulier")
  28.      */
  29.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorClientAuthenticator $authenticatorEntityManagerInterface $em): Response
  30.     {
  31.         $user = new ClientUser();
  32.         $form $this->createForm(RegistrationFormType::class, $user);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             // encode the plain password
  36.             $user->setPassword(
  37.             $userPasswordHasher->hashPassword(
  38.                     $user,
  39.                     $form->get('plainPassword')->getData()
  40.                 )
  41.             );
  42.             $user->setCompte('client');
  43.             $em->persist($user);
  44.             $em->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('utilisateurs@aidemarket.com''Aidmarket Mail'))
  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('auth/inscriptionParticulier.html.twig', [
  61.             'registrationForm' => $form->createView(),
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/verify/email", name="app_verify_email")
  66.      */
  67.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorClientUserRepository $clientUserRepository): Response
  68.     {
  69.         $id $request->get('id');
  70.         if (null === $id) {
  71.             return $this->redirectToRoute('app_register');
  72.         }
  73.         $user $clientUserRepository->find($id);
  74.         if (null === $user) {
  75.             return $this->redirectToRoute('app_register');
  76.         }
  77.         // validate email confirmation link, sets User::isVerified=true and persists
  78.         try {
  79.             $this->emailVerifier->handleEmailConfirmation($request$user);
  80.         } catch (VerifyEmailExceptionInterface $exception) {
  81.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  82.             return $this->redirectToRoute('app_register');
  83.         }
  84.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  85.         $this->addFlash('success''Your email address has been verified.');
  86.         return $this->redirectToRoute('app_register');
  87.     }
  88. }