ChangePasswordController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. use FOS\UserBundle\Model\UserInterface;
  15. /**
  16. * Controller managing the password change
  17. *
  18. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  19. * @author Christophe Coevoet <stof@notk.org>
  20. */
  21. class ChangePasswordController extends ContainerAware
  22. {
  23. /**
  24. * Change user password
  25. */
  26. public function changePasswordAction()
  27. {
  28. $user = $this->container->get('security.context')->getToken()->getUser();
  29. if (!is_object($user) || !$user instanceof UserInterface) {
  30. throw new AccessDeniedException('This user does not have access to this section.');
  31. }
  32. $form = $this->container->get('fos_user.change_password.form');
  33. $formHandler = $this->container->get('fos_user.change_password.form.handler');
  34. $process = $formHandler->process($user);
  35. if ($process) {
  36. $this->setFlash('fos_user_success', 'change_password.flash.success');
  37. return new RedirectResponse($this->getRedirectionUrl($user));
  38. }
  39. return $this->container->get('templating')->renderResponse(
  40. 'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
  41. array('form' => $form->createView())
  42. );
  43. }
  44. /**
  45. * Generate the redirection url when the resetting is completed.
  46. *
  47. * @param \FOS\UserBundle\Model\UserInterface $user
  48. *
  49. * @return string
  50. */
  51. protected function getRedirectionUrl(UserInterface $user)
  52. {
  53. return $this->container->get('router')->generate('fos_user_profile_show');
  54. }
  55. /**
  56. * @param string $action
  57. * @param string $value
  58. */
  59. protected function setFlash($action, $value)
  60. {
  61. $this->container->get('session')->getFlashBag()->set($action, $value);
  62. }
  63. }