<?php
namespace App\Security\Voter;
use App\Entity\Library\Ouvrage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class OuvrageVoter extends Voter
{
public const EDIT = 'OUVRAGE_EDIT';
public const VIEW = 'OUVRAGE_VIEW';
public const DELETE = 'OUVRAGE_DELETE';
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::EDIT, self::VIEW, self::DELETE])
&& $subject instanceof Ouvrage;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return $subject->getCreatedBy()->getCompany()->getId() == $user->getCompany()->getId();
}
}