|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the FOSUserBundle package. |
|
5 * |
|
6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace FOS\UserBundle\Controller; |
|
13 |
|
14 use Symfony\Component\DependencyInjection\ContainerAware; |
|
15 use Symfony\Component\HttpFoundation\RedirectResponse; |
|
16 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
17 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
18 use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
19 use FOS\UserBundle\Model\UserInterface; |
|
20 |
|
21 /** |
|
22 * Controller managing the registration |
|
23 * |
|
24 * @author Thibault Duplessis <thibault.duplessis@gmail.com> |
|
25 * @author Christophe Coevoet <stof@notk.org> |
|
26 */ |
|
27 class RegistrationController extends ContainerAware |
|
28 { |
|
29 public function registerAction() |
|
30 { |
|
31 $form = $this->container->get('fos_user.registration.form'); |
|
32 $formHandler = $this->container->get('fos_user.registration.form.handler'); |
|
33 $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled'); |
|
34 |
|
35 $process = $formHandler->process($confirmationEnabled); |
|
36 if ($process) { |
|
37 $user = $form->getData(); |
|
38 |
|
39 if ($confirmationEnabled) { |
|
40 $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail()); |
|
41 $route = 'fos_user_registration_check_email'; |
|
42 } else { |
|
43 $this->authenticateUser($user); |
|
44 $route = 'fos_user_registration_confirmed'; |
|
45 } |
|
46 |
|
47 $this->setFlash('fos_user_success', 'registration.flash.user_created'); |
|
48 $url = $this->container->get('router')->generate($route); |
|
49 |
|
50 return new RedirectResponse($url); |
|
51 } |
|
52 |
|
53 return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array( |
|
54 'form' => $form->createView(), |
|
55 'theme' => $this->container->getParameter('fos_user.template.theme'), |
|
56 )); |
|
57 } |
|
58 |
|
59 /** |
|
60 * Tell the user to check his email provider |
|
61 */ |
|
62 public function checkEmailAction() |
|
63 { |
|
64 $email = $this->container->get('session')->get('fos_user_send_confirmation_email/email'); |
|
65 $this->container->get('session')->remove('fos_user_send_confirmation_email/email'); |
|
66 $user = $this->container->get('fos_user.user_manager')->findUserByEmail($email); |
|
67 |
|
68 if (null === $user) { |
|
69 throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email)); |
|
70 } |
|
71 |
|
72 return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:checkEmail.html.'.$this->getEngine(), array( |
|
73 'user' => $user, |
|
74 )); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Receive the confirmation token from user email provider, login the user |
|
79 */ |
|
80 public function confirmAction($token) |
|
81 { |
|
82 $user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token); |
|
83 |
|
84 if (null === $user) { |
|
85 throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token)); |
|
86 } |
|
87 |
|
88 $user->setConfirmationToken(null); |
|
89 $user->setEnabled(true); |
|
90 |
|
91 $this->container->get('fos_user.user_manager')->updateUser($user); |
|
92 $this->authenticateUser($user); |
|
93 |
|
94 return new RedirectResponse($this->container->get('router')->generate('fos_user_registration_confirmed')); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Tell the user his account is now confirmed |
|
99 */ |
|
100 public function confirmedAction() |
|
101 { |
|
102 $user = $this->container->get('security.context')->getToken()->getUser(); |
|
103 if (!is_object($user) || !$user instanceof UserInterface) { |
|
104 throw new AccessDeniedException('This user does not have access to this section.'); |
|
105 } |
|
106 |
|
107 return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:confirmed.html.'.$this->getEngine(), array( |
|
108 'user' => $user, |
|
109 )); |
|
110 } |
|
111 |
|
112 /** |
|
113 * Authenticate a user with Symfony Security |
|
114 * |
|
115 * @param Boolean $reAuthenticate |
|
116 */ |
|
117 protected function authenticateUser(UserInterface $user) |
|
118 { |
|
119 $providerKey = $this->container->getParameter('fos_user.firewall_name'); |
|
120 $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); |
|
121 |
|
122 $this->container->get('security.context')->setToken($token); |
|
123 } |
|
124 |
|
125 protected function setFlash($action, $value) |
|
126 { |
|
127 $this->container->get('session')->setFlash($action, $value); |
|
128 } |
|
129 |
|
130 protected function getEngine() |
|
131 { |
|
132 return $this->container->getParameter('fos_user.template.engine'); |
|
133 } |
|
134 } |