<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Sponsor;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SponsorPermissionVoter extends Voter
{
public const FULL = 'full';
public const FULL_OR_SIMPLE = 'full_or_simple';
public const SIMPLE = 'simple';
public const RESTRICTED = 'restricted';
private const LIST = [
self::FULL,
self::FULL_OR_SIMPLE,
self::SIMPLE,
self::RESTRICTED,
];
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, self::LIST, true) && $subject instanceof Sponsor;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Sponsor) {
return false;
}
if ($attribute === self::FULL_OR_SIMPLE) {
return $user->hasFullPermission() || $user->hasSimplePermission();
}
return $user->hasPermission($attribute);
}
}