<?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.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
protected function supports($attribute, $subject)
{
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.
*
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var PortalUserInterface|Concrete $user */
$user = $this->securityService->getPortalUser();
return $this->guestUserPermissionService->checkGuestUserPermissions($attribute, $user);
}
}