Skip to content

Commit 44a1361

Browse files
committed
Refactored the password resetting submission
1 parent f48cc28 commit 44a1361

File tree

13 files changed

+188
-143
lines changed

13 files changed

+188
-143
lines changed

Controller/ResettingController.php

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

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Event\GetResponseUserEvent;
17+
use FOS\UserBundle\Event\UserResponseEvent;
18+
use FOS\UserBundle\Model\UserInterface;
1419
use Symfony\Component\DependencyInjection\ContainerAware;
15-
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpFoundation\Request;
1621
use Symfony\Component\HttpFoundation\RedirectResponse;
1722
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18-
use Symfony\Component\Security\Core\Exception\AccountStatusException;
19-
use FOS\UserBundle\Model\UserInterface;
2023

2124
/**
2225
* Controller managing the resetting of the password
@@ -90,28 +93,49 @@ public function checkEmailAction()
9093
/**
9194
* Reset user password
9295
*/
93-
public function resetAction($token)
96+
public function resetAction(Request $request, $token)
9497
{
95-
$user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token);
98+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
99+
$formFactory = $this->container->get('fos_user.resetting.form.factory');
100+
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
101+
$userManager = $this->container->get('fos_user.user_manager');
102+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
103+
$dispatcher = $this->container->get('event_dispatcher');
104+
105+
$user = $userManager->findUserByConfirmationToken($token);
96106

97107
if (null === $user) {
98108
throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
99109
}
100110

101-
if (!$user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
102-
return new RedirectResponse($this->container->get('router')->generate('fos_user_resetting_request'));
111+
$event = new GetResponseUserEvent($user);
112+
$dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);
113+
114+
if (null !== $event->getResponse()) {
115+
return $event->getResponse();
103116
}
104117

105-
$form = $this->container->get('fos_user.resetting.form');
106-
$formHandler = $this->container->get('fos_user.resetting.form.handler');
107-
$process = $formHandler->process($user);
118+
$form = $formFactory->createForm();
119+
$form->setData($user);
120+
121+
if ('POST' === $request->getMethod()) {
122+
$form->bind($request);
123+
124+
if ($form->isValid()) {
125+
$event = new FormEvent($form);
126+
$dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);
127+
128+
$userManager->updateUser($user);
108129

109-
if ($process) {
110-
$this->setFlash('fos_user_success', 'resetting.flash.success');
111-
$response = new RedirectResponse($this->getRedirectionUrl($user));
112-
$this->authenticateUser($user, $response);
130+
if (null === $response = $event->getResponse()) {
131+
$url = $this->container->get('router')->generate('fos_user_profile_show');
132+
$response = new RedirectResponse($url);
133+
}
113134

114-
return $response;
135+
$dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new UserResponseEvent($user, $response));
136+
137+
return $response;
138+
}
115139
}
116140

117141
return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:reset.html.'.$this->getEngine(), array(
@@ -120,37 +144,6 @@ public function resetAction($token)
120144
));
121145
}
122146

123-
/**
124-
* Authenticate a user with Symfony Security
125-
*
126-
* @param \FOS\UserBundle\Model\UserInterface $user
127-
* @param \Symfony\Component\HttpFoundation\Response $response
128-
*/
129-
protected function authenticateUser(UserInterface $user, Response $response)
130-
{
131-
try {
132-
$this->container->get('fos_user.security.login_manager')->loginUser(
133-
$this->container->getParameter('fos_user.firewall_name'),
134-
$user,
135-
$response);
136-
} catch (AccountStatusException $ex) {
137-
// We simply do not authenticate users which do not pass the user
138-
// checker (not enabled, expired, etc.).
139-
}
140-
}
141-
142-
/**
143-
* Generate the redirection url when the resetting is completed.
144-
*
145-
* @param \FOS\UserBundle\Model\UserInterface $user
146-
*
147-
* @return string
148-
*/
149-
protected function getRedirectionUrl(UserInterface $user)
150-
{
151-
return $this->container->get('router')->generate('fos_user_profile_show');
152-
}
153-
154147
/**
155148
* Get the truncated email displayed when requesting the resetting.
156149
*
@@ -170,15 +163,6 @@ protected function getObfuscatedEmail(UserInterface $user)
170163
return $email;
171164
}
172165

173-
/**
174-
* @param string $action
175-
* @param string $value
176-
*/
177-
protected function setFlash($action, $value)
178-
{
179-
$this->container->get('session')->setFlash($action, $value);
180-
}
181-
182166
protected function getEngine()
183167
{
184168
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
@@ -170,7 +170,6 @@ private function addResettingSection(ArrayNodeDefinition $node)
170170
->addDefaultsIfNotSet()
171171
->children()
172172
->scalarNode('type')->defaultValue('fos_user_resetting')->end()
173-
->scalarNode('handler')->defaultValue('fos_user.resetting.form.handler.default')->end()
174173
->scalarNode('name')->defaultValue('fos_user_resetting_form')->end()
175174
->arrayNode('validation_groups')
176175
->prototype('scalar')->end()

DependencyInjection/FOSUserExtension.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ private function loadResetting(array $config, ContainerBuilder $container, XmlFi
144144
{
145145
$loader->load('resetting.xml');
146146

147-
$container->setAlias('fos_user.resetting.form.handler', $config['form']['handler']);
148-
unset($config['form']['handler']);
149-
150147
if (isset($config['email']['from_email'])) {
151148
// overwrite the global one
152149
$fromEmail = $config['email']['from_email'];

Event/GetResponseUserEvent.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\HttpFoundation\Response;
15+
16+
class GetResponseUserEvent extends UserEvent
17+
{
18+
private $response;
19+
20+
public function setResponse(Response $response)
21+
{
22+
$this->response = $response;
23+
}
24+
25+
/**
26+
* @return Response|null
27+
*/
28+
public function getResponse()
29+
{
30+
return $this->response;
31+
}
32+
}

EventListener/AuthenticationListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static function getSubscribedEvents()
3434
return array(
3535
FOSUserEvents::REGISTRATION_COMPLETED => 'authenticate',
3636
FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate',
37+
FOSUserEvents::RESETTING_RESET_COMPLETED => 'authenticate',
3738
);
3839
}
3940

EventListener/FlashListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static function getSubscribedEvents()
3636
return array(
3737
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
3838
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
39+
FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess'
3940
);
4041
}
4142

@@ -49,6 +50,11 @@ public function onRegistrationSuccess(FormEvent $event)
4950
$this->session->getFlashBag()->add('success', $this->trans('registration.flash.user_created'));
5051
}
5152

53+
public function onResettingResetSuccess(FormEvent $event)
54+
{
55+
$this->session->getFlashBag()->add('success', $this->trans('resetting.flash.success'));
56+
}
57+
5258
private function trans($message, array $params = array())
5359
{
5460
return $this->translator->trans($message, $params, 'FOSUserBundle');

EventListener/ResettingListener.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Event\GetResponseUserEvent;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
use Symfony\Component\HttpFoundation\RedirectResponse;
19+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20+
21+
class ResettingListener implements EventSubscriberInterface
22+
{
23+
private $router;
24+
private $tokenTtl;
25+
26+
public function __construct(UrlGeneratorInterface $router, $tokenTtl)
27+
{
28+
$this->router = $router;
29+
$this->tokenTtl = $tokenTtl;
30+
}
31+
32+
public static function getSubscribedEvents()
33+
{
34+
return array(
35+
FOSUserEvents::RESETTING_RESET_INITIALIZE => 'onResettingResetInitialize',
36+
FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess'
37+
);
38+
}
39+
40+
public function onResettingResetInitialize(GetResponseUserEvent $event)
41+
{
42+
if (!$event->getUser()->isPasswordRequestNonExpired($this->tokenTtl)) {
43+
$event->setResponse(new RedirectResponse($this->router->generate('fos_user_resetting_request')));
44+
}
45+
}
46+
47+
public function onResettingResetSuccess(FormEvent $event)
48+
{
49+
/** @var $user \FOS\UserBundle\Model\UserInterface */
50+
$user = $event->getForm()->getData();
51+
52+
$user->setConfirmationToken(null);
53+
$user->setPasswordRequestedAt(null);
54+
$user->setEnabled(true);
55+
}
56+
}

FOSUserEvents.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ final class FOSUserEvents
7272
*/
7373
const REGISTRATION_CONFIRMED = 'fos_user.registration.confirmed';
7474

75+
/**
76+
* The RESETTING_RESET_INITIALIZE event occurs when the resetting process is initialized.
77+
*
78+
* This event allows you to set the response to bypass the processing.
79+
* The event listener method receives a FOS\UserBundle\Event\GetResponseUserEvent instance.
80+
*/
81+
const RESETTING_RESET_INITIALIZE = 'fos_user.resetting.reset.initialize';
82+
83+
/**
84+
* The RESETTING_RESET_SUCCESS event occurs when the resetting form is submitted successfully.
85+
*
86+
* This event allows you to set the response instead of using the default one.
87+
* The event listener method receives a FOS\UserBundle\Event\FormEvent instance.
88+
*/
89+
const RESETTING_RESET_SUCCESS = 'fos_user.resetting.reset.success';
90+
91+
/**
92+
* The RESETTING_RESET_COMPLETED event occurs after saving the user in the resetting process.
93+
*
94+
* This event allows you to access the response which will be sent.
95+
* The event listener method receives a FOS\UserBundle\Event\UserResponseEvent instance.
96+
*/
97+
const RESETTING_RESET_COMPLETED = 'fos_user.resetting.reset.completed';
98+
7599
/**
76100
* The SECURITY_IMPLICIT_LOGIN event occurs when the user is logged in programmatically.
77101
*

Form/Handler/ResettingFormHandler.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

Form/Type/ResettingFormType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@
1717

1818
class ResettingFormType extends AbstractType
1919
{
20+
private $class;
21+
22+
/**
23+
* @param string $class The User class name
24+
*/
25+
public function __construct($class)
26+
{
27+
$this->class = $class;
28+
}
29+
2030
public function buildForm(FormBuilderInterface $builder, array $options)
2131
{
22-
$builder->add('new', 'repeated', array(
32+
$builder->add('plainPassword', 'repeated', array(
2333
'type' => 'password',
2434
'options' => array('translation_domain' => 'FOSUserBundle'),
2535
'first_options' => array('label' => 'form.new_password'),
@@ -31,7 +41,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3141
public function setDefaultOptions(OptionsResolverInterface $resolver)
3242
{
3343
$resolver->setDefaults(array(
34-
'data_class' => 'FOS\UserBundle\Form\Model\ChangePassword',
44+
'data_class' => $this->class,
3545
'intention' => 'resetting',
3646
));
3747
}

0 commit comments

Comments
 (0)