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.     protected function supports($attribute$subject)
  58.     {
  59.         return $this->portalConfigService->isPortalEngineSite()
  60.                && $attribute === Permission::PORTAL_ACCESS;
  61.     }
  62.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  63.     {
  64.         $portalId $this->portalConfigService->getCurrentPortalConfig()->getPortalId();
  65.         /**
  66.          * @var PortalUserInterface|Concrete $user
  67.          */
  68.         $user $this->securityService->getPortalUser();
  69.         $allowed $user instanceof PortalUserInterface
  70.             && $this->permissionService->isAllowed($userPermission::PORTAL_ACCESS Permission::PERMISSION_DELIMITER $portalId);
  71.         $event = new PortalAccessEvent($allowed$portalId$token);
  72.         $this->eventDispatcher->dispatch($event);
  73.         return $event->isAllowed();
  74.     }
  75. }