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.     protected function supports($attribute$subject)
  72.     {
  73.         return $this->portalConfigService->isPortalEngineSite()
  74.                && $attribute === Permission::VERSION_HISTORY;
  75.     }
  76.     /**
  77.      * @param string $attribute
  78.      * @param ElementInterface $subject
  79.      * @param TokenInterface $token
  80.      *
  81.      * @return bool
  82.      */
  83.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  84.     {
  85.         $allowed true;
  86.         if (!$this->security->isGranted(Permission::DATA_POOL_ACCESS)) {
  87.             $allowed false;
  88.         }
  89.         $dataPoolConfig $this->dataPoolConfigService->getCurrentDataPoolConfig();
  90.         if (!$dataPoolConfig->getEnableVersionHistory()) {
  91.             $allowed false;
  92.         }
  93.         if ($allowed) {
  94.             /**
  95.              * @var PortalUserInterface $user
  96.              */
  97.             $user $this->securityService->getPortalUser();
  98.             $allowed $this->permissionService->isAllowed($userPermission::VERSION_HISTORY Permission::PERMISSION_DELIMITER $dataPoolConfig->getId());
  99.         }
  100.         $event = new DataPoolVersionHistoryEvent($allowed$dataPoolConfig->getId(), $this->securityService->getPortalUser());
  101.         $this->eventDispatcher->dispatch($event);
  102.         return $event->isAllowed();
  103.     }
  104. }