vendor/ibexa/admin-ui/src/lib/EventListener/RequestListener.php line 55

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace Ibexa\AdminUi\EventListener;
  7. use Ibexa\Core\MVC\Symfony\SiteAccess;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\HttpKernelInterface;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class RequestListener implements EventSubscriberInterface
  14. {
  15.     /** @var array */
  16.     private $groupsBySiteAccess;
  17.     /**
  18.      * @param $groupsBySiteAccess
  19.      */
  20.     public function __construct(array $groupsBySiteAccess)
  21.     {
  22.         $this->groupsBySiteAccess $groupsBySiteAccess;
  23.     }
  24.     /**
  25.      * Returns an array of event names this subscriber wants to listen to.
  26.      *
  27.      * The array keys are event names and the value can be:
  28.      *
  29.      *  * The method name to call (priority defaults to 0)
  30.      *  * An array composed of the method name to call and the priority
  31.      *  * An array of arrays composed of the method names to call and respective
  32.      *    priorities, or 0 if unset
  33.      *
  34.      * For instance:
  35.      *
  36.      *  * array('eventName' => 'methodName')
  37.      *  * array('eventName' => array('methodName', $priority))
  38.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  39.      *
  40.      * @return array The event names to listen to
  41.      */
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             KernelEvents::REQUEST => ['onKernelRequest'13],
  46.         ];
  47.     }
  48.     public function onKernelRequest(RequestEvent $event)
  49.     {
  50.         if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  51.             return;
  52.         }
  53.         $requestAttributes $event->getRequest()->attributes;
  54.         $siteAccess $requestAttributes->get('siteaccess');
  55.         $allowedGroups $requestAttributes->get('siteaccess_group_whitelist');
  56.         if (!$siteAccess instanceof SiteAccess || empty($allowedGroups)) {
  57.             return;
  58.         }
  59.         $allowedGroups = (array)$allowedGroups;
  60.         foreach ($this->groupsBySiteAccess[$siteAccess->name] as $group) {
  61.             if (in_array($group$allowedGroupstrue)) {
  62.                 return;
  63.             }
  64.         }
  65.         throw new NotFoundHttpException('The route is not allowed in the current SiteAccess');
  66.     }
  67. }
  68. class_alias(RequestListener::class, 'EzSystems\EzPlatformAdminUi\EventListener\RequestListener');