src/EventListener/RequestListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class RequestListener implements EventSubscriberInterface
  9. {
  10.         private $authorizationChecker;
  11.         private $params;
  12.         
  13.         public function __construct(AuthorizationCheckerInterface $authorizationCheckerParameterBagInterface $params)
  14.         {
  15.                 $this->authorizationChecker $authorizationChecker;
  16.                 $this->params $params;
  17.         }
  18.         
  19.         public function onKernelRequest(RequestEvent $event)
  20.         {
  21.                 if ($this->params->get('kernel.environment') !== 'dev' and !$this->authorizationChecker->isGranted('access_route'))
  22.                         $event->setResponse(new JsonResponse(null401));
  23.                 
  24.         }
  25.         
  26.         public static function getSubscribedEvents()
  27.         {
  28.                 return [
  29.                             'kernel.request' => 'onKernelRequest',
  30.                 ];
  31.         }
  32. }