<?php
namespace App\Package\Admin\Tools\SecurityVoter;
use Symfony\Component\HttpFoundation\RequestStack,
Symfony\Component\Security\Core\Authorization\Voter\VoterInterface,
Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use App\Package\Toolkit\ApplicationMode\ApplicationMode,
App\Package\Admin\Tools\AdminState\AdminState,
App\Package\Admin\Main\EntityInterface\AdminInterface;
/**
* SecurityVoter
*
* Security voter for administration panel
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright Openform
* @since 03.2019
*/
class SecurityVoter implements VoterInterface
{
/**
* @var ApplicationMode
*/
protected $applicationMode;
/**
* @var AdminState
*/
protected $adminState;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @param ApplicationMode $applicationMode
* @param AdminState $adminState
* @param RequestStack $requestStack
*/
public function __construct(ApplicationMode $applicationMode, AdminState $adminState, RequestStack $requestStack)
{
$this->applicationMode = $applicationMode;
$this->adminState = $adminState;
$this->requestStack = $requestStack;
}
/**
* {@inheritDoc}
*/
public function vote(TokenInterface $token, $subject = null, array $attributes = []) : int
{
if (
$this->applicationMode->getCurrentMode() !== 'admin' ||
$this->requestStack->getParentRequest()
) {
return VoterInterface::ACCESS_GRANTED;
}
$admin = $this->getAdmin($token);
if (! $admin) {
return VoterInterface::ACCESS_GRANTED;
}
$module = $this->adminState->getCurrentModule();
return
! $module || $admin->verifyAdminModuleAccess($module) ?
VoterInterface::ACCESS_GRANTED :
VoterInterface::ACCESS_DENIED;
}
/**
* Gets admin
*
* @param TokenInterface|null $token (optional)
*
* @return AdminInterface|null
*/
protected function getAdmin(TokenInterface $token = null) : ? AdminInterface
{
if (! $token) {
return null;
}
$admin = $token->getUser();
return
$admin && gettype($admin) !== 'string' ?
$admin :
null;
}
}