Skip to content

Commit 95d57ad

Browse files
committed
Refactored the Registration controller
The controller now handles the form itself and dispatches several events during the workflow.
1 parent 5a4db1f commit 95d57ad

16 files changed

+438
-160
lines changed

Controller/RegistrationController.php

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Event\UserResponseEvent;
1417
use Symfony\Component\DependencyInjection\ContainerAware;
15-
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpFoundation\Request;
1619
use Symfony\Component\HttpFoundation\RedirectResponse;
1720
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1821
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19-
use Symfony\Component\Security\Core\Exception\AccountStatusException;
2022
use FOS\UserBundle\Model\UserInterface;
2123

2224
/**
@@ -27,34 +29,40 @@
2729
*/
2830
class RegistrationController extends ContainerAware
2931
{
30-
public function registerAction()
32+
public function registerAction(Request $request)
3133
{
32-
$form = $this->container->get('fos_user.registration.form');
33-
$formHandler = $this->container->get('fos_user.registration.form.handler');
34-
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
35-
36-
$process = $formHandler->process($confirmationEnabled);
37-
if ($process) {
38-
$user = $form->getData();
39-
40-
$authUser = false;
41-
if ($confirmationEnabled) {
42-
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
43-
$route = 'fos_user_registration_check_email';
44-
} else {
45-
$authUser = true;
46-
$route = 'fos_user_registration_confirmed';
47-
}
34+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
35+
$formFactory = $this->container->get('fos_user.registration.form.factory');
36+
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
37+
$userManager = $this->container->get('fos_user.user_manager');
4838

49-
$this->setFlash('fos_user_success', 'registration.flash.user_created');
50-
$url = $this->container->get('router')->generate($route);
51-
$response = new RedirectResponse($url);
39+
$form = $formFactory->createForm();
40+
$user = $userManager->createUser();
5241

53-
if ($authUser) {
54-
$this->authenticateUser($user, $response);
55-
}
42+
$user->setEnabled(true);
43+
$form->setData($user);
44+
45+
if ('POST' === $request->getMethod()) {
46+
$form->bind($request);
47+
48+
if ($form->isValid()) {
49+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
50+
$dispatcher = $this->container->get('event_dispatcher');
51+
52+
$event = new FormEvent($form);
53+
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
5654

57-
return $response;
55+
$userManager->updateUser($user);
56+
57+
if (null === $response = $event->getResponse()) {
58+
$url = $this->container->get('router')->generate('fos_user_registration_confirmed');
59+
$response = new RedirectResponse($url);
60+
}
61+
62+
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new UserResponseEvent($user, $response));
63+
64+
return $response;
65+
}
5866
}
5967

6068
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
@@ -97,7 +105,10 @@ public function confirmAction($token)
97105

98106
$this->container->get('fos_user.user_manager')->updateUser($user);
99107
$response = new RedirectResponse($this->container->get('router')->generate('fos_user_registration_confirmed'));
100-
$this->authenticateUser($user, $response);
108+
109+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
110+
$dispatcher = $this->container->get('event_dispatcher');
111+
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRMED, new UserResponseEvent($user, $response));
101112

102113
return $response;
103114
}
@@ -117,34 +128,6 @@ public function confirmedAction()
117128
));
118129
}
119130

120-
/**
121-
* Authenticate a user with Symfony Security
122-
*
123-
* @param \FOS\UserBundle\Model\UserInterface $user
124-
* @param \Symfony\Component\HttpFoundation\Response $response
125-
*/
126-
protected function authenticateUser(UserInterface $user, Response $response)
127-
{
128-
try {
129-
$this->container->get('fos_user.security.login_manager')->loginUser(
130-
$this->container->getParameter('fos_user.firewall_name'),
131-
$user,
132-
$response);
133-
} catch (AccountStatusException $ex) {
134-
// We simply do not authenticate users which do not pass the user
135-
// checker (not enabled, expired, etc.).
136-
}
137-
}
138-
139-
/**
140-
* @param string $action
141-
* @param string $value
142-
*/
143-
protected function setFlash($action, $value)
144-
{
145-
$this->container->get('session')->setFlash($action, $value);
146-
}
147-
148131
protected function getEngine()
149132
{
150133
return $this->container->getParameter('fos_user.template.engine');

DependencyInjection/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ private function addRegistrationSection(ArrayNodeDefinition $node)
133133
->addDefaultsIfNotSet()
134134
->children()
135135
->scalarNode('type')->defaultValue('fos_user_registration')->end()
136-
->scalarNode('handler')->defaultValue('fos_user.registration.form.handler.default')->end()
137136
->scalarNode('name')->defaultValue('fos_user_registration_form')->end()
138137
->arrayNode('validation_groups')
139138
->prototype('scalar')->end()

DependencyInjection/FOSUserExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function load(array $configs, ContainerBuilder $container)
3232
$loader->load(sprintf('%s.xml', $config['db_driver']));
3333
}
3434

35-
foreach (array('validator', 'security', 'util', 'mailer') as $basename) {
35+
foreach (array('validator', 'security', 'util', 'mailer', 'listeners') as $basename) {
3636
$loader->load(sprintf('%s.xml', $basename));
3737
}
3838

@@ -114,8 +114,9 @@ private function loadRegistration(array $config, ContainerBuilder $container, Xm
114114
{
115115
$loader->load('registration.xml');
116116

117-
$container->setAlias('fos_user.registration.form.handler', $config['form']['handler']);
118-
unset($config['form']['handler']);
117+
if ($config['confirmation']['enabled']) {
118+
$loader->load('email_confirmation.xml');
119+
}
119120

120121
if (isset($config['confirmation']['from_email'])) {
121122
// overwrite the global one

Event/FormEvent.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Event;
13+
14+
use Symfony\Component\EventDispatcher\Event;
15+
use Symfony\Component\Form\FormInterface;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
class FormEvent extends Event
19+
{
20+
private $form;
21+
private $response;
22+
23+
public function __construct(FormInterface $form)
24+
{
25+
$this->form = $form;
26+
}
27+
28+
/**
29+
* @return FormInterface
30+
*/
31+
public function getForm()
32+
{
33+
return $this->form;
34+
}
35+
36+
public function setResponse(Response $response)
37+
{
38+
$this->response = $response;
39+
}
40+
41+
/**
42+
* @return Response|null
43+
*/
44+
public function getResponse()
45+
{
46+
return $this->response;
47+
}
48+
}

Event/UserResponseEvent.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Event;
13+
14+
use FOS\UserBundle\Model\UserInterface;
15+
use Symfony\Component\EventDispatcher\Event;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
class UserResponseEvent extends Event
19+
{
20+
private $user;
21+
private $response;
22+
23+
public function __construct(UserInterface $user, Response $response)
24+
{
25+
$this->user = $user;
26+
$this->response = $response;
27+
}
28+
29+
/**
30+
* @return UserInterface
31+
*/
32+
public function getUser()
33+
{
34+
return $this->user;
35+
}
36+
37+
/**
38+
* @return Response
39+
*/
40+
public function getResponse()
41+
{
42+
return $this->response;
43+
}
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\EventListener;
13+
14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\UserResponseEvent;
16+
use FOS\UserBundle\Security\LoginManagerInterface;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
use Symfony\Component\Security\Core\Exception\AccountStatusException;
19+
20+
class AuthenticationListener implements EventSubscriberInterface
21+
{
22+
private $loginManager;
23+
private $firewallName;
24+
25+
public function __construct(LoginManagerInterface $loginManager, $firewallName)
26+
{
27+
$this->loginManager = $loginManager;
28+
$this->firewallName = $firewallName;
29+
}
30+
31+
public static function getSubscribedEvents()
32+
{
33+
return array(
34+
FOSUserEvents::REGISTRATION_COMPLETED => 'authenticate',
35+
FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate',
36+
);
37+
}
38+
39+
public function authenticate(UserResponseEvent $event)
40+
{
41+
if (!$event->getUser()->isEnabled()) {
42+
return;
43+
}
44+
45+
try {
46+
$this->loginManager->loginUser($this->firewallName, $event->getUser(),$event->getResponse());
47+
} catch (AccountStatusException $ex) {
48+
// We simply do not authenticate users which do not pass the user
49+
// checker (not enabled, expired, etc.).
50+
}
51+
}
52+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\EventListener;
13+
14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Mailer\MailerInterface;
17+
use FOS\UserBundle\Util\TokenGeneratorInterface;
18+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19+
use Symfony\Component\HttpFoundation\RedirectResponse;
20+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
21+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22+
23+
class EmailConfirmationListener implements EventSubscriberInterface
24+
{
25+
private $mailer;
26+
private $tokenGenerator;
27+
private $router;
28+
private $session;
29+
30+
public function __construct(MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, UrlGeneratorInterface $router, SessionInterface $session)
31+
{
32+
$this->mailer = $mailer;
33+
$this->tokenGenerator = $tokenGenerator;
34+
$this->router = $router;
35+
$this->session = $session;
36+
}
37+
38+
public static function getSubscribedEvents()
39+
{
40+
return array(
41+
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
42+
);
43+
}
44+
45+
public function onRegistrationSuccess(FormEvent $event)
46+
{
47+
/** @var $user \FOS\UserBundle\Model\UserInterface */
48+
$user = $event->getForm()->getData();
49+
50+
$user->setEnabled(false);
51+
if (null === $user->getConfirmationToken()) {
52+
$user->setConfirmationToken($this->tokenGenerator->generateToken());
53+
}
54+
55+
$this->mailer->sendConfirmationEmailMessage($user);
56+
57+
$this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
58+
59+
$url = $this->router->generate('fos_user_registration_check_email');
60+
$event->setResponse(new RedirectResponse($url));
61+
}
62+
}

0 commit comments

Comments
 (0)