<?php
namespace App\Security\Voter\Api;
use App\Entity\Taxonomy;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class TaxonomyVoter extends Voter
{
public const VIEW = 'edit';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW])
&& $subject instanceof Taxonomy;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::VIEW => $subject->getIsPublic(),
default => false,
};
}
}