<?php
declare(strict_types=1);
namespace App\Security;
use App\Entity\System\Employee;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class IsEmployeeVoter extends Voter
{
public const IS_EMPLOYEE = 'IS_EMPLOYEE';
protected function supports($attribute, $subject): bool
{
return $attribute === self::IS_EMPLOYEE;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if ($token instanceof SwitchUserToken) {
$user = $token->getOriginalToken()->getUser();
}
if (!$user instanceof Employee) {
return false;
}
return true;
}
}