<?php
namespace App\Security;
use App\Model\Operation\Game;
use App\Model\User\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class GameVoter extends Voter
{
public const GET_GAME = 'get_game';
public const ARRAY_GAME_VOTER = [
self::GET_GAME,
];
protected function supports($attribute, $subject)
{
return in_array($attribute, self::ARRAY_GAME_VOTER);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$boolean = false;
$user = $token->getUser();
if (self::GET_GAME === $attribute) {
$boolean = $this->checkOneGame($user, $subject);
}
return $boolean;
}
protected function checkOneGame($user, $subject): bool
{
if (!$subject instanceof Game) {
return false;
}
if ($subject->isRequireUser() === true && (!$user instanceof User)) {
return false;
}
return true;
}
}