vendor/friendsofsymfony/user-bundle/src/Controller/RegistrationController.php line 97

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\Controller;
  11. use FOS\UserBundle\CompatibilityUtil;
  12. use FOS\UserBundle\Event\FilterUserResponseEvent;
  13. use FOS\UserBundle\Event\FormEvent;
  14. use FOS\UserBundle\Event\GetResponseUserEvent;
  15. use FOS\UserBundle\Form\Factory\FactoryInterface;
  16. use FOS\UserBundle\FOSUserEvents;
  17. use FOS\UserBundle\Model\UserInterface;
  18. use FOS\UserBundle\Model\UserManagerInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  25. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  26. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  27. /**
  28.  * Controller managing the registration.
  29.  *
  30.  * @author Thibault Duplessis <[email protected]>
  31.  * @author Christophe Coevoet <[email protected]>
  32.  *
  33.  * @final
  34.  */
  35. class RegistrationController extends AbstractController
  36. {
  37.     private $eventDispatcher;
  38.     private $formFactory;
  39.     private $userManager;
  40.     private $tokenStorage;
  41.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManagerTokenStorageInterface $tokenStorage)
  42.     {
  43.         $this->eventDispatcher CompatibilityUtil::upgradeEventDispatcher($eventDispatcher);
  44.         $this->formFactory $formFactory;
  45.         $this->userManager $userManager;
  46.         $this->tokenStorage $tokenStorage;
  47.     }
  48.     public function registerAction(Request $request): Response
  49.     {
  50.         $user $this->userManager->createUser();
  51.         $user->setEnabled(true);
  52.         $event = new GetResponseUserEvent($user$request);
  53.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  54.         if (null !== $event->getResponse()) {
  55.             return $event->getResponse();
  56.         }
  57.         $form $this->formFactory->createForm();
  58.         $form->setData($user);
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted()) {
  61.             if ($form->isValid()) {
  62.                 $event = new FormEvent($form$request);
  63.                 $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  64.                 $this->userManager->updateUser($user);
  65.                 if (null === $response $event->getResponse()) {
  66.                     $url $this->generateUrl('fos_user_registration_confirmed');
  67.                     $response = new RedirectResponse($url);
  68.                 }
  69.                 $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  70.                 return $response;
  71.             }
  72.             $event = new FormEvent($form$request);
  73.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_FAILURE);
  74.             if (null !== $response $event->getResponse()) {
  75.                 return $response;
  76.             }
  77.         }
  78.         return $this->render('@FOSUser/Registration/register.html.twig', [
  79.             'form' => $form->createView(),
  80.         ]);
  81.     }
  82.     /**
  83.      * Tell the user to check their email provider.
  84.      */
  85.     public function checkEmailAction(Request $request): Response
  86.     {
  87.         $email $request->getSession()->get('fos_user_send_confirmation_email/email');
  88.         if (empty($email)) {
  89.             return new RedirectResponse($this->generateUrl('fos_user_registration_register'));
  90.         }
  91.         $request->getSession()->remove('fos_user_send_confirmation_email/email');
  92.         $user $this->userManager->findUserByEmail($email);
  93.         if (null === $user) {
  94.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  95.         }
  96.         return $this->render('@FOSUser/Registration/check_email.html.twig', [
  97.             'user' => $user,
  98.         ]);
  99.     }
  100.     /**
  101.      * Receive the confirmation token from user email provider, login the user.
  102.      *
  103.      * @param string $token
  104.      */
  105.     public function confirmAction(Request $request$token): Response
  106.     {
  107.         $userManager $this->userManager;
  108.         $user $userManager->findUserByConfirmationToken($token);
  109.         if (null === $user) {
  110.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  111.         }
  112.         $user->setConfirmationToken(null);
  113.         $user->setEnabled(true);
  114.         $event = new GetResponseUserEvent($user$request);
  115.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_CONFIRM);
  116.         $userManager->updateUser($user);
  117.         if (null === $response $event->getResponse()) {
  118.             $url $this->generateUrl('fos_user_registration_confirmed');
  119.             $response = new RedirectResponse($url);
  120.         }
  121.         $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_CONFIRMED);
  122.         return $response;
  123.     }
  124.     /**
  125.      * Tell the user his account is now confirmed.
  126.      */
  127.     public function confirmedAction(Request $request): Response
  128.     {
  129.         $user $this->getUser();
  130.         if (!is_object($user) || !$user instanceof UserInterface) {
  131.             throw new AccessDeniedException('This user does not have access to this section.');
  132.         }
  133.         return $this->render('@FOSUser/Registration/confirmed.html.twig', [
  134.             'user' => $user,
  135.             'targetUrl' => $this->getTargetUrlFromSession($request->getSession()),
  136.         ]);
  137.     }
  138.     private function getTargetUrlFromSession(SessionInterface $session): ?string
  139.     {
  140.         $token $this->tokenStorage->getToken();
  141.         if (null === $token) {
  142.             return null;
  143.         }
  144.         if (method_exists($token'getFirewallName')) {
  145.             $firewallName $token->getFirewallName();
  146.         } elseif (method_exists($token'getProviderKey')) {
  147.             // BC with Symfony 5.x
  148.             $firewallName $token->getProviderKey();
  149.         } else {
  150.             return null;
  151.         }
  152.         $key sprintf('_security.%s.target_path'$firewallName);
  153.         if ($session->has($key)) {
  154.             return $session->get($key);
  155.         }
  156.         return null;
  157.     }
  158. }