src/EventListener/FirewallPartnerListener.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. class FirewallPartnerListener
  9. {
  10.     /** @var TokenStorageInterface $tokenStorage */
  11.     private $tokenStorage;
  12.     /** @var AuthorizationCheckerInterface $authorizationChecker */
  13.     private $authorizationChecker;
  14.     /** @var Router $router */
  15.     private $router;
  16.     public function __construct(TokenStorageInterface         $tokenStorage,
  17.                                 AuthorizationCheckerInterface $authorizationChecker,
  18.                                 Router                        $router
  19.     )
  20.     {
  21.         $this->tokenStorage $tokenStorage;
  22.         $this->authorizationChecker $authorizationChecker;
  23.         $this->router $router;
  24.     }
  25.     public function onKernelRequest(RequestEvent $event)
  26.     {
  27.         $controller $event->getRequest()->get('_controller');
  28.         $parts explode(':'$controller);
  29.         $currentController $parts[0];
  30.         $currentAction = empty($parts[1]) ? null $parts[1];
  31.         if (!$this->tokenStorage->getToken() ||
  32.             false === $this->authorizationChecker->isGranted('ROLE_ADMINISTRATION_ACCESS') ||
  33.             !($user $this->tokenStorage->getToken()->getUser()) ||
  34.             !$user->getPartner() ||
  35.             strpos($controller'exception') !== false ||
  36.             $this->isAllowedController($currentController$currentAction)
  37.         ) {
  38.             return;
  39.         }
  40.         $event->setResponse(new RedirectResponse($this->router->generate('sylius_admin_dashboard')));
  41.     }
  42.     private function isAllowedController(string $controllerstring $action null): bool
  43.     {
  44.         if (!empty($action) && $controller === 'app.controller.order.grid' &&
  45.             preg_match('/(logs|downloadImportedFile)/i'$action)
  46.         ) {
  47.             return false;
  48.         }
  49.         switch ($controller) {
  50.             case 'app.controller.order.grid':
  51.             case 'app.controller.partner.partner':
  52.             case 'app.controller.rca.rca_admin_grid';
  53.             case 'app.controller.cms_rca';
  54.             case 'app.controller.road_assistance.admin_road_assistance':
  55.             case 'app.controller.vignette.ro_vignette_admin_grid':
  56.             case 'app.controller.license_recovery.license_recovery_admin':
  57.             case 'app.controller.telemedicine.telemedicine_admin':
  58.             case 'sylius.controller.admin.dashboard':
  59.             case 'sylius.controller.admin_user':
  60.             case 'sylius.controller.channel':
  61.             case 'sylius.controller.order':
  62.             case 'sylius_admin_admin_user_update':
  63.                 return true;
  64.             default:
  65.                 return false;
  66.         }
  67.     }
  68. }