<?php
namespace App\EventListener;
use App\RBAC\PermissionsInterface;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class FirewallRcaPartnersListener
{
/** @var TokenStorageInterface $tokenStorage */
private $tokenStorage;
/** @var AuthorizationCheckerInterface $authorizationChecker */
private $authorizationChecker;
/** @var Router $router */
private $router;
public function __construct(TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $authorizationChecker,
Router $router
)
{
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
$this->router = $router;
}
public function onKernelRequest(RequestEvent $event)
{
$controller = $event->getRequest()->get('_controller');
$parts = explode(':', $controller);
$currentController = $parts[0];
$currentAction = empty($parts[1]) ? null : $parts[1];
if (!$this->tokenStorage->getToken() ||
false === $this->authorizationChecker->isGranted('ROLE_ADMINISTRATION_ACCESS') ||
!($user = $this->tokenStorage->getToken()->getUser()) ||
!$user->hasPermission(PermissionsInterface::TYPE_RCA_PARTNER) ||
strpos($controller, 'exception') !== false ||
$this->isAllowedController($currentController, $currentAction)
) {
return;
}
$event->setResponse(new RedirectResponse($this->router->generate('admin_report_products')));
}
private function isAllowedController(string $controller, string $action = null): bool
{
if (!empty($action) && $controller === 'app.controller.order.grid' &&
preg_match('/(logs|downloadImportedFile)/i', $action)
) {
return false;
}
switch ($controller) {
case 'app.controller.order.grid':
case 'sylius.controller.channel':
case 'sylius.controller.admin_user':
return true;
default:
return false;
}
}
}