Skip to content

Commit ee8cb63

Browse files
committed
Refactored the ChangePasswordController to use events
1 parent 44a1361 commit ee8cb63

File tree

11 files changed

+82
-156
lines changed

11 files changed

+82
-156
lines changed

Controller/ChangePasswordController.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +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;
17+
use FOS\UserBundle\Model\UserInterface;
1418
use Symfony\Component\DependencyInjection\ContainerAware;
19+
use Symfony\Component\HttpFoundation\Request;
1520
use Symfony\Component\HttpFoundation\RedirectResponse;
1621
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17-
use FOS\UserBundle\Model\UserInterface;
1822

1923
/**
2024
* Controller managing the password change
@@ -27,47 +31,47 @@ class ChangePasswordController extends ContainerAware
2731
/**
2832
* Change user password
2933
*/
30-
public function changePasswordAction()
34+
public function changePasswordAction(Request $request)
3135
{
3236
$user = $this->container->get('security.context')->getToken()->getUser();
3337
if (!is_object($user) || !$user instanceof UserInterface) {
3438
throw new AccessDeniedException('This user does not have access to this section.');
3539
}
3640

37-
$form = $this->container->get('fos_user.change_password.form');
38-
$formHandler = $this->container->get('fos_user.change_password.form.handler');
41+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
42+
$formFactory = $this->container->get('fos_user.change_password.form.factory');
43+
44+
$form = $formFactory->createForm();
45+
$form->setData($user);
46+
47+
if ($request->isMethod('POST')) {
48+
$form->bind($request);
49+
50+
if ($form->isValid()) {
51+
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
52+
$userManager = $this->container->get('fos_user.user_manager');
53+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
54+
$dispatcher = $this->container->get('event_dispatcher');
3955

40-
$process = $formHandler->process($user);
41-
if ($process) {
42-
$this->setFlash('fos_user_success', 'change_password.flash.success');
56+
$event = new FormEvent($form);
57+
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
4358

44-
return new RedirectResponse($this->getRedirectionUrl($user));
59+
$userManager->updateUser($user);
60+
61+
if (null === $response = $event->getResponse()) {
62+
$url = $this->container->get('router')->generate('fos_user_profile_show');
63+
$response = new RedirectResponse($url);
64+
}
65+
66+
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new UserResponseEvent($user, $response));
67+
68+
return $response;
69+
}
4570
}
4671

4772
return $this->container->get('templating')->renderResponse(
4873
'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
4974
array('form' => $form->createView())
5075
);
5176
}
52-
53-
/**
54-
* Generate the redirection url when the resetting is completed.
55-
*
56-
* @param \FOS\UserBundle\Model\UserInterface $user
57-
*
58-
* @return string
59-
*/
60-
protected function getRedirectionUrl(UserInterface $user)
61-
{
62-
return $this->container->get('router')->generate('fos_user_profile_show');
63-
}
64-
65-
/**
66-
* @param string $action
67-
* @param string $value
68-
*/
69-
protected function setFlash($action, $value)
70-
{
71-
$this->container->get('session')->setFlash($action, $value);
72-
}
7377
}

DependencyInjection/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ private function addChangePasswordSection(ArrayNodeDefinition $node)
194194
->addDefaultsIfNotSet()
195195
->children()
196196
->scalarNode('type')->defaultValue('fos_user_change_password')->end()
197-
->scalarNode('handler')->defaultValue('fos_user.change_password.form.handler.default')->end()
198197
->scalarNode('name')->defaultValue('fos_user_change_password_form')->end()
199198
->arrayNode('validation_groups')
200199
->prototype('scalar')->end()

DependencyInjection/FOSUserExtension.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ private function loadChangePassword(array $config, ContainerBuilder $container,
132132
{
133133
$loader->load('change_password.xml');
134134

135-
$container->setAlias('fos_user.change_password.form.handler', $config['form']['handler']);
136-
unset($config['form']['handler']);
137-
138135
$this->remapParametersNamespaces($config, $container, array(
139136
'form' => 'fos_user.change_password.form.%s',
140137
));

EventListener/FlashListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(SessionInterface $session, TranslatorInterface $tran
3434
public static function getSubscribedEvents()
3535
{
3636
return array(
37+
FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'onChangePasswordSuccess',
3738
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
3839
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
3940
FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess'
@@ -45,6 +46,11 @@ public function onProfileEditSuccess(FormEvent $event)
4546
$this->session->getFlashBag()->add('success', $this->trans('profile.flash.updated'));
4647
}
4748

49+
public function onChangePasswordSuccess(FormEvent $event)
50+
{
51+
$this->session->getFlashBag()->add('success', $this->trans('change_password.flash.updated'));
52+
}
53+
4854
public function onRegistrationSuccess(FormEvent $event)
4955
{
5056
$this->session->getFlashBag()->add('success', $this->trans('registration.flash.user_created'));

FOSUserEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
*/
1717
final class FOSUserEvents
1818
{
19+
/**
20+
* The CHANGE_PASSWORD_SUCCESS event occurs when the change password form is submitted successfully.
21+
*
22+
* This event allows you to set the response instead of using the default one.
23+
* The event listener method receives a FOS\UserBundle\Event\FormEvent instance.
24+
*/
25+
const CHANGE_PASSWORD_SUCCESS = 'fos_user.change_password.edit.success';
26+
27+
/**
28+
* The CHANGE_PASSWORD_COMPLETED event occurs after saving the user in the change password process.
29+
*
30+
* This event allows you to access the response which will be sent.
31+
* The event listener method receives a FOS\UserBundle\Event\UserResponseEvent instance.
32+
*/
33+
const CHANGE_PASSWORD_COMPLETED = 'fos_user.change_password.edit.completed';
34+
1935
/**
2036
* The PROFILE_EDIT_SUCCESS event occurs when the profile edit form is submitted successfully.
2137
*

Form/Handler/ChangePasswordFormHandler.php

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

Form/Model/ChangePassword.php

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

Form/Type/ChangePasswordFormType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818

1919
class ChangePasswordFormType extends AbstractType
2020
{
21+
private $class;
22+
23+
/**
24+
* @param string $class The User class name
25+
*/
26+
public function __construct($class)
27+
{
28+
$this->class = $class;
29+
}
30+
2131
public function buildForm(FormBuilderInterface $builder, array $options)
2232
{
2333
$builder->add('current_password', 'password', array(
@@ -26,7 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2636
'mapped' => false,
2737
'constraints' => new UserPassword(),
2838
));
29-
$builder->add('new', 'repeated', array(
39+
$builder->add('plainPassword', 'repeated', array(
3040
'type' => 'password',
3141
'options' => array('translation_domain' => 'FOSUserBundle'),
3242
'first_options' => array('label' => 'form.new_password'),
@@ -38,7 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3848
public function setDefaultOptions(OptionsResolverInterface $resolver)
3949
{
4050
$resolver->setDefaults(array(
41-
'data_class' => 'FOS\UserBundle\Form\Model\ChangePassword',
51+
'data_class' => $this->class,
4252
'intention' => 'change_password',
4353
));
4454
}

Resources/config/change_password.xml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
9-
<service id="fos_user.change_password.form" factory-method="createNamed" factory-service="form.factory" class="Symfony\Component\Form\Form">
8+
<service id="fos_user.change_password.form.factory" class="FOS\UserBundle\Form\Factory\FormFactory">
9+
<argument type="service" id="form.factory" />
1010
<argument>%fos_user.change_password.form.name%</argument>
1111
<argument>%fos_user.change_password.form.type%</argument>
12-
<argument>null</argument>
13-
<argument type="collection">
14-
<argument key="validation_groups">%fos_user.change_password.form.validation_groups%</argument>
15-
</argument>
12+
<argument>%fos_user.change_password.form.validation_groups%</argument>
1613
</service>
1714

1815
<service id="fos_user.change_password.form.type" class="FOS\UserBundle\Form\Type\ChangePasswordFormType">
1916
<tag name="form.type" alias="fos_user_change_password" />
17+
<argument>%fos_user.model.user.class%</argument>
2018
</service>
21-
22-
<service id="fos_user.change_password.form.handler.default" class="FOS\UserBundle\Form\Handler\ChangePasswordFormHandler" scope="request">
23-
<argument type="service" id="fos_user.change_password.form" />
24-
<argument type="service" id="request" />
25-
<argument type="service" id="fos_user.user_manager" />
26-
</service>
27-
2819
</services>
2920

3021
</container>

Resources/config/validation.xml

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<option name="groups">
6060
<value>Registration</value>
6161
<value>ResetPassword</value>
62+
<value>ChangePassword</value>
6263
</option>
6364
</constraint>
6465
<constraint name="Length">
@@ -68,26 +69,7 @@
6869
<value>Registration</value>
6970
<value>Profile</value>
7071
<value>ResetPassword</value>
71-
</option>
72-
</constraint>
73-
</property>
74-
</class>
75-
76-
<class name="FOS\UserBundle\Form\Model\ChangePassword">
77-
<property name="new">
78-
<constraint name="NotBlank">
79-
<option name="message">fos_user.new_password.blank</option>
80-
<option name="groups">
81-
<value>ChangePassword</value>
82-
<value>ResetPassword</value>
83-
</option>
84-
</constraint>
85-
<constraint name="Length">
86-
<option name="min">2</option>
87-
<option name="minMessage">fos_user.new_password.short</option>
88-
<option name="groups">
8972
<value>ChangePassword</value>
90-
<value>ResetPassword</value>
9173
</option>
9274
</constraint>
9375
</property>
@@ -106,7 +88,6 @@
10688
<option name="maxMessage">fos_user.group.long</option>
10789
<option name="groups">Registration</option>
10890
</constraint>
109-
11091
</property>
11192
</class>
11293

@@ -164,14 +145,20 @@
164145
<property name="plainPassword">
165146
<constraint name="NotBlank">
166147
<option name="message">fos_user.password.blank</option>
167-
<option name="groups">Registration</option>
148+
<option name="groups">
149+
<value>Registration</value>
150+
<value>ResetPassword</value>
151+
<value>ChangePassword</value>
152+
</option>
168153
</constraint>
169154
<constraint name="Length">
170155
<option name="min">2</option>
171156
<option name="minMessage">fos_user.password.short</option>
172157
<option name="groups">
173158
<value>Registration</value>
174159
<value>Profile</value>
160+
<value>ResetPassword</value>
161+
<value>ChangePassword</value>
175162
</option>
176163
</constraint>
177164
</property>

0 commit comments

Comments
 (0)