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.     /**
  68.      * @return bool
  69.      */
  70.     protected function supports($attribute$subject)
  71.     {
  72.         return ($this->portalConfigService->isPortalEngineSite() || $this->securityService->isAdminPreviewCall() || $this->securityService->isAdminRestApiCall())
  73.                && $attribute === Permission::DATA_POOL_ACCESS;
  74.     }
  75.     /**
  76.      * @param string $attribute
  77.      * @param mixed $subject
  78.      * @param TokenInterface $token
  79.      *
  80.      * @return bool
  81.      */
  82.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  83.     {
  84.         if ($this->securityService->isAdminPreviewCall()) {
  85.             return true;
  86.         }
  87.         $currentDataPoolConfigId $this->dataPoolConfigService->getCurrentDataPoolConfig() ? $this->dataPoolConfigService->getCurrentDataPoolConfig()->getId() : 0;
  88.         $dataPoolId = !empty($subject) ? $subject $currentDataPoolConfigId;
  89.         $user $this->securityService->getPortalUser();
  90.         if (!$user instanceof PortalUserInterface) {
  91.             return false;
  92.         }
  93.         return $this->permissionService->isDataPoolAccessAllowed($user$dataPoolId);
  94.     }
  95. }