vendor/friendsofsymfony/user-bundle/Controller/RegistrationController.php line 62

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