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

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 <thibault.duplessis@gmail.com>
  31.  * @author Christophe Coevoet <stof@notk.org>
  32.  */
  33. class RegistrationController extends Controller {
  34.     private $eventDispatcher;
  35.     private $formFactory;
  36.     private $userManager;
  37.     private $tokenStorage;
  38.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManagerTokenStorageInterface $tokenStorage) {
  39.         $this->eventDispatcher $eventDispatcher;
  40.         $this->formFactory $formFactory;
  41.         $this->userManager $userManager;
  42.         $this->tokenStorage $tokenStorage;
  43.     }
  44.     /**
  45.      * @param Request $request
  46.      *
  47.      * @return Response
  48.      */
  49.     public function registerAction(Request $request) {
  50.         $user $this->userManager->createUser();
  51.         $user->setEnabled(true);
  52.         $event = new GetResponseUserEvent($user$request);
  53.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE$event);
  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(FOSUserEvents::REGISTRATION_SUCCESS$event);
  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(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user$request$response));
  70.                 return $response;
  71.             }
  72.             $event = new FormEvent($form$request);
  73.             $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE$event);
  74.             if (null !== $response $event->getResponse()) {
  75.                 return $response;
  76.             }
  77.         }
  78.         return $this->render('@FOSUser/Registration/register.html.twig', array(
  79.                     'form' => $form->createView(),
  80.         ));
  81.     }
  82.     /**
  83.      * Tell the user to check their email provider.
  84.      */
  85.     public function checkEmailAction(Request $request) {
  86.         $email $request->getSession()->get('fos_user_send_confirmation_email/email');
  87.         if (empty($email)) {
  88.             return new RedirectResponse($this->generateUrl('fos_user_registration_register'));
  89.         }
  90.         $request->getSession()->remove('fos_user_send_confirmation_email/email');
  91.         $user $this->userManager->findUserByEmail($email);
  92.         if (null === $user) {
  93.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  94.         }
  95.         return $this->render('@FOSUser/Registration/check_email.html.twig', array(
  96.                     'user' => $user,
  97.         ));
  98.     }
  99.     /**
  100.      * Receive the confirmation token from user email provider, login the user.
  101.      *
  102.      * @param Request $request
  103.      * @param string  $token
  104.      *
  105.      * @return Response
  106.      */
  107.     public function confirmAction(Request $request$token) {
  108.         $userManager $this->userManager;
  109.         $user $userManager->findUserByConfirmationToken($token);
  110.         if (null === $user) {
  111.             throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist'$token));
  112.         }
  113.         $user->setConfirmationToken(null);
  114.         $user->setEnabled(true);
  115.         $event = new GetResponseUserEvent($user$request);
  116.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRM$event);
  117.         $userManager->updateUser($user);
  118.         if (null === $response $event->getResponse()) {
  119.             $url $this->generateUrl('fos_user_registration_confirmed');
  120.             $response = new RedirectResponse($url);
  121.         }
  122.         $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user$request$response));
  123.         return $response;
  124.     }
  125.     /**
  126.      * Tell the user his account is now confirmed.
  127.      */
  128.     public function confirmedAction(Request $request) {
  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', array(
  134.                     'user' => $user,
  135.                     'targetUrl' => $this->getTargetUrlFromSession($request->getSession()),
  136.         ));
  137.     }
  138.     /**
  139.      * @return string|null
  140.      */
  141.     private function getTargetUrlFromSession(SessionInterface $session) {
  142.         $key sprintf('_security.%s.target_path'$this->tokenStorage->getToken()->getProviderKey());
  143.         if ($session->has($key)) {
  144.             return $session->get($key);
  145.         }
  146.         return null;
  147.     }
  148. }