<?php
namespace App\Security;
use App\Entity\Reunion;
use App\Entity\User;
use App\Repository\Configuration\AutorisationRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ReunionVoter extends Voter
{
const REUNION_CRUD = 'REUNION_CRUD';
private $autorisationRepository;
public function __construct(AutorisationRepository $autorisationRepository)
{
$this->autorisationRepository = $autorisationRepository;
}
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, [self::REUNION_CRUD])) {
return false;
}
// if (!$subject instanceof Reunion) {
// return false;
// }
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
$autorisation = $this->autorisationRepository->findOneBy(['code'=>$attribute]);
if (!$autorisation) {
return false;
}
$roles = array_intersect($autorisation->getRoles(),$user->getRoles());
if(count($roles)) {
return true;
}
return false;
throw new \LogicException('This code should not be reached!');
}
}