vendor/pimcore/portal-engine/src/Service/Security/Voter/DataPoolItemPermissionVoter.php line 25

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\Security\PermissionService;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Traits\SecurityServiceAware;
  17. use Pimcore\Model\Asset;
  18. use Pimcore\Model\Element\ElementInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  21. class DataPoolItemPermissionVoter extends Voter
  22. {
  23.     use SecurityServiceAware;
  24.     const PERMISSIONS = [
  25.         Permission::CREATE,
  26.         Permission::DELETE,
  27.         Permission::EDIT,
  28.         Permission::VIEW,
  29.         Permission::UPDATE,
  30.         Permission::DOWNLOAD,
  31.         Permission::SUBFOLDER,
  32.         Permission::VIEW_OWNED_ASSET_ONLY,
  33.     ];
  34.     /**
  35.      * @var PortalConfigService
  36.      */
  37.     protected $portalConfigService;
  38.     /**
  39.      * @var DataPoolConfigService
  40.      */
  41.     protected $dataPoolConfigService;
  42.     /**
  43.      * @var PermissionService
  44.      */
  45.     protected $permissionService;
  46.     /**
  47.      * @param PortalConfigService $portalConfigService
  48.      * @param DataPoolConfigService $dataPoolConfigService
  49.      * @param PermissionService $permissionService
  50.      */
  51.     public function __construct(PortalConfigService $portalConfigServiceDataPoolConfigService $dataPoolConfigServicePermissionService $permissionService)
  52.     {
  53.         $this->portalConfigService $portalConfigService;
  54.         $this->dataPoolConfigService $dataPoolConfigService;
  55.         $this->permissionService $permissionService;
  56.     }
  57.     /**
  58.      * @param string $attribute
  59.      * @param mixed $subject
  60.      *
  61.      * @return bool
  62.      */
  63.     protected function supports($attribute$subject)
  64.     {
  65.         return $this->portalConfigService->isPortalEngineSite()
  66.             && in_array($attributeself::PERMISSIONS)
  67.             && (is_string($subject) || $subject instanceof ElementInterface);
  68.     }
  69.     /**
  70.      * @param string $attribute
  71.      * @param mixed $subject
  72.      * @param TokenInterface $token
  73.      *
  74.      * @return bool
  75.      */
  76.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  77.     {
  78.         $dataPoolConfig $this->dataPoolConfigService->getCurrentDataPoolConfig();
  79.         if (empty($dataPoolConfig)) {
  80.             return false;
  81.         }
  82.         $fullPath $subject instanceof ElementInterface $subject->getRealFullPath() : $subject;
  83.         $respectWorkflowPermissions $subject instanceof Asset;
  84.         $respectUploadFolderPermissions $subject instanceof Asset;
  85.         return $this->permissionService->isPermissionAllowed(
  86.             $attribute,
  87.             $this->securityService->getPortalUser(),
  88.             $dataPoolConfig->getId(),
  89.             $fullPath,
  90.             false,
  91.             $respectWorkflowPermissions,
  92.             true,
  93.             $respectUploadFolderPermissions
  94.         );
  95.     }
  96. }