vendor/ibexa/corporate-account/src/bundle/EventSubscriber/AbstractViewSubscriber.php line 38

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. declare(strict_types=1);
  7. namespace Ibexa\Bundle\CorporateAccount\EventSubscriber;
  8. use Ibexa\Bundle\AdminUi\IbexaAdminUiBundle;
  9. use Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent;
  10. use Ibexa\Core\MVC\Symfony\MVCEvents;
  11. use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface;
  12. use Ibexa\Core\MVC\Symfony\View\View;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. abstract class AbstractViewSubscriber implements EventSubscriberInterface
  15. {
  16.     private SiteAccessServiceInterface $siteAccessService;
  17.     public function __construct(
  18.         SiteAccessServiceInterface $siteAccessService
  19.     ) {
  20.         $this->siteAccessService $siteAccessService;
  21.     }
  22.     /**
  23.      * @return array<string,string>
  24.      */
  25.     final public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
  29.         ];
  30.     }
  31.     final public function onPreContentView(PreContentViewEvent $event): void
  32.     {
  33.         $view $event->getContentView();
  34.         if ($this->supports($view) && $this->isAdminSiteAccess()) {
  35.             $this->configureView($view);
  36.         }
  37.     }
  38.     /**
  39.      * Returns true if given $view is supported by subscriber.
  40.      */
  41.     abstract protected function supports(View $view): bool;
  42.     abstract protected function configureView(View $view): void;
  43.     /**
  44.      * Returns true if current siteaccess is administrative.
  45.      */
  46.     private function isAdminSiteAccess(): bool
  47.     {
  48.         $currentSiteAccess $this->siteAccessService->getCurrent();
  49.         if ($currentSiteAccess === null) {
  50.             return false;
  51.         }
  52.         // Workaround: for some reason value returned from SiteAccessServiceInterface::getCurrent() doesn't
  53.         // contains groups
  54.         $currentSiteAccess $this->siteAccessService->get($currentSiteAccess->name);
  55.         foreach ($currentSiteAccess->groups as $group) {
  56.             if ($group->getName() === IbexaAdminUiBundle::ADMIN_GROUP_NAME) {
  57.                 return true;
  58.             }
  59.         }
  60.         return false;
  61.     }
  62. }