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