<?php
namespace App\Security\Voter;
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 DocumentVoter extends Voter
{
public const LIST = 'list';
public const ADD = 'add';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return 'docPlus' === $subject && \in_array($attribute, [
self::LIST,
self::ADD,
], true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// Utilisateur doit être authentifié
if (!$token->getUser() instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::LIST:
case self::ADD:
return $this->security->isGranted('ROLE_RESPONSABLE_ETABLISSEMENT') ||
$this->security->isGranted('ROLE_COMMUNICATION') ||
$this->security->isGranted('ROLE_GESTIONNAIRE_ESPACE_ETABLISSEMENT');
}
return false;
}
}