src/SymfonyDev/AppBundle/EventListener/RequestListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace SymfonyDev\AppBundle\EventListener;
  3. use SymfonyDev\AppBundle\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. class RequestListener
  10. {
  11.     protected $security;
  12.     protected $em;
  13.     protected $router;
  14.     public function __construct(Security $securityEntityManagerInterface $emUrlGeneratorInterface $router)
  15.     {
  16.         $this->security $security;
  17.         $this->em $em;
  18.         $this->router $router;
  19.     }
  20.     public function onKernelRequest(GetResponseEvent $event)
  21.     {
  22.         if (!$event->isMasterRequest()) {
  23.             return;
  24.         }
  25.         $allowRoutes = [
  26.             'app_profile_profile_change_password'
  27.         ];
  28.         $user $this->security->getUser();
  29.         if ($user && $user instanceof User) {
  30.             $request $event->getRequest();
  31.             if (!in_array($request->attributes->get('_route'), $allowRoutes) && !$user->getIsPwdSecure()) {
  32.                 $event->setResponse(new RedirectResponse($this->router->generate('app_profile_profile_change_password').'?sec=1'));
  33.             }
  34.         }
  35.     }
  36. }