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.     /**
  57.      * @return bool
  58.      */
  59.     protected function supports($attribute$subject)
  60.     {
  61.         return ($this->portalConfigService->isPortalEngineSite() || Tool::isFrontendRequestByAdmin() || $this->securityService->isAdminRestApiCall())
  62.                && $attribute === Permission::DATA_POOL_ACCESS;
  63.     }
  64.     /**
  65.      * @param string $attribute
  66.      * @param mixed $subject
  67.      * @param TokenInterface $token
  68.      *
  69.      * @return bool
  70.      */
  71.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  72.     {
  73.         if (Tool::isFrontendRequestByAdmin()) {
  74.             return true;
  75.         }
  76.         $currentDataPoolConfigId $this->dataPoolConfigService->getCurrentDataPoolConfig() ? $this->dataPoolConfigService->getCurrentDataPoolConfig()->getId() : 0;
  77.         $dataPoolId = !empty($subject) ? $subject $currentDataPoolConfigId;
  78.         $user $this->securityService->getPortalUser();
  79.         if (!$user instanceof PortalUserInterface) {
  80.             return false;
  81.         }
  82.         return $this->permissionService->isDataPoolAccessAllowed($user$dataPoolId);
  83.     }
  84. }