<?php
namespace App\Security\Voter;
use App\Entity\User;
use App\Manager\VoterManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class AbcenseVoter extends Voter
{
const ACCESS = 'ABSENCE_ACCESS';
private $security;
private $voterManager;
public function __construct(Security $security,VoterManager $voterManager)
{
$this->security = $security;
$this->voterManager = $voterManager;
}
/**
* @param string $attribute
* @param $subject
* @return bool
*/
protected function supports(string $attribute, $subject): bool
{
if(!in_array($attribute, [self::ACCESS])){
return false;
}
/* if(!$subject instanceof User){
return false;
}*/
return true;
}
/**
* @param $attribute
* @param $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
// On récupère l'utilisateur à partir du token
$currentUser = $token->getUser();
if(!$currentUser instanceof UserInterface) return false;
// On vérifie si l'utilisateur est admin
if($this->security->isGranted('ROLE_SUPER_ADMIN')) return true;
return $this->voterManager->hasRight($currentUser,$attribute);
}
}