<?php
namespace SymfonyDev\AppBundle\EventListener;
use SymfonyDev\AppBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RequestListener
{
protected $security;
protected $em;
protected $router;
public function __construct(Security $security, EntityManagerInterface $em, UrlGeneratorInterface $router)
{
$this->security = $security;
$this->em = $em;
$this->router = $router;
}
public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$allowRoutes = [
'app_profile_profile_change_password'
];
$user = $this->security->getUser();
if ($user && $user instanceof User) {
$request = $event->getRequest();
if (!in_array($request->attributes->get('_route'), $allowRoutes) && !$user->getIsPwdSecure()) {
$event->setResponse(new RedirectResponse($this->router->generate('app_profile_profile_change_password').'?sec=1'));
}
}
}
}