<?php
namespace App\Voter;
use App\Entity\Site;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SiteVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
if ($attribute !== 'site') {
return false;
}
return $subject instanceof Site;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
return $user->isAllowedOnSite($subject);
}
}