src/EventListener/LoginListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  8. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. class LoginListener {
  11.     private $securityContext;
  12.     private $router;
  13.     private $dispatcher;
  14.     public function __construct(AuthorizationChecker $securityContextRouter $routerEventDispatcherInterface $dispatcher) {
  15.         $this->securityContext $securityContext;
  16.         $this->router $router;
  17.         $this->dispatcher $dispatcher;
  18.     }
  19.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {
  20.         if ($this->securityContext->isGranted 'IS_AUTHENTICATED_FULLY' )) {
  21.             $user $event->getAuthenticationToken ()->getUser ();
  22.             if ($user->getLastLogin () === null) {
  23.                 $this->dispatcher->addListener KernelEvents::RESPONSE, array (
  24.                         $this,
  25.                         'onKernelResponse' 
  26.                 ) );
  27.             }
  28.         }
  29.     }
  30.     public function onKernelResponse(FilterResponseEvent $event) {
  31.         $response = new RedirectResponse $this->router->generate 'fos_user_change_password' ) );
  32.         $event->setResponse $response );
  33.     }
  34. }