vendor/kunstmaan/admin-bundle/EventListener/PasswordCheckListener.php line 72

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\AdminBundle\EventListener;
  3. use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Session\Session;
  6. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\Routing\RouterInterface as Router;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. use Symfony\Component\Translation\TranslatorInterface;
  12. use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
  13. /**
  14.  * PasswordCheckListener to check if the user has to change his password
  15.  */
  16. class PasswordCheckListener
  17. {
  18.     /**
  19.      * @var AuthorizationCheckerInterface
  20.      */
  21.     private $authorizationChecker;
  22.     /**
  23.      * @var TokenStorageInterface
  24.      */
  25.     private $tokenStorage;
  26.     /**
  27.      * @var Router
  28.      */
  29.     private $router;
  30.     /**
  31.      * @var Session
  32.      */
  33.     private $session;
  34.     /**
  35.      * @var TranslatorInterface
  36.      */
  37.     private $translator;
  38.     /**
  39.      * @var AdminRouteHelper
  40.      */
  41.     private $adminRouteHelper;
  42.     /**
  43.      * @param AuthorizationCheckerInterface $authorizationChecker
  44.      * @param TokenStorageInterface         $tokenStorage
  45.      * @param Router                        $router
  46.      * @param Session                       $session
  47.      * @param TranslatorInterface           $translator
  48.      * @param AdminRouteHelper              $adminRouteHelper
  49.      */
  50.     public function __construct(AuthorizationCheckerInterface $authorizationCheckerTokenStorageInterface $tokenStorageRouter $routerSession $sessionTranslatorInterface $translatorAdminRouteHelper $adminRouteHelper)
  51.     {
  52.         $this->authorizationChecker $authorizationChecker;
  53.         $this->tokenStorage $tokenStorage;
  54.         $this->router $router;
  55.         $this->session $session;
  56.         $this->translator $translator;
  57.         $this->adminRouteHelper $adminRouteHelper;
  58.     }
  59.     /**
  60.      * @param GetResponseEvent|ResponseEvent $event
  61.      **/
  62.     public function onKernelRequest($event)
  63.     {
  64.         if (!$event instanceof GetResponseEvent && !$event instanceof ResponseEvent) {
  65.             throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class, \is_object($event) ? \get_class($event) : \gettype($event)));
  66.         }
  67.         $url $event->getRequest()->getRequestUri();
  68.         if (!$this->adminRouteHelper->isAdminRoute($url)) {
  69.             return;
  70.         }
  71.         if ($this->tokenStorage->getToken()) {
  72.             $route $event->getRequest()->get('_route');
  73.             if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') && $route != 'fos_user_change_password') {
  74.                 $user $this->tokenStorage->getToken()->getUser();
  75.                 if ($user->isPasswordChanged() === false) {
  76.                     $response = new RedirectResponse($this->router->generate('fos_user_change_password'));
  77.                     $this->session->getFlashBag()->add(
  78.                         FlashTypes::DANGER,
  79.                         $this->translator->trans('kuma_admin.password_check.flash.error')
  80.                     );
  81.                     $event->setResponse($response);
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }