src/Security/Voter/EtablissementVoter.php line 12
<?phpnamespace App\Security\Voter;use App\Entity\Etablissement;use App\Entity\Utilisateur;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;use Symfony\Component\Security\Core\User\UserInterface;class EtablissementVoter extends Voter{private $security;public const EDIT = 'ETABLISSEMENT_EDIT';public const VIEW = 'ETABLISSEMENT_VIEW';public function __construct(Security $security){$this->security = $security;}protected function supports(string $attribute, mixed $subject): bool{// replace with your own logic// https://symfony.com/doc/current/security/voters.htmlreturn in_array($attribute, [self::EDIT, self::VIEW])&& $subject instanceof \App\Entity\Etablissement;}protected function voteOnAttribute(string $attribute, mixed $etablissement, TokenInterface $token): bool{/** @var Utilisateur $user */$user = $token->getUser();// if the user is anonymous, do not grant accessif (!$user instanceof UserInterface) {return false;}// ROLE_SUPERADMIN > ROLE_ADMINif ($this->security->isGranted('ROLE_ADMIN')) {return true;}/** @var Etablissement $etablissement */// ... (check conditions and return true to grant permission) ...switch ($attribute) {case self::EDIT:case self::VIEW:// logic to determine if the user can EDIT or VIEW// return true or falseif ($this->security->isGranted('ROLE_GROUPE') &&$etablissement->getOrganisme()->getGroupe() &&$etablissement->getOrganisme()->getGroupe()->getRepresentantLegal() === $user) {return true;}if ($this->security->isGranted('ROLE_ORGANISME') &&$etablissement->getOrganisme()->getRepresentantLegal() === $user) {return true;}if ($this->security->isGranted('ROLE_ETABLISSEMENT') &&$etablissement->getResponsables()->contains($user)) {return true;}break;}return false;}}