<?php
namespace App\EventSubscriber;
use App\Controller\AdminFilterController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class PermissionFilterSubscriber implements EventSubscriberInterface
{
protected AuthorizationCheckerInterface $authChecker;
public function __construct(AuthorizationCheckerInterface $authChecker)
{
$this->authChecker = $authChecker;
}
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
if (is_array($controller)) {
$controller = $controller[0];
}
if ($controller instanceof AdminFilterController) {
$name = $event->getRequest()->get('_route');
if (!$this->authChecker->isGranted('ROLE_' . $name) and $name != 'app_admission_review_new' and $name != 'app_admission_review_request') {
if (!$this->authChecker->isGranted('ROLE_SUPER_USER')) {
throw new AccessDeniedHttpException("Хандах эрхгүй байна");
}
}
}
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'onKernelController',
];
}
}