src/Controller/RegistrationController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Mailer\UserMailer;
  6. use App\Security\AppMainAuthenticator;
  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 Symfony\Contracts\Translation\TranslatorInterface;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. class RegistrationController extends AbstractController
  17. {
  18.     public function __construct(
  19.         private EmailVerifier $emailVerifier,
  20.         private EntityManagerInterface $entityManager,
  21.         private UserMailer $userMailer
  22.     ) {
  23.     }
  24.     #[Route('/register'name'app_register')]
  25.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasher): Response
  26.     {
  27.         if ($this->getUser()) {
  28.             return $this->redirectToRoute('app_home');
  29.         }
  30.         $user = new User();
  31.         $form $this->createForm(RegistrationFormType::class, $user);
  32.         $form->handleRequest($request);
  33.         if ($form->isSubmitted() && $form->isValid()) {
  34.             // encode the plain password
  35.             $user->setPassword(
  36.                 $userPasswordHasher->hashPassword(
  37.                     $user,
  38.                     $form->get('plainPassword')->getData()
  39.                 )
  40.             );
  41.             $informationForModeration = [];
  42.             foreach (RegistrationFormType::FIELD_EQUIPMENT_NAMES as $field) {
  43.                 $informationForModeration[$field] = $form->get($field)->getData();
  44.             }
  45.             $user
  46.                 ->setRoles([User::ROLE_CLIENT])
  47.                 ->setInformationForModeration($informationForModeration)
  48.                 // if user sign up from form register
  49.                 // Don't need to force user changes password
  50.                 ->setForceChangePassword(false);
  51.             $this->entityManager->persist($user);
  52.             $this->entityManager->flush();
  53.             // generate a signed url and email it to the user
  54.             $isSentEmail $this->userMailer->sendVerifyEmail($user'registration');
  55.             if ($isSentEmail) {
  56.                 $user->setEmailSentAt(new \DateTimeImmutable());
  57.                 $this->entityManager->persist($user);
  58.                 $this->entityManager->flush();
  59.                 $this->addFlash('success''User.Message.EmailValidation');
  60.             }
  61.             return $this->redirectToRoute(AppMainAuthenticator::LOGIN_ROUTE);
  62.         }
  63.         return $this->render('registration/register.html.twig', [
  64.             'registrationForm' => $form->createView(),
  65.         ]);
  66.     }
  67.     #[Route('/verify/email'name'app_verify_email')]
  68.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  69.     {
  70.         $path $request->query->get('path');
  71.         if ($this->getUser()) {
  72.             return $this->redirectToRoute('app_home');
  73.         }
  74.         $id $request->get('id');
  75.         // Verify the user id exists and is not null
  76.         if (null == $id) {
  77.             return $this->redirectToRoute('app_home');
  78.         }
  79.         $user $this->entityManager->getRepository(User::class)->find($id);
  80.         // Ensure the user exists in persistence
  81.         if (null === $user) {
  82.             return $this->redirectToRoute('app_home');
  83.         }
  84.         // validate email confirmation link, sets User::isVerified=true and persists
  85.         try {
  86.             $this->emailVerifier->handleEmailConfirmation($request$user);
  87.         } catch (VerifyEmailExceptionInterface $exception) {
  88.             $this->addFlash('danger'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  89.             $request->getSession()->set('non_verified_email'$user->getEmail());
  90.             return $this->redirectToRoute('app_verify_resend_email');
  91.         }
  92.         $messageEnd 'employeeCompany' === $path 'EmployeeCompany' 'Default';
  93.         $this->addFlash('success''User.Message.EmailIsVerified.'.$messageEnd);
  94.         return $this->redirectToRoute(AppMainAuthenticator::LOGIN_ROUTE);
  95.     }
  96.     #[Route('/verify/resend'name'app_verify_resend_email'methods: ['GET''POST'])]
  97.     public function resendVerifyEmail(Request $request)
  98.     {
  99.         if ($this->getUser()) {
  100.             return $this->redirectToRoute('app_home');
  101.         }
  102.         if ('POST' === $request->getMethod()) {
  103.             $userEmail $request->getSession()->get('non_verified_email');
  104.             if ($userEmail) {
  105.                 $user $this->entityManager->getRepository(User::class)->findOneBy([
  106.                     'email' => $userEmail,
  107.                 ]);
  108.                 // generate a signed url and email it to the user
  109.                 $isSentEmail $this->userMailer->sendVerifyEmail($user'registration');
  110.                 if ($isSentEmail) {
  111.                     $user->setEmailSentAt(new \DateTimeImmutable());
  112.                     $this->entityManager->persist($user);
  113.                     $this->entityManager->flush();
  114.                     $this->addFlash('success''User.Message.EmailValidation');
  115.                 }
  116.                 return $this->redirectToRoute(AppMainAuthenticator::LOGIN_ROUTE);
  117.             }
  118.         }
  119.         return $this->render('registration/resend_verify_email.html.twig');
  120.     }
  121. }