<?php
namespace App\Security\Voter;
use App\Entity\InstantGagnant;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class InstantGagnantVoter extends Voter
{
public const EDIT = 'IG_EDIT';
public const VIEW = 'IG_VIEW';
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::EDIT, self::VIEW])
&& $subject instanceof \App\Entity\InstantGagnant;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if (!$subject instanceof InstantGagnant) {
return false;
}
switch ($attribute) {
case self::VIEW:
case self::EDIT:
return in_array('ROLE_ADMIN_SOGEC', $user->getRoles()) || (in_array('ROLE_USER', $user->getRoles()) && $user === $subject->getOperation()->getUser());
}
return false;
}
}