<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class RequestListener implements EventSubscriberInterface
{
private $authorizationChecker;
private $params;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, ParameterBagInterface $params)
{
$this->authorizationChecker = $authorizationChecker;
$this->params = $params;
}
public function onKernelRequest(RequestEvent $event)
{
if ($this->params->get('kernel.environment') !== 'dev' and !$this->authorizationChecker->isGranted('access_route'))
$event->setResponse(new JsonResponse(null, 401));
}
public static function getSubscribedEvents()
{
return [
'kernel.request' => 'onKernelRequest',
];
}
}