1111
1212namespace 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 ;
1418use Symfony \Component \DependencyInjection \ContainerAware ;
19+ use Symfony \Component \HttpFoundation \Request ;
1520use Symfony \Component \HttpFoundation \RedirectResponse ;
1621use 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}
0 commit comments