src/Security/Voter/ServiceVoter.php line 10
<?phpnamespace App\Security\Voter;use App\Entity\Admin\Parameter\Service;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;use Symfony\Component\Security\Core\User\UserInterface;class ServiceVoter extends Voter{public const EDIT = 'POST_EDIT';public const VIEW = 'POST_VIEW';public const IS_RESPONSABLE = 'IS_RESPONSABLE';protected function supports(string $attribute, mixed $subject): bool{// IS_RESPONSABLE ne nécessite pas de $subjectif ($attribute === self::IS_RESPONSABLE) {return true;}return in_array($attribute, [self::EDIT, self::VIEW])&& $subject instanceof Service;}protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool{$user = $token->getUser();if (!$user instanceof UserInterface) {return false;}if ($attribute === self::IS_RESPONSABLE) {return $this->isResponsable($user);}if (!$subject instanceof Service) {return false;}return match ($attribute) {self::EDIT => $this->canEdit($subject, $user),self::VIEW => $this->canView($subject, $user),default => false,};}private function canEdit(Service $service, UserInterface $user): bool{return $user === $service->getUser();}private function canView(Service $service, UserInterface $user): bool{return $this->canEdit($service, $user) || $service->getUsers()->contains($user);}private function isResponsable(UserInterface $user): bool{return $user->getService() !== null && $user === $user->getService()->getUser();}}