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