vendor/ibexa/admin-ui/src/lib/EventListener/RequestAttributesListener.php line 56

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\Bundle\AdminUi\IbexaAdminUiBundle;
  8. use Ibexa\Contracts\Core\Repository\Repository;
  9. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  10. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  11. use Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
  12. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\ParameterBag;
  15. use Symfony\Component\HttpFoundation\Request;
  16. /**
  17.  * Collects parameters for the ViewBuilder from the Request.
  18.  */
  19. class RequestAttributesListener implements EventSubscriberInterface
  20. {
  21.     private const TRANSLATED_CONTENT_VIEW_ROUTE_NAME 'ibexa.content.translation.view';
  22.     /** @var \Ibexa\Contracts\Core\Repository\Repository */
  23.     private $repository;
  24.     /** @var array */
  25.     private $siteAccessGroups;
  26.     /**
  27.      * @param array $siteAccessGroups
  28.      * @param \Ibexa\Contracts\Core\Repository\Repository $repository
  29.      */
  30.     public function __construct(array $siteAccessGroupsRepository $repository)
  31.     {
  32.         $this->repository $repository;
  33.         $this->siteAccessGroups $siteAccessGroups;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'addRequestAttributes'];
  38.     }
  39.     /**
  40.      * Adds all the request attributes to the parameters.
  41.      *
  42.      * @param \Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent $event
  43.      *
  44.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  45.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  46.      */
  47.     public function addRequestAttributes(FilterViewBuilderParametersEvent $event)
  48.     {
  49.         $request $event->getRequest();
  50.         if (!$this->isAdmin($request)) {
  51.             return;
  52.         }
  53.         $parameterBag $event->getParameters();
  54.         if ($parameterBag->has('locationId') && null !== $parameterBag->get('locationId')) {
  55.             $location $this->loadLocation((int)$parameterBag->get('locationId'));
  56.             $parameterBag->set('location'$location);
  57.         }
  58.         if ($this->hasContentLanguage($request$parameterBag)) {
  59.             /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location $location */
  60.             $location $parameterBag->get('location');
  61.             $languageCode $parameterBag->get('languageCode');
  62.             $content $this->loadContent($location->contentInfo->id$languageCode);
  63.             $parameterBag->set('content'$content);
  64.         }
  65.     }
  66.     /**
  67.      * @param \Symfony\Component\HttpFoundation\Request $request
  68.      * @param \Symfony\Component\HttpFoundation\ParameterBag $parameterBag
  69.      *
  70.      * @return bool
  71.      */
  72.     private function hasContentLanguage(Request $requestParameterBag $parameterBag): bool
  73.     {
  74.         return $parameterBag->has('languageCode')
  75.             && $parameterBag->has('location')
  76.             && $request->get('_route') === self::TRANSLATED_CONTENT_VIEW_ROUTE_NAME;
  77.     }
  78.     /**
  79.      * @param int $locationId
  80.      *
  81.      * @return \Ibexa\Contracts\Core\Repository\Values\Content\Location
  82.      */
  83.     private function loadLocation(int $locationId): Location
  84.     {
  85.         $location $this->repository->sudo(
  86.             static function (Repository $repository) use ($locationId) {
  87.                 return $repository->getLocationService()->loadLocation($locationId);
  88.             }
  89.         );
  90.         return $location;
  91.     }
  92.     /**
  93.      * @param int $contentId
  94.      * @param string $language
  95.      *
  96.      * @return \Ibexa\Contracts\Core\Repository\Values\Content\Content
  97.      *
  98.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  99.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  100.      */
  101.     private function loadContent(int $contentId, ?string $language): Content
  102.     {
  103.         return $this->repository->getContentService()->loadContent($contentId$language ? [$language] : null);
  104.     }
  105.     private function isAdmin(Request $request): bool
  106.     {
  107.         $siteAccess $request->attributes->get('siteaccess');
  108.         return \in_array($siteAccess->name$this->siteAccessGroups[IbexaAdminUiBundle::ADMIN_GROUP_NAME], true);
  109.     }
  110. }
  111. class_alias(RequestAttributesListener::class, 'EzSystems\EzPlatformAdminUi\EventListener\RequestAttributesListener');