RegistrationFOSUser1Controller.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\UserBundle\Controller;
  11. use FOS\UserBundle\Model\UserInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  17. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  18. /**
  19. * This class is inspired from the FOS RegistrationController.
  20. *
  21. * @author Hugo Briand <briand@ekino.com>
  22. */
  23. class RegistrationFOSUser1Controller extends Controller
  24. {
  25. /**
  26. * @return RedirectResponse|Response
  27. */
  28. public function registerAction()
  29. {
  30. $user = $this->getUser();
  31. if ($user instanceof UserInterface) {
  32. $this->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
  33. return $this->redirect($this->generateUrl('sonata_user_profile_show'));
  34. }
  35. $form = $this->get('sonata.user.registration.form');
  36. $formHandler = $this->get('sonata.user.registration.form.handler');
  37. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  38. $process = $formHandler->process($confirmationEnabled);
  39. if ($process) {
  40. $user = $form->getData();
  41. $authUser = false;
  42. if ($confirmationEnabled) {
  43. $this->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  44. $url = $this->generateUrl('fos_user_registration_check_email');
  45. } else {
  46. $authUser = true;
  47. $route = $this->get('session')->get('sonata_basket_delivery_redirect');
  48. if (null !== $route) {
  49. // NEXT_MAJOR: remove the if block
  50. @trigger_error(<<<'EOT'
  51. Setting a redirect url in the sonata_basket_delivery_redirect session variable
  52. is deprecated since 3.2 and will no longer result in a redirection to this url in 4.0.
  53. EOT
  54. , E_USER_DEPRECATED);
  55. $this->get('session')->remove('sonata_basket_delivery_redirect');
  56. $url = $this->generateUrl($route);
  57. } else {
  58. $url = $this->get('session')->get('sonata_user_redirect_url');
  59. }
  60. }
  61. if (!$url) {
  62. $url = $this->generateUrl('sonata_user_profile_show');
  63. }
  64. $this->setFlash('fos_user_success', 'registration.flash.user_created');
  65. $response = $this->redirect($url);
  66. if ($authUser) {
  67. $this->authenticateUser($user, $response);
  68. }
  69. return $response;
  70. }
  71. // NEXT_MAJOR: Inject $request in the method signature instead.
  72. if ($this->has('request_stack')) {
  73. $request = $this->get('request_stack')->getCurrentRequest();
  74. } else {
  75. $request = $this->get('request');
  76. }
  77. $this->get('session')->set('sonata_user_redirect_url', $request->headers->get('referer'));
  78. return $this->render('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
  79. 'form' => $form->createView(),
  80. ));
  81. }
  82. /**
  83. * Tell the user to check his email provider.
  84. *
  85. * @return Response
  86. *
  87. * @throws NotFoundHttpException
  88. */
  89. public function checkEmailAction()
  90. {
  91. $email = $this->get('session')->get('fos_user_send_confirmation_email/email');
  92. $this->get('session')->remove('fos_user_send_confirmation_email/email');
  93. $user = $this->get('fos_user.user_manager')->findUserByEmail($email);
  94. if (null === $user) {
  95. throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
  96. }
  97. return $this->render('FOSUserBundle:Registration:checkEmail.html.'.$this->getEngine(), array(
  98. 'user' => $user,
  99. ));
  100. }
  101. /**
  102. * Receive the confirmation token from user email provider, login the user.
  103. *
  104. * @param string $token
  105. *
  106. * @return RedirectResponse
  107. *
  108. * @throws NotFoundHttpException
  109. */
  110. public function confirmAction($token)
  111. {
  112. $user = $this->get('fos_user.user_manager')->findUserByConfirmationToken($token);
  113. if (null === $user) {
  114. throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
  115. }
  116. $user->setConfirmationToken(null);
  117. $user->setEnabled(true);
  118. $user->setLastLogin(new \DateTime());
  119. $this->get('fos_user.user_manager')->updateUser($user);
  120. if ($redirectRoute = $this->container->getParameter('sonata.user.register.confirm.redirect_route')) {
  121. $response = $this->redirect($this->generateUrl(
  122. $redirectRoute,
  123. $this->container->getParameter('sonata.user.register.confirm.redirect_route_params')
  124. ));
  125. } else {
  126. $response = $this->redirect($this->generateUrl('fos_user_registration_confirmed'));
  127. }
  128. $this->authenticateUser($user, $response);
  129. return $response;
  130. }
  131. /**
  132. * Tell the user his account is now confirmed.
  133. *
  134. * @return Response
  135. *
  136. * @throws AccessDeniedException
  137. */
  138. public function confirmedAction()
  139. {
  140. $user = $this->getUser();
  141. if (!is_object($user) || !$user instanceof UserInterface) {
  142. throw new AccessDeniedException('This user does not have access to this section.');
  143. }
  144. return $this->render('FOSUserBundle:Registration:confirmed.html.'.$this->getEngine(), array(
  145. 'user' => $user,
  146. ));
  147. }
  148. /**
  149. * Authenticate a user with Symfony Security.
  150. *
  151. * @param UserInterface $user
  152. * @param Response $response
  153. */
  154. protected function authenticateUser(UserInterface $user, Response $response)
  155. {
  156. try {
  157. $this->get('fos_user.security.login_manager')->loginUser(
  158. $this->container->getParameter('fos_user.firewall_name'),
  159. $user,
  160. $response
  161. );
  162. } catch (AccountStatusException $ex) {
  163. // We simply do not authenticate users which do not pass the user
  164. // checker (not enabled, expired, etc.).
  165. }
  166. }
  167. /**
  168. * @param string $action
  169. * @param string $value
  170. */
  171. protected function setFlash($action, $value)
  172. {
  173. $this->get('session')->getFlashBag()->set($action, $value);
  174. }
  175. /**
  176. * @return string
  177. */
  178. protected function getEngine()
  179. {
  180. return $this->container->getParameter('fos_user.template.engine');
  181. }
  182. }