vendor/friendsofsymfony/user-bundle/Controller/ResettingController.php line 69

Open in your IDE?
  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 FOS\UserBundle\Event\FilterUserResponseEvent;
  12. use FOS\UserBundle\Event\FormEvent;
  13. use FOS\UserBundle\Event\GetResponseNullableUserEvent;
  14. use FOS\UserBundle\Event\GetResponseUserEvent;
  15. use FOS\UserBundle\Form\Factory\FactoryInterface;
  16. use FOS\UserBundle\FOSUserEvents;
  17. use FOS\UserBundle\Mailer\MailerInterface;
  18. use FOS\UserBundle\Model\UserManagerInterface;
  19. use FOS\UserBundle\Util\TokenGeneratorInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. /**
  26.  * Controller managing the resetting of the password.
  27.  *
  28.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  29.  * @author Christophe Coevoet <stof@notk.org>
  30.  */
  31. class ResettingController extends Controller
  32. {
  33.     private $eventDispatcher;
  34.     private $formFactory;
  35.     private $userManager;
  36.     private $tokenGenerator;
  37.     private $mailer;
  38.     /**
  39.      * @var int
  40.      */
  41.     private $retryTtl;
  42.     /**
  43.      * @param EventDispatcherInterface $eventDispatcher
  44.      * @param FactoryInterface         $formFactory
  45.      * @param UserManagerInterface     $userManager
  46.      * @param TokenGeneratorInterface  $tokenGenerator
  47.      * @param MailerInterface          $mailer
  48.      * @param int                      $retryTtl
  49.      */
  50.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManagerTokenGeneratorInterface $tokenGeneratorMailerInterface $mailer$retryTtl)
  51.     {
  52.         $this->eventDispatcher $eventDispatcher;
  53.         $this->formFactory $formFactory;
  54.         $this->userManager $userManager;
  55.         $this->tokenGenerator $tokenGenerator;
  56.         $this->mailer $mailer;
  57.         $this->retryTtl $retryTtl;
  58.     }
  59.     /**
  60.      * Request reset user password: show form.
  61.      */
  62.     public function requestAction()
  63.     {
  64.         return $this->render('@FOSUser/Resetting/request.html.twig');
  65.     }
  66.     /**
  67.      * Request reset user password: submit form and send email.
  68.      *
  69.      * @param Request $request
  70.      *
  71.      * @return Response
  72.      */
  73.     public function sendEmailAction(Request $request)
  74.     {
  75.         $username $request->request->get('username');
  76.         $user $this->userManager->findUserByUsernameOrEmail($username);
  77. // AƱadida linea para comprobar reenvio de resseting duplicado
  78.            if ($user->getPasswordRequestedAt()!=null || $user->getPasswordRequestedAt()!=''){
  79.                $em$this->getDoctrine()->getManager();
  80.                $user->setPasswordRequestedAt(null);
  81.               $em->flush();
  82.           }
  83.         $event = new GetResponseNullableUserEvent($user$request);
  84.         $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE$event);
  85.         if (null !== $event->getResponse()) {
  86.             return $event->getResponse();
  87.         }
  88.         if (null !== $user && !$user->isPasswordRequestNonExpired($this->retryTtl)) {
  89.             $event = new GetResponseUserEvent($user$request);
  90.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_REQUEST$event);
  91.             if (null !== $event->getResponse()) {
  92.                 return $event->getResponse();
  93.             }
  94.             if (null === $user->getConfirmationToken()) {
  95.                 $user->setConfirmationToken($this->tokenGenerator->generateToken());
  96.             }
  97.             $event = new GetResponseUserEvent($user$request);
  98.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_CONFIRM$event);
  99.             if (null !== $event->getResponse()) {
  100.                 return $event->getResponse();
  101.             }
  102.             $this->mailer->sendResettingEmailMessage($user);
  103.             $user->setPasswordRequestedAt(new \DateTime());
  104.             $this->userManager->updateUser($user);
  105.             $event = new GetResponseUserEvent($user$request);
  106.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_COMPLETED$event);
  107.             if (null !== $event->getResponse()) {
  108.                 return $event->getResponse();
  109.             }
  110.         }
  111.         return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email', array('username' => $username)));
  112.     }
  113.     /**
  114.      * Tell the user to check his email provider.
  115.      *
  116.      * @param Request $request
  117.      *
  118.      * @return Response
  119.      */
  120.     public function checkEmailAction(Request $request)
  121.     {
  122.         $username $request->query->get('username');
  123.         if (empty($username)) {
  124.             // the user does not come from the sendEmail action
  125.             return new RedirectResponse($this->generateUrl('fos_user_resetting_request'));
  126.         }
  127.         return $this->render('@FOSUser/Resetting/check_email.html.twig', array(
  128.             'tokenLifetime' => ceil($this->retryTtl 3600),
  129.         ));
  130.     }
  131.     /**
  132.      * Reset user password.
  133.      *
  134.      * @param Request $request
  135.      * @param string  $token
  136.      *
  137.      * @return Response
  138.      */
  139.     public function resetAction(Request $request$token)
  140.     {
  141.         $user $this->userManager->findUserByConfirmationToken($token);
  142.         if (null === $user) {
  143.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  144.         }
  145.         $event = new GetResponseUserEvent($user$request);
  146.         $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE$event);
  147.         if (null !== $event->getResponse()) {
  148.             return $event->getResponse();
  149.         }
  150.         $form $this->formFactory->createForm();
  151.         $form->setData($user);
  152.         $form->handleRequest($request);
  153.         if ($form->isSubmitted() && $form->isValid()) {
  154.             $event = new FormEvent($form$request);
  155.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS$event);
  156.             $this->userManager->updateUser($user);
  157.             if (null === $response $event->getResponse()) {
  158.                 $url $this->generateUrl('fos_user_profile_show');
  159.                 $response = new RedirectResponse($url);
  160.             }
  161.             $this->eventDispatcher->dispatch(
  162.                 FOSUserEvents::RESETTING_RESET_COMPLETED,
  163.                 new FilterUserResponseEvent($user$request$response)
  164.             );
  165.             return $response;
  166.         }
  167.         return $this->render('@FOSUser/Resetting/reset.html.twig', array(
  168.             'token' => $token,
  169.             'form' => $form->createView(),
  170.         ));
  171.     }
  172. }