<?php
namespace App\Security;
use App\Entity\Client;
use App\Entity\Security\AppUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ClientVoter extends Voter
{
public const VIEW = 'view';
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, [self::VIEW])) {
return false;
}
return $subject instanceof Client;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if (!in_array($attribute, [self::VIEW])) {
return false;
}
$user = $token->getUser();
// the user must be logged in; if not, deny access
if (!$user instanceof AppUser) {
return false;
}
return $user->getClient() == $subject;
}
}