vendor/pimcore/portal-engine/src/Service/Security/Voter/PortalAccessVoter.php line 27

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\PortalAccessEvent;
  14. use Pimcore\Bundle\PortalEngineBundle\Model\DataObject\PortalUserInterface;
  15. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\Security\PermissionService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\Security\Traits\SecurityServiceAware;
  18. use Pimcore\Model\DataObject\Concrete;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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. class PortalAccessVoter extends Voter
  24. {
  25.     use SecurityServiceAware;
  26.     /**
  27.      * @var PortalConfigService
  28.      */
  29.     protected $portalConfigService;
  30.     /**
  31.      * @var Security $security
  32.      */
  33.     protected $security;
  34.     /**
  35.      * @var EventDispatcherInterface
  36.      */
  37.     protected $eventDispatcher;
  38.     /**
  39.      * @var PermissionService
  40.      */
  41.     protected $permissionService;
  42.     /**
  43.      * PortalAccessVoter constructor.
  44.      *
  45.      * @param PortalConfigService $portalConfigService
  46.      * @param Security $security
  47.      * @param EventDispatcherInterface $eventDispatcher
  48.      * @param PermissionService $permissionService
  49.      */
  50.     public function __construct(PortalConfigService $portalConfigServiceSecurity $securityEventDispatcherInterface $eventDispatcherPermissionService $permissionService)
  51.     {
  52.         $this->portalConfigService $portalConfigService;
  53.         $this->security $security;
  54.         $this->eventDispatcher $eventDispatcher;
  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.                && $attribute === Permission::PORTAL_ACCESS;
  67.     }
  68.     /**
  69.      * @param string $attribute
  70.      * @param mixed $subject
  71.      * @param TokenInterface $token
  72.      *
  73.      * @return bool
  74.      */
  75.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  76.     {
  77.         $portalId $this->portalConfigService->getCurrentPortalConfig()->getPortalId();
  78.         /**
  79.          * @var PortalUserInterface|Concrete $user
  80.          */
  81.         $user $this->securityService->getPortalUser();
  82.         $allowed $user instanceof PortalUserInterface
  83.             && $this->permissionService->isAllowed($userPermission::PORTAL_ACCESS Permission::PERMISSION_DELIMITER $portalId);
  84.         $event = new PortalAccessEvent($allowed$portalId$token);
  85.         $this->eventDispatcher->dispatch($event);
  86.         return $event->isAllowed();
  87.     }
  88. }