vendor/pimcore/portal-engine/src/Service/Security/Voter/DataPoolAccessVoter.php line 26

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\Service\Security\Voter;
  12. use Pimcore\Bundle\PortalEngineBundle\Enum\Permission;
  13. use Pimcore\Bundle\PortalEngineBundle\Model\DataObject\PortalUserInterface;
  14. use Pimcore\Bundle\PortalEngineBundle\Service\DataPool\DataPoolConfigService;
  15. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\Security\PermissionService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\Security\SecurityService;
  18. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Traits\SecurityServiceAware;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  22. class DataPoolAccessVoter extends Voter
  23. {
  24.     use SecurityServiceAware;
  25.     /**
  26.      * @var PortalConfigService
  27.      */
  28.     protected $portalConfigService;
  29.     /**
  30.      * @var DataPoolConfigService
  31.      */
  32.     protected $dataPoolConfigService;
  33.     /**
  34.      * @var EventDispatcherInterface
  35.      */
  36.     protected $eventDispatcher;
  37.     /**
  38.      * @var PermissionService
  39.      */
  40.     protected $permissionService;
  41.     /**
  42.      * @var SecurityService
  43.      */
  44.     protected $securityService;
  45.     /**
  46.      * DataPoolAccessVoter constructor.
  47.      *
  48.      * @param PortalConfigService $portalConfigService
  49.      * @param DataPoolConfigService $dataPoolConfigService
  50.      * @param EventDispatcherInterface $eventDispatcher
  51.      * @param PermissionService $permissionService
  52.      * @param SecurityService $securityService
  53.      */
  54.     public function __construct(
  55.         PortalConfigService $portalConfigService,
  56.         DataPoolConfigService $dataPoolConfigService,
  57.         EventDispatcherInterface $eventDispatcher,
  58.         PermissionService $permissionService,
  59.         SecurityService $securityService
  60.     ) {
  61.         $this->portalConfigService $portalConfigService;
  62.         $this->dataPoolConfigService $dataPoolConfigService;
  63.         $this->eventDispatcher $eventDispatcher;
  64.         $this->permissionService $permissionService;
  65.         $this->securityService $securityService;
  66.     }
  67.     protected function supports($attribute$subject): bool
  68.     {
  69.         return ($this->portalConfigService->isPortalEngineSite() || $this->securityService->isAdminPreviewCall() || $this->securityService->isAdminRestApiCall())
  70.                && $attribute === Permission::DATA_POOL_ACCESS;
  71.     }
  72.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  73.     {
  74.         if ($this->securityService->isAdminPreviewCall()) {
  75.             return true;
  76.         }
  77.         $currentDataPoolConfigId $this->dataPoolConfigService->getCurrentDataPoolConfig() ? $this->dataPoolConfigService->getCurrentDataPoolConfig()->getId() : 0;
  78.         $dataPoolId = !empty($subject) ? $subject $currentDataPoolConfigId;
  79.         $user $this->securityService->getPortalUser();
  80.         if (!$user instanceof PortalUserInterface) {
  81.             return false;
  82.         }
  83.         return $this->permissionService->isDataPoolAccessAllowed($user$dataPoolId);
  84.     }
  85. }