RegistrationController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  17. use FOS\UserBundle\Model\UserInterface;
  18. /**
  19. * Controller managing the registration
  20. *
  21. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  22. * @author Christophe Coevoet <stof@notk.org>
  23. */
  24. class RegistrationController extends ContainerAware
  25. {
  26. public function registerAction()
  27. {
  28. $form = $this->container->get('fos_user.registration.form');
  29. $formHandler = $this->container->get('fos_user.registration.form.handler');
  30. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  31. $process = $formHandler->process($confirmationEnabled);
  32. if ($process) {
  33. $user = $form->getData();
  34. $authUser = false;
  35. if ($confirmationEnabled) {
  36. $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  37. $route = 'fos_user_registration_check_email';
  38. } else {
  39. $authUser = true;
  40. $route = 'fos_user_registration_confirmed';
  41. }
  42. $this->setFlash('fos_user_success', 'registration.flash.user_created');
  43. $url = $this->container->get('router')->generate($route);
  44. $response = new RedirectResponse($url);
  45. if ($authUser) {
  46. $this->authenticateUser($user, $response);
  47. }
  48. return $response;
  49. }
  50. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
  51. 'form' => $form->createView(),
  52. ));
  53. }
  54. /**
  55. * Tell the user to check his email provider
  56. */
  57. public function checkEmailAction()
  58. {
  59. $email = $this->container->get('session')->get('fos_user_send_confirmation_email/email');
  60. $this->container->get('session')->remove('fos_user_send_confirmation_email/email');
  61. $user = $this->container->get('fos_user.user_manager')->findUserByEmail($email);
  62. if (null === $user) {
  63. throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
  64. }
  65. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:checkEmail.html.'.$this->getEngine(), array(
  66. 'user' => $user,
  67. ));
  68. }
  69. /**
  70. * Receive the confirmation token from user email provider, login the user
  71. */
  72. public function confirmAction($token)
  73. {
  74. $user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token);
  75. if (null === $user) {
  76. throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
  77. }
  78. $user->setConfirmationToken(null);
  79. $user->setEnabled(true);
  80. $user->setLastLogin(new \DateTime());
  81. $this->container->get('fos_user.user_manager')->updateUser($user);
  82. $response = new RedirectResponse($this->container->get('router')->generate('fos_user_registration_confirmed'));
  83. $this->authenticateUser($user, $response);
  84. return $response;
  85. }
  86. /**
  87. * Tell the user his account is now confirmed
  88. */
  89. public function confirmedAction()
  90. {
  91. $user = $this->container->get('security.context')->getToken()->getUser();
  92. if (!is_object($user) || !$user instanceof UserInterface) {
  93. throw new AccessDeniedException('This user does not have access to this section.');
  94. }
  95. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:confirmed.html.'.$this->getEngine(), array(
  96. 'user' => $user,
  97. ));
  98. }
  99. /**
  100. * Authenticate a user with Symfony Security
  101. *
  102. * @param \FOS\UserBundle\Model\UserInterface $user
  103. * @param \Symfony\Component\HttpFoundation\Response $response
  104. */
  105. protected function authenticateUser(UserInterface $user, Response $response)
  106. {
  107. try {
  108. $this->container->get('fos_user.security.login_manager')->loginUser(
  109. $this->container->getParameter('fos_user.firewall_name'),
  110. $user,
  111. $response);
  112. } catch (AccountStatusException $ex) {
  113. // We simply do not authenticate users which do not pass the user
  114. // checker (not enabled, expired, etc.).
  115. }
  116. }
  117. /**
  118. * @param string $action
  119. * @param string $value
  120. */
  121. protected function setFlash($action, $value)
  122. {
  123. $this->container->get('session')->getFlashBag()->set($action, $value);
  124. }
  125. protected function getEngine()
  126. {
  127. return $this->container->getParameter('fos_user.template.engine');
  128. }
  129. }