<?php
declare(strict_types=1);
namespace App\System\Authorization\Voter;
use App\Entities\User\Type\TypeEnum;
use App\Entities\User\UserAccess;
use App\Entities\User\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AuthorizationVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [TypeEnum::Admin->value, TypeEnum::User->value], true);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
TypeEnum::Admin->value => $user->isAdmin(),
TypeEnum::User->value => $user->isAdmin() || $user instanceof UserAccess,
};
}
}