<?php
namespace App\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class ImpersonatorAdminVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return $attribute === 'IS_IMPERSONATOR_ADMIN';
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if (!$token instanceof SwitchUserToken) {
return false;
}
$impersonatorUser = $token->getOriginalToken()->getUser();
if ($impersonatorUser instanceof User && in_array('ROLE_ADMIN', $impersonatorUser->getRoles())) {
return true;
}
return false;
}
}