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\Traits\SecurityServiceAware;
  18. use Pimcore\Tool;
  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.      * DataPoolAccessVoter constructor.
  43.      *
  44.      * @param PortalConfigService $portalConfigService
  45.      * @param DataPoolConfigService $dataPoolConfigService
  46.      * @param EventDispatcherInterface $eventDispatcher
  47.      * @param PermissionService $permissionService
  48.      */
  49.     public function __construct(PortalConfigService $portalConfigServiceDataPoolConfigService $dataPoolConfigServiceEventDispatcherInterface $eventDispatcherPermissionService $permissionService)
  50.     {
  51.         $this->portalConfigService $portalConfigService;
  52.         $this->dataPoolConfigService $dataPoolConfigService;
  53.         $this->eventDispatcher $eventDispatcher;
  54.         $this->permissionService $permissionService;
  55.     }
  56.     protected function supports($attribute$subject)
  57.     {
  58.         return ($this->portalConfigService->isPortalEngineSite() || Tool::isFrontendRequestByAdmin() || $this->securityService->isAdminRestApiCall())
  59.                && $attribute === Permission::DATA_POOL_ACCESS;
  60.     }
  61.     /**
  62.      * @param string $attribute
  63.      * @param mixed $subject
  64.      * @param TokenInterface $token
  65.      *
  66.      * @return bool
  67.      */
  68.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  69.     {
  70.         if (Tool::isFrontendRequestByAdmin()) {
  71.             return true;
  72.         }
  73.         $currentDataPoolConfigId $this->dataPoolConfigService->getCurrentDataPoolConfig() ? $this->dataPoolConfigService->getCurrentDataPoolConfig()->getId() : 0;
  74.         $dataPoolId = !empty($subject) ? $subject $currentDataPoolConfigId;
  75.         $user $this->securityService->getPortalUser();
  76.         if (!$user instanceof PortalUserInterface) {
  77.             return false;
  78.         }
  79.         return $this->permissionService->isDataPoolAccessAllowed($user$dataPoolId);
  80.     }
  81. }