vendor/pimcore/portal-engine/src/Service/Security/Voter/DataPoolVersionHistoryVoter.php line 29

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\Event\Permission\DataPoolVersionHistoryEvent;
  14. use Pimcore\Bundle\PortalEngineBundle\Model\DataObject\PortalUserInterface;
  15. use Pimcore\Bundle\PortalEngineBundle\Service\DataPool\DataPoolConfigService;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\DataPool\DataPoolService;
  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 Pimcore\Model\Element\ElementInterface;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  23. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  24. use Symfony\Component\Security\Core\Security;
  25. class DataPoolVersionHistoryVoter extends Voter
  26. {
  27.     use SecurityServiceAware;
  28.     /**
  29.      * @var PortalConfigService
  30.      */
  31.     protected $portalConfigService;
  32.     /**
  33.      * @var DataPoolConfigService
  34.      */
  35.     protected $dataPoolConfigService;
  36.     /**
  37.      * @var DataPoolService
  38.      */
  39.     protected $dataPoolService;
  40.     /**
  41.      * @var Security
  42.      */
  43.     protected $security;
  44.     /**
  45.      * @var PermissionService
  46.      */
  47.     protected $permissionService;
  48.     /**
  49.      * @var EventDispatcherInterface
  50.      */
  51.     protected $eventDispatcher;
  52.     /**
  53.      * DataPoolVersionHistoryVoter constructor.
  54.      *
  55.      * @param PortalConfigService $portalConfigService
  56.      * @param DataPoolConfigService $dataPoolConfigService
  57.      * @param Security $security
  58.      * @param DataPoolService $dataPoolService
  59.      * @param PermissionService $permissionService
  60.      * @param EventDispatcherInterface $eventDispatcher
  61.      */
  62.     public function __construct(PortalConfigService $portalConfigServiceDataPoolConfigService $dataPoolConfigServiceSecurity $securityDataPoolService $dataPoolServicePermissionService $permissionServiceEventDispatcherInterface $eventDispatcher)
  63.     {
  64.         $this->portalConfigService $portalConfigService;
  65.         $this->dataPoolConfigService $dataPoolConfigService;
  66.         $this->dataPoolService $dataPoolService;
  67.         $this->security $security;
  68.         $this->permissionService $permissionService;
  69.         $this->eventDispatcher $eventDispatcher;
  70.     }
  71.     /**
  72.      * @param string $attribute
  73.      * @param mixed $subject
  74.      *
  75.      * @return bool
  76.      */
  77.     protected function supports($attribute$subject)
  78.     {
  79.         return $this->portalConfigService->isPortalEngineSite()
  80.                && $attribute === Permission::VERSION_HISTORY;
  81.     }
  82.     /**
  83.      * @param string $attribute
  84.      * @param ElementInterface $subject
  85.      * @param TokenInterface $token
  86.      *
  87.      * @return bool
  88.      */
  89.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  90.     {
  91.         $allowed true;
  92.         if (!$this->security->isGranted(Permission::DATA_POOL_ACCESS)) {
  93.             $allowed false;
  94.         }
  95.         $dataPoolConfig $this->dataPoolConfigService->getCurrentDataPoolConfig();
  96.         if (!$dataPoolConfig->getEnableVersionHistory()) {
  97.             $allowed false;
  98.         }
  99.         if ($allowed) {
  100.             /**
  101.              * @var PortalUserInterface $user
  102.              */
  103.             $user $this->securityService->getPortalUser();
  104.             $allowed $this->permissionService->isAllowed($userPermission::VERSION_HISTORY Permission::PERMISSION_DELIMITER $dataPoolConfig->getId());
  105.         }
  106.         $event = new DataPoolVersionHistoryEvent($allowed$dataPoolConfig->getId(), $this->securityService->getPortalUser());
  107.         $this->eventDispatcher->dispatch($event);
  108.         return $event->isAllowed();
  109.     }
  110. }