vendor/pimcore/portal-engine/src/Service/Security/Voter/DataPoolItemPermissionVoter.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\Service\DataPool\DataPoolConfigService;
  14. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  15. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\Security\PermissionService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Traits\SecurityServiceAware;
  18. use Pimcore\Model\Asset;
  19. use Pimcore\Model\Element\ElementInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  22. class DataPoolItemPermissionVoter extends Voter
  23. {
  24.     use SecurityServiceAware;
  25.     const PERMISSIONS = [
  26.         Permission::CREATE,
  27.         Permission::DELETE,
  28.         Permission::EDIT,
  29.         Permission::VIEW,
  30.         Permission::UPDATE,
  31.         Permission::DOWNLOAD,
  32.         Permission::SUBFOLDER,
  33.         Permission::VIEW_OWNED_ASSET_ONLY,
  34.     ];
  35.     /**
  36.      * @var PortalConfigService
  37.      */
  38.     protected $portalConfigService;
  39.     /**
  40.      * @var DataPoolConfigService
  41.      */
  42.     protected $dataPoolConfigService;
  43.     /**
  44.      * @var PermissionService
  45.      */
  46.     protected $permissionService;
  47.     /**
  48.      * @var SearchIndex\Asset\SearchService
  49.      */
  50.     protected $assetSearchService;
  51.     /**
  52.      * @var SearchIndex\DataObject\SearchService
  53.      */
  54.     protected $objectSearchService;
  55.     /**
  56.      * @param PortalConfigService $portalConfigService
  57.      * @param DataPoolConfigService $dataPoolConfigService
  58.      * @param PermissionService $permissionService
  59.      */
  60.     public function __construct(
  61.         PortalConfigService $portalConfigService,
  62.         DataPoolConfigService $dataPoolConfigService,
  63.         PermissionService $permissionService,
  64.         SearchIndex\Asset\SearchService $assetSearchService,
  65.         SearchIndex\DataObject\SearchService $objectSearchService)
  66.     {
  67.         $this->portalConfigService $portalConfigService;
  68.         $this->dataPoolConfigService $dataPoolConfigService;
  69.         $this->permissionService $permissionService;
  70.         $this->assetSearchService $assetSearchService;
  71.         $this->objectSearchService $objectSearchService;
  72.     }
  73.     /**
  74.      * @param string $attribute
  75.      * @param mixed $subject
  76.      *
  77.      * @return bool
  78.      */
  79.     protected function supports($attribute$subject)
  80.     {
  81.         return $this->portalConfigService->isPortalEngineSite()
  82.             && in_array($attributeself::PERMISSIONS)
  83.             && (is_string($subject) || $subject instanceof ElementInterface);
  84.     }
  85.     /**
  86.      * @param string $attribute
  87.      * @param mixed $subject
  88.      * @param TokenInterface $token
  89.      *
  90.      * @return bool
  91.      */
  92.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  93.     {
  94.         $dataPoolConfig $this->dataPoolConfigService->getCurrentDataPoolConfig();
  95.         if (empty($dataPoolConfig)) {
  96.             return false;
  97.         }
  98.         $respectWorkflowPermissions $subject instanceof Asset;
  99.         $respectUploadFolderPermissions $subject instanceof Asset;
  100.         if ($subject instanceof ElementInterface) {
  101.             return $this->permissionService->isPermissionAllowedConsiderPreconditions(
  102.                 $attribute,
  103.                 $this->securityService->getPortalUser(),
  104.                 $dataPoolConfig->getId(),
  105.                 $subject,
  106.                 false,
  107.                 $respectWorkflowPermissions,
  108.                 true,
  109.                 $respectUploadFolderPermissions
  110.             );
  111.         }
  112.         return $this->permissionService->isPermissionAllowed(
  113.             $attribute,
  114.             $this->securityService->getPortalUser(),
  115.             $dataPoolConfig->getId(),
  116.             $subject,
  117.             false,
  118.             $respectWorkflowPermissions,
  119.             true,
  120.             $respectUploadFolderPermissions
  121.         );
  122.     }
  123. }