<?php
namespace App\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class BoAccessVoter extends Voter
{
public const BO_ACCESS = 'BO_ACCESS';
private const ALLOWED_ROLES = [
'ROLE_ADMIN',
'ROLE_SUPER_ADMIN',
'ROLE_DIRECTEUR_ORGANISATION',
'ROLE_DIRECTEUR_DOMAINES',
'ROLE_DIRECTEUR_SOUS_DOMAINES',
'ROLE_DIRECTEUR_RUBRIQUE',
];
public function __construct()
{
}
protected function supports(string $attribute, mixed $subject): bool
{
return $attribute === self::BO_ACCESS;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
foreach ($user->getRoles() as $roleCode) {
if (in_array($roleCode, self::ALLOWED_ROLES, true)) {
return true;
}
}
return false;
}
}