src/EventSubscriber/PermissionFilterSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\AdminFilterController;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. class PermissionFilterSubscriber implements EventSubscriberInterface
  9. {
  10.     protected AuthorizationCheckerInterface $authChecker;
  11.     public function __construct(AuthorizationCheckerInterface $authChecker)
  12.     {
  13.         $this->authChecker $authChecker;
  14.     }
  15.     public function onKernelController(ControllerEvent $event)
  16.     {
  17.         $controller $event->getController();
  18.         if (is_array($controller)) {
  19.             $controller $controller[0];
  20.         }
  21.         if ($controller instanceof AdminFilterController) {
  22.             $name $event->getRequest()->get('_route');
  23.             if (!$this->authChecker->isGranted('ROLE_' $name) and $name != 'app_admission_review_new' and $name != 'app_admission_review_request') {
  24.                 if (!$this->authChecker->isGranted('ROLE_SUPER_USER')) {
  25.                     throw new AccessDeniedHttpException("Хандах эрхгүй байна");
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             'kernel.controller' => 'onKernelController',
  34.         ];
  35.     }
  36. }