<?php
namespace App\Security;
use App\Entity\Processus;
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 ProcessusVoter extends Voter
{
const PROCESSUS_CRU = 'PROCESSUS_CRU';
const PROCESSUS_D = 'PROCESSUS_D';
const PROCESSUS_CONFIGURATION_CRUD = 'PROCESSUS_CONFIGURATION_CRUD';
const PROCESSUS_INDICATOR_INSERT = 'PROCESSUS_INDICATOR_INSERT';
const PROCESSUS_INDICATOR_REPORTING = 'PROCESSUS_INDICATOR_REPORTING';
private $autorisationRepository;
public function __construct(AutorisationRepository $autorisationRepository)
{
$this->autorisationRepository = $autorisationRepository;
}
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, [self::PROCESSUS_CRU, self::PROCESSUS_D,self::PROCESSUS_CONFIGURATION_CRUD,self::PROCESSUS_INDICATOR_INSERT, self::PROCESSUS_INDICATOR_REPORTING])) {
return false;
}
// if (!$subject instanceof Processus) {
// 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!');
}
}