vendor/pimcore/portal-engine/src/Service/Security/Voter/DataPoolAssetUploadFolderReviewingVoter.php line 32

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\Configuration\DataPool\AssetConfig;
  14. use Pimcore\Bundle\PortalEngineBundle\Model\Configuration\DataPool\DataPoolConfigInterface;
  15. use Pimcore\Bundle\PortalEngineBundle\Model\DataObject\PortalUserInterface;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\DataPool\DataPoolConfigService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  18. use Pimcore\Bundle\PortalEngineBundle\Service\Security\PermissionService;
  19. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Traits\SecurityServiceAware;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  22. use Symfony\Component\Security\Core\Security;
  23. /**
  24.  * Class DataPoolAssetUploadFolderReviewingVoter
  25.  *
  26.  * @package Pimcore\Bundle\PortalEngineBundle\Service\Security\Voter
  27.  */
  28. class DataPoolAssetUploadFolderReviewingVoter extends Voter
  29. {
  30.     use SecurityServiceAware;
  31.     /**
  32.      * @var PortalConfigService
  33.      */
  34.     protected $portalConfigService;
  35.     /**
  36.      * @var Security
  37.      */
  38.     protected $security;
  39.     /**
  40.      * @var PermissionService
  41.      */
  42.     protected $permissionService;
  43.     /**
  44.      * @var DataPoolConfigService
  45.      */
  46.     protected $dataPoolConfigService;
  47.     /**
  48.      * DataPoolAssetUploadFolderReviewingVoter constructor.
  49.      *
  50.      * @param PortalConfigService $portalConfigService
  51.      * @param Security $security
  52.      * @param PermissionService $permissionService
  53.      * @param DataPoolConfigService $dataPoolConfigService
  54.      */
  55.     public function __construct(PortalConfigService $portalConfigServiceSecurity $securityPermissionService $permissionServiceDataPoolConfigService $dataPoolConfigService)
  56.     {
  57.         $this->portalConfigService $portalConfigService;
  58.         $this->security $security;
  59.         $this->permissionService $permissionService;
  60.         $this->dataPoolConfigService $dataPoolConfigService;
  61.     }
  62.     /**
  63.      * Determines if the attribute and subject are supported by this voter.
  64.      *
  65.      * @param string $attribute An attribute
  66.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  67.      *
  68.      * @return bool True if the attribute and subject are supported, false otherwise
  69.      */
  70.     protected function supports($attribute$subject)
  71.     {
  72.         return $this->portalConfigService->isPortalEngineSite()
  73.             && $attribute === Permission::DATA_POOL_ASSET_UPLOAD_FOLDER_REVIEWING;
  74.     }
  75.     /**
  76.      * Perform a single access check operation on a given attribute, subject and token.
  77.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  78.      *
  79.      * @param string $attribute
  80.      * @param mixed $subject
  81.      *
  82.      * @return bool
  83.      */
  84.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  85.     {
  86.         /** @var bool $allowed */
  87.         $allowed true;
  88.         try {
  89.             if (!$this->security->isGranted(Permission::DATA_POOL_ACCESS)) {
  90.                 throw new \Exception('dataPool access not granted');
  91.             }
  92.             /** @var DataPoolConfigInterface $dataPoolConfig */
  93.             $dataPoolConfig $this->dataPoolConfigService->getCurrentDataPoolConfig();
  94.             if (!$dataPoolConfig instanceof AssetConfig) {
  95.                 throw new \Exception('current dataPoolConfig is not a AssetConfig');
  96.             }
  97.             /** @var PortalUserInterface $user */
  98.             $user $this->securityService->getPortalUser();
  99.             if (!$this->permissionService->isAllowed($userPermission::DATA_POOL_ASSET_UPLOAD_FOLDER_REVIEWING Permission::PERMISSION_DELIMITER $dataPoolConfig->getId())) {
  100.                 throw new \Exception('permission not allowed');
  101.             }
  102.         } catch (\Exception $e) {
  103.             $allowed false;
  104.         }
  105.         return $allowed;
  106.     }
  107. }