src/Controller/RegistrationController.php line 173

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\PrivateUser;
  4. use App\Entity\ProUser;
  5. use App\Entity\User;
  6. use App\Form\Serializer\FormErrorsSerializer;
  7. use App\Form\Type\Localized\PortugalRegistrationFormType;
  8. use App\Form\Type\Localized\SpainRegistrationFormType;
  9. use App\Form\Type\PrivateRegistrationStep2FormType;
  10. use App\Form\Type\ProRegistrationStep1FormType;
  11. use App\Form\Type\ProRegistrationStep2FormType;
  12. use App\Form\Type\ProRegistrationStep3FormType;
  13. use App\Mailer\TranslatableMailer;
  14. use App\Service\PrivateUserRegistrationSession;
  15. use App\Service\ProUserRegistrationSession;
  16. use App\Twig\Extension\CountryExtension;
  17. use FOS\UserBundle\Event\FilterUserResponseEvent;
  18. use FOS\UserBundle\Event\FormEvent;
  19. use FOS\UserBundle\Event\GetResponseUserEvent;
  20. use FOS\UserBundle\Event\UserEvent;
  21. use FOS\UserBundle\Form\Factory\FactoryInterface;
  22. use FOS\UserBundle\FOSUserEvents;
  23. use FOS\UserBundle\Model\UserManagerInterface;
  24. use Psr\Log\LoggerInterface;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface as ObsoleteEventDispatcherInterface;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\Response;
  31. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  32. use Symfony\Component\Routing\RouterInterface;
  33. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  34. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  35. use Symfony\Contracts\Translation\TranslatorInterface;
  36. class RegistrationController extends AbstractController
  37. {
  38.     public function __construct(
  39.         protected EventDispatcherInterface $eventDispatcher,
  40.         ObsoleteEventDispatcherInterface $obsoleteEventDispatcher,
  41.         protected FactoryInterface $formFactory,
  42.         protected UserManagerInterface $userManager,
  43.         protected TokenStorageInterface $tokenStorage,
  44.         protected FormErrorsSerializer $formErrorsSerializer,
  45.         protected TranslatableMailer $translatableMailer,
  46.         protected TranslatorInterface $translator,
  47.         protected LoggerInterface $logger)
  48.     {
  49.     }
  50.     public function registerAction(Request $request): Response
  51.     {
  52.         $user $this->userManager->createUser();
  53.         $user->setEnabled(true);
  54.         $event = new GetResponseUserEvent($user$request);
  55.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  56.         if (null !== $event->getResponse()) {
  57.             return $event->getResponse();
  58.         }
  59.         $form $this->formFactory->createForm();
  60.         $form->setData($user);
  61.         $form->handleRequest($request);
  62.         if ($form->isSubmitted()) {
  63.             if ($form->isValid()) {
  64.                 $event = new FormEvent($form$request);
  65.                 $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  66.                 $this->userManager->updateUser($user);
  67.                 if (null === $response $event->getResponse()) {
  68.                     $url $this->generateUrl('fos_user_registration_confirmed');
  69.                     $response = new RedirectResponse($url);
  70.                 }
  71.                 $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  72.                 return $response;
  73.             }
  74.             $event = new FormEvent($form$request);
  75.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_FAILURE);
  76.             if (null !== $response $event->getResponse()) {
  77.                 return $response;
  78.             }
  79.         }
  80.         return $this->render('@FOSUser/Registration/register.html.twig', [
  81.             'form' => $form->createView(),
  82.         ]);
  83.     }
  84.     /**
  85.      * The users from Portugal and Spain have to perform another step to register.
  86.      *
  87.      * @see RegistrationController::registerPortugalAction
  88.      * @see RegistrationController::registerSpainAction()
  89.      */
  90.     public function registerStepAction(
  91.         Request $request,
  92.         RouterInterface $router,
  93.         PrivateUserRegistrationSession $userRegistrationSession,
  94.         int $step
  95.     ): Response {
  96.         if ($step || $step 4) {
  97.             throw new NotFoundHttpException();
  98.         }
  99.         if (=== $step) {
  100.             /** @var PrivateUser $user */
  101.             $user $this->userManager->createUser(PrivateUser::class);
  102.             $form $this->createForm(
  103.                 PrivateRegistrationStep2FormType::class,
  104.                 $user
  105.             );
  106.             $form->handleRequest($request);
  107.             if ($form->isSubmitted() && $form->isValid()) {
  108.                 /** @var PrivateUser $user */
  109.                 $user $form->getData();
  110.                 // special cases: registration for Spain or Portugal require more data,
  111.                 // users are sent to another form
  112.                 if (
  113.                     in_array(strtolower((string) $user->getCountry()), [CountryExtension::COUNTRY_PORTUGALCountryExtension::COUNTRY_SPAIN], true)
  114.                 ) {
  115.                     // create or overwrite the data in session, e.g. if the user choose the wrong country
  116.                     // and get back on this step
  117.                     $userRegistrationSession->save($user);
  118.                     // redirect the user to the corresponding country
  119.                     if (strtoupper(CountryExtension::COUNTRY_PORTUGAL) === $user->getCountry()) {
  120.                         $path $router->generate('fos_user_registration_register_portugal');
  121.                     } else {
  122.                         $path $router->generate('fos_user_registration_register_spain');
  123.                     }
  124.                     return new RedirectResponse($path);
  125.                 }
  126.                 $event = new FormEvent($form$request);
  127.                 $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  128.                 $this->userManager->updateUser($user);
  129.                 /** @var RedirectResponse $response */
  130.                 $response $event->getResponse();
  131.                 $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  132.                 return $response;
  133.             }
  134.             return $this->render('frontend/registration/step_2.html.twig', [
  135.                 'form' => $form->createView(),
  136.             ]);
  137.         } elseif (=== $step) {
  138.             return $this->render('frontend/registration/step_3.html.twig');
  139.         } elseif (=== $step) {
  140.             return $this->render('frontend/registration/step_4.html.twig');
  141.         }
  142.         return $this->render('frontend/registration/step_1.html.twig');
  143.     }
  144.     public function registerProAction(Request $requestint $step)
  145.     {
  146.         if ($step || $step 3) {
  147.             throw new NotFoundHttpException();
  148.         }
  149.         $registrationManager = new ProUserRegistrationSession($request->getSession(), $this->userManager$this->logger);
  150.         $user $registrationManager->load();
  151.         /* @var User $user */
  152.         if (=== $step && !$registrationManager->exists()) {
  153.             $this->eventDispatcher->dispatch(new UserEvent($user$request), FOSUserEvents::REGISTRATION_INITIALIZE);
  154.         }
  155.         $form $this->createForm(
  156.             [
  157.                 => ProRegistrationStep1FormType::class,
  158.                 => ProRegistrationStep2FormType::class,
  159.                 => ProRegistrationStep3FormType::class,
  160.             ][$step],
  161.             $user,
  162.             === $step ? ['locale' => $request->getLocale()] : []
  163.         );
  164.         $form->handleRequest($request);
  165.         if ($form->isSubmitted() && $form->isValid()) {
  166.             $user $form->getData();
  167.             if ($step 3) {
  168.                 // Don't put attachments in session, we don't need them and it would break the registration manager
  169.                 $registrationManager->save($user);
  170.                 return new RedirectResponse($this->generateUrl('fos_user_registration_register_pro_step', ['step' => $step 1]));
  171.             }
  172.             $event = new FormEvent($form$request);
  173.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  174.             $this->userManager->updateUser($user);
  175.             /** @var RedirectResponse $response */
  176.             if (null === $response $event->getResponse()) {
  177.                 $url $this->generateUrl('fos_user_registration_confirmed');
  178.                 $response = new RedirectResponse($url);
  179.             }
  180.             $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  181.             $registrationManager->remove();
  182.             if ($request->isXmlHttpRequest()) {
  183.                 return new JsonResponse([
  184.                     'success' => true,
  185.                     'url' => $event->getResponse()->getTargetUrl(),
  186.                 ]);
  187.             }
  188.             return $response;
  189.         }
  190.         return $this->render(
  191.             '/Registration/pro/step_'.$step.'.html.twig',
  192.             [
  193.                 'form' => $form->createView(),
  194.             ]
  195.         );
  196.     }
  197.     public function registerPortugalAction(
  198.         Request $request,
  199.         PrivateUserRegistrationSession $userRegistrationSession
  200.     ) {
  201.         if (!$userRegistrationSession->exists()) {
  202.             throw new NotFoundHttpException('Pas d’inscription en cours');
  203.         }
  204.         /** @var PrivateUser $user */
  205.         $user $userRegistrationSession->load();
  206.         $user->setEnabled(false);
  207.         $user->setNeedsTransaction(false);
  208.         $user->setLanguage('PT');
  209.         $this->eventDispatcher->dispatch(new UserEvent($user$request), FOSUserEvents::REGISTRATION_INITIALIZE);
  210.         $form $this->createForm(PortugalRegistrationFormType::class, $user);
  211.         $form->handleRequest($request);
  212.         if ($form->isSubmitted() && $form->isValid()) {
  213.             $event = new FormEvent($form$request);
  214.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  215.             $this->userManager->updateUser($user);
  216.             $userRegistrationSession->remove();
  217.             $response $event->getResponse();
  218.             $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  219.             return $response;
  220.         }
  221.         return $this->render(
  222.             '/Registration/register_portugal.html.twig',
  223.             [
  224.                 'form' => $form->createView(),
  225.             ]
  226.         );
  227.     }
  228.     public function registerSpainAction(
  229.         Request $request,
  230.         PrivateUserRegistrationSession $userRegistrationSession
  231.     ) {
  232.         if (!$userRegistrationSession->exists()) {
  233.             throw new NotFoundHttpException('Pas d’inscription en cours');
  234.         }
  235.         /** @var PrivateUser $user */
  236.         $user $userRegistrationSession->load();
  237.         $user->setNeedsTransaction(false);
  238.         $user->setLanguage('ES');
  239.         $this->eventDispatcher->dispatch(new UserEvent($user$request), FOSUserEvents::REGISTRATION_INITIALIZE);
  240.         $form $this->createForm(SpainRegistrationFormType::class, $user);
  241.         $form->handleRequest($request);
  242.         if ($form->isSubmitted() && $form->isValid()) {
  243.             $event = new FormEvent($form$request);
  244.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  245.             $user->setEnabled(false);
  246.             $this->userManager->updateUser($user);
  247.             $userRegistrationSession->remove();
  248.             $response $event->getResponse();
  249.             $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  250.             return $response;
  251.         }
  252.         return $this->render(
  253.             '/Registration/register_spain.html.twig',
  254.             [
  255.                 'form' => $form->createView(),
  256.             ]
  257.         );
  258.     }
  259.     /**
  260.      * Receive the confirmation token from user email provider.
  261.      *
  262.      * @return RedirectResponse|Response|null
  263.      */
  264.     public function confirmAction(Request $request$token): Response
  265.     {
  266.         $user $this->userManager->findUserByConfirmationToken($token);
  267.         if (null === $user) {
  268.             return new RedirectResponse($this->generateUrl('fos_user_security_login'));
  269.         }
  270.         $user->setConfirmationToken(null);
  271.         $user->setEnabled(true);
  272.         $this->logger->info(sprintf('%s:%s User %s enabled'__CLASS____LINE__$user->getId()));
  273.         $event = new GetResponseUserEvent($user$request);
  274.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_CONFIRM);
  275.         $this->userManager->updateUser($user);
  276.         if (null === $response $event->getResponse()) {
  277.             $url $this->generateUrl(sprintf('fos_user_registration_confirmed%s'$user instanceof ProUser '_pro' ''));
  278.             $response = new RedirectResponse($url);
  279.         }
  280.         // An email will be sent by the FOSUserEvents::REGISTRATION_CONFIRM event, don't send another one
  281.         if ($user && 'PT' !== $user->getCountry()) {
  282.             $this->sendValidationMessage($user);
  283.         }
  284.         $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_CONFIRMED);
  285.         return $response;
  286.     }
  287.     // Tell the user to check his email provider.
  288.     public function checkEmailAction(Request $request): Response
  289.     {
  290.         $email $request->getSession()->get('fos_user_send_confirmation_email/email');
  291.         $request->getSession()->remove('fos_user_send_confirmation_email/email');
  292.         $user $this->userManager->findUserByEmail($email);
  293.         if (null === $user) {
  294.             throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist'$email));
  295.         }
  296.         return $this->render('@FOSUser/Registration/check_email.html.twig', [
  297.             'user' => $user,
  298.         ]);
  299.     }
  300.     // Tell the user his account is now confirmed and send him an email.
  301.     public function confirmedAction(Request $request): Response
  302.     {
  303.         $url $this->generateUrl('fos_user_security_login');
  304.         $request->getSession()->getFlashBag()->add('success'$this->translator->trans('security.flash.account_created'));
  305.         return new RedirectResponse($url);
  306.     }
  307.     // Tell the user his account is now confirmed and send him an email.
  308.     public function confirmedProAction()
  309.     {
  310.         return $this->render('/Registration/confirmed_pro.html.twig');
  311.     }
  312.     // Display verification page
  313.     public function waitingVerificationAction()
  314.     {
  315.         return $this->render('/Registration/wait_verification.html.twig');
  316.     }
  317.     // Pro users need to wait account validation by VPAuto salesperson.
  318.     public function waitingValidationAction()
  319.     {
  320.         return $this->render('/Registration/wait.html.twig');
  321.     }
  322.     private function sendValidationMessage($user): void
  323.     {
  324.         $this->translatableMailer->send(
  325.             $user,
  326.             '/Mail/welcome.html.twig',
  327.             []
  328.         );
  329.     }
  330. }