ChangePasswordFOSUser1Controller.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\UserBundle\Controller;
  11. use FOS\UserBundle\Model\UserInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Response;
  16. /**
  17. * This class is inspired from the FOS Change Password Controller.
  18. *
  19. * @author Hugo Briand <briand@ekino.com>
  20. */
  21. class ChangePasswordFOSUser1Controller extends Controller
  22. {
  23. /**
  24. * @return Response|RedirectResponse
  25. *
  26. * @throws AccessDeniedException
  27. */
  28. public function changePasswordAction()
  29. {
  30. $user = $this->getUser();
  31. if (!is_object($user) || !$user instanceof UserInterface) {
  32. throw new AccessDeniedException('This user does not have access to this section.');
  33. }
  34. $form = $this->get('fos_user.change_password.form');
  35. $formHandler = $this->get('fos_user.change_password.form.handler');
  36. $process = $formHandler->process($user);
  37. if ($process) {
  38. $this->setFlash('fos_user_success', 'change_password.flash.success');
  39. return $this->redirect($this->getRedirectionUrl($user));
  40. }
  41. return $this->render(
  42. 'SonataUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
  43. array('form' => $form->createView())
  44. );
  45. }
  46. /**
  47. * @param UserInterface $user
  48. *
  49. * @return string
  50. */
  51. protected function getRedirectionUrl(UserInterface $user)
  52. {
  53. return $this->generateUrl('sonata_user_profile_show');
  54. }
  55. /**
  56. * @param string $action
  57. * @param string $value
  58. */
  59. protected function setFlash($action, $value)
  60. {
  61. $this->get('session')->getFlashBag()->set($action, $value);
  62. }
  63. }