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