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 <thibault.duplessis@gmail.com>
  31.  * @author Christophe Coevoet <stof@notk.org>
  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.         
  54.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  55.         
  56.         if (null !== $event->getResponse()) {
  57.             return $event->getResponse();
  58.         }
  59.         $form $this->formFactory->createForm();
  60.         $form->setData($user);
  61.         
  62.         $form->handleRequest($request);
  63. //dump($request,$event); die;
  64.         if ($form->isSubmitted()) {
  65.             
  66.             if ($form->isValid()) {
  67.                 $event = new FormEvent($form$request);
  68.                 $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  69.                 $this->userManager->updateUser($user);
  70.                 if (null === $response $event->getResponse()) {
  71.                     $url $this->generateUrl('fos_user_registration_confirmed');
  72.                     $response = new RedirectResponse($url);
  73.                 }
  74.                 $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  75.                 return $response;
  76.             }
  77.             $event = new FormEvent($form$request);
  78.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_FAILURE);
  79.             if (null !== $response $event->getResponse()) {
  80.                 return $response;
  81.             }
  82.         }
  83.         return $this->render('@FOSUser/Registration/register.html.twig', [
  84.             'form' => $form->createView(),
  85.         ]);
  86.     }
  87.     /**
  88.      * Tell the user to check their email provider.
  89.      */
  90.     public function checkEmailAction(Request $request): Response
  91.     {
  92.         $email $request->getSession()->get('fos_user_send_confirmation_email/email');
  93.         if (empty($email)) {
  94.             return new RedirectResponse($this->generateUrl('fos_user_registration_register'));
  95.         }
  96.         $request->getSession()->remove('fos_user_send_confirmation_email/email');
  97.         $user $this->userManager->findUserByEmail($email);
  98.         if (null === $user) {
  99.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  100.         }
  101.         return $this->render('@FOSUser/Registration/check_email.html.twig', [
  102.             'user' => $user,
  103.         ]);
  104.     }
  105.     /**
  106.      * Receive the confirmation token from user email provider, login the user.
  107.      *
  108.      * @param string $token
  109.      */
  110.     public function confirmAction(Request $request$token): Response
  111.     {
  112.         $userManager $this->userManager;
  113.         $user $userManager->findUserByConfirmationToken($token);
  114.         if (null === $user) {
  115.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  116.         }
  117.         $user->setConfirmationToken(null);
  118.         $user->setEnabled(true);
  119.         $event = new GetResponseUserEvent($user$request);
  120.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_CONFIRM);
  121.         $userManager->updateUser($user);
  122.         if (null === $response $event->getResponse()) {
  123.             $url $this->generateUrl('fos_user_registration_confirmed');
  124.             $response = new RedirectResponse($url);
  125.         }
  126.         $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user$request$response), FOSUserEvents::REGISTRATION_CONFIRMED);
  127.         return $response;
  128.     }
  129.     /**
  130.      * Tell the user his account is now confirmed.
  131.      */
  132.     public function confirmedAction(Request $request): Response
  133.     {
  134.         $user $this->getUser();
  135.         if (!is_object($user) || !$user instanceof UserInterface) {
  136.             throw new AccessDeniedException('This user does not have access to this section.');
  137.         }
  138.         return $this->render('@FOSUser/Registration/confirmed.html.twig', [
  139.             'user' => $user,
  140.             'targetUrl' => $this->getTargetUrlFromSession($request->getSession()),
  141.         ]);
  142.     }
  143.     private function getTargetUrlFromSession(SessionInterface $session): ?string
  144.     {
  145.         $token $this->tokenStorage->getToken();
  146.         if (null === $token) {
  147.             return null;
  148.         }
  149.         if (method_exists($token'getFirewallName')) {
  150.             $firewallName $token->getFirewallName();
  151.         } elseif (method_exists($token'getProviderKey')) {
  152.             // BC with Symfony 5.x
  153.             $firewallName $token->getProviderKey();
  154.         } else {
  155.             return null;
  156.         }
  157.         $key sprintf('_security.%s.target_path'$firewallName);
  158.         if ($session->has($key)) {
  159.             return $session->get($key);
  160.         }
  161.         return null;
  162.     }
  163. }