src/EventListener/CheckAdminPathListener.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Users;
  4. use Symfony\Component\Security\Core\Security;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  9. class CheckAdminPathListener implements EventSubscriberInterface
  10. {
  11.     private $security;
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             KernelEvents::REQUEST =>[
  20.                 ['onKernelRequest',  -10], 
  21.             ],
  22.         ];
  23.     }
  24.     public function onKernelRequest(RequestEvent $event)
  25.     {
  26.         $request $event->getRequest();
  27.         $pathInfo $request->getPathInfo();
  28.         if (substr($pathInfo07) === '/admin/') {
  29.             $token $this->security->getToken();
  30.             if ($token && $token->getUser() && in_array("ROLE_ADMIN"$token->getUser()->getRoles())) {
  31.                 $attributes $token->getAttributes();
  32.                 if (isset($attributes['custom_roles']) && $attributes['custom_roles'] == Users::ROLE_ADMIN_CLIENT_ACCESS) {
  33.                     throw new AccessDeniedException('You do not have access to this resource.');
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }