<?php
/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/
namespace Pimcore\Bundle\PortalEngineBundle\Service\Security\Voter;
use Pimcore\Bundle\PortalEngineBundle\Model\DataObject\PortalUserInterface;
use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
use Pimcore\Bundle\PortalEngineBundle\Service\Security\GuestUserPermissionService;
use Pimcore\Bundle\PortalEngineBundle\Service\Security\Traits\SecurityServiceAware;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class GuestUserPermissionVoter extends Voter
{
use SecurityServiceAware;
/**
* @var PortalConfigService
*/
protected $portalConfigService;
/**
* @var GuestUserPermissionService
*/
protected $guestUserPermissionService;
/**
* @param PortalConfigService $portalConfigService
* @param GuestUserPermissionService $guestUserPermissionService
*/
public function __construct(PortalConfigService $portalConfigService, GuestUserPermissionService $guestUserPermissionService)
{
$this->portalConfigService = $portalConfigService;
$this->guestUserPermissionService = $guestUserPermissionService;
}
/**
* Determines if the attribute and subject are supported by this voter.
*
*/
protected function supports(string $attribute, mixed $subject): bool
{
return $this->portalConfigService->isPortalEngineSite() &&
in_array($attribute, $this->guestUserPermissionService->getGuestUserRelevantPermissions());
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
*/
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var PortalUserInterface|Concrete $user */
$user = $this->securityService->getPortalUser();
return $this->guestUserPermissionService->checkGuestUserPermissions($attribute, $user);
}
}