* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace UBV\AutenticacionBundle\Controller; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\FormEvent; use FOS\UserBundle\Event\FilterUserResponseEvent; use FOS\UserBundle\Event\GetResponseUserEvent; use FOS\UserBundle\Model\UserInterface; use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Security\Core\Exception\AccessDeniedException; /** * Controller managing the password change * * @author Thibault Duplessis * @author Christophe Coevoet */ class ChangePasswordController extends ContainerAware { /** * Change user password */ public function changePasswordAction(Request $request) { $user = $this->container->get('security.context')->getToken()->getUser(); if (!is_object($user) || !$user instanceof UserInterface) { throw new AccessDeniedException('This user does not have access to this section.'); } /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ $dispatcher = $this->container->get('event_dispatcher'); $event = new GetResponseUserEvent($user, $request); $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE, $event); if (null !== $event->getResponse()) { return $event->getResponse(); } /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */ $formFactory = $this->container->get('fos_user.change_password.form.factory'); $form = $formFactory->createForm(); $form->setData($user); if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ $userManager = $this->container->get('fos_user.user_manager'); $event = new FormEvent($form, $request); $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event); $userManager->updateUser($user); if (null === $response = $event->getResponse()) { $url = $this->container->get('router')->generate('ubv_principal'); $response = new RedirectResponse($url); } $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); return $response; } } return $this->container->get('templating')->renderResponse( 'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'), array('form' => $form->createView()) ); } }