vendor/bundles/FOS/UserBundle/Controller/ResettingController.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\HttpKernel\Exception\NotFoundHttpException;
       
    17 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
       
    18 use FOS\UserBundle\Model\UserInterface;
       
    19 
       
    20 /**
       
    21  * Controller managing the resetting of the password
       
    22  *
       
    23  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
       
    24  * @author Christophe Coevoet <stof@notk.org>
       
    25  */
       
    26 class ResettingController extends ContainerAware
       
    27 {
       
    28     /**
       
    29      * Request reset user password: show form
       
    30      */
       
    31     public function requestAction()
       
    32     {
       
    33         return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:request.html.'.$this->getEngine());
       
    34     }
       
    35 
       
    36     /**
       
    37      * Request reset user password: submit form and send email
       
    38      */
       
    39     public function sendEmailAction()
       
    40     {
       
    41         $username = $this->container->get('request')->request->get('username');
       
    42 
       
    43         $user = $this->container->get('fos_user.user_manager')->findUserByUsernameOrEmail($username);
       
    44 
       
    45         if (null === $user){
       
    46             return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:request.html.'.$this->getEngine(), array('invalid_username' => $username));
       
    47         }
       
    48 
       
    49         if ($user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
       
    50             return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:passwordAlreadyRequested.html.'.$this->getEngine());
       
    51         }
       
    52 
       
    53         $user->generateConfirmationToken();
       
    54         $this->container->get('session')->set('fos_user_send_resetting_email/email', $user->getEmail());
       
    55         $this->container->get('fos_user.mailer')->sendResettingEmailMessage($user);
       
    56         $user->setPasswordRequestedAt(new \DateTime());
       
    57         $this->container->get('fos_user.user_manager')->updateUser($user);
       
    58 
       
    59         return new RedirectResponse($this->container->get('router')->generate('fos_user_resetting_check_email'));
       
    60     }
       
    61 
       
    62     /**
       
    63      * Tell the user to check his email provider
       
    64      */
       
    65     public function checkEmailAction()
       
    66     {
       
    67         $session = $this->container->get('session');
       
    68         $email = $session->get('fos_user_send_resetting_email/email');
       
    69         $session->remove('fos_user_send_resetting_email/email');
       
    70         $user = $this->container->get('fos_user.user_manager')->findUserByEmail($email);
       
    71         if (empty($user)) {
       
    72             return new RedirectResponse($this->container->get('router')->generate('fos_user_resetting_request'));
       
    73         }
       
    74 
       
    75         return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:checkEmail.html.'.$this->getEngine(), array(
       
    76             'user' => $user,
       
    77         ));
       
    78     }
       
    79 
       
    80     /**
       
    81      * Reset user password
       
    82      */
       
    83     public function resetAction($token)
       
    84     {
       
    85         $user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token);
       
    86 
       
    87         if (null === $user){
       
    88             throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
       
    89         }
       
    90 
       
    91         if (!$user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
       
    92             return new RedirectResponse($this->container->get('router')->generate('fos_user_resetting_request'));
       
    93         }
       
    94 
       
    95         $form = $this->container->get('fos_user.resetting.form');
       
    96         $formHandler = $this->container->get('fos_user.resetting.form.handler');
       
    97         $process = $formHandler->process($user);
       
    98 
       
    99         if ($process) {
       
   100             $this->authenticateUser($user);
       
   101 
       
   102             $this->setFlash('fos_user_success', 'resetting.flash.success');
       
   103 
       
   104             return new RedirectResponse($this->getRedirectionUrl($user));
       
   105         }
       
   106 
       
   107         return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:reset.html.'.$this->getEngine(), array(
       
   108             'token' => $token,
       
   109             'form' => $form->createView(),
       
   110             'theme' => $this->container->getParameter('fos_user.template.theme'),
       
   111         ));
       
   112     }
       
   113 
       
   114     /**
       
   115      * Authenticate a user with Symfony Security
       
   116      *
       
   117      * @param UserInterface $user
       
   118      */
       
   119     protected function authenticateUser(UserInterface $user)
       
   120     {
       
   121         $providerKey = $this->container->getParameter('fos_user.firewall_name');
       
   122         $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
       
   123 
       
   124         $this->container->get('security.context')->setToken($token);
       
   125     }
       
   126 
       
   127     /**
       
   128      * Generate the redirection url when the resetting is completed.
       
   129      *
       
   130      * @param UserInterface $user
       
   131      * @return string
       
   132      */
       
   133     protected function getRedirectionUrl(UserInterface $user)
       
   134     {
       
   135         return $this->container->get('router')->generate('fos_user_profile_show');
       
   136     }
       
   137 
       
   138     protected function setFlash($action, $value)
       
   139     {
       
   140         $this->container->get('session')->setFlash($action, $value);
       
   141     }
       
   142 
       
   143     protected function getEngine()
       
   144     {
       
   145         return $this->container->getParameter('fos_user.template.engine');
       
   146     }
       
   147 }