src/Security/Voter/FrequentationVoter.php line 12
<?phpnamespace App\Security\Voter;use App\Entity\Frequentation;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 FrequentationVoter extends Voter{private $security;public const EDIT = 'FREQUENTATION_EDIT';public const VIEW = 'FREQUENTATION_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\Frequentation;}protected function voteOnAttribute(string $attribute, mixed $frequentation, 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 Frequentation $frequentation */// ... (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') &&$frequentation->getBassin()->getEtablissement()->getOrganisme()->getGroupe() &&$user->getGroupesRepresentantLegal()->contains($frequentation->getBassin()->getEtablissement()->getOrganisme()->getGroupe())) {return true;}if ($this->security->isGranted('ROLE_ORGANISME') &&$user->getOrganismesRepresentantLegal()->contains($frequentation->getBassin()->getEtablissement()->getOrganisme())) {return true;}if ($this->security->isGranted('ROLE_ETABLISSEMENT') &&$user->getEtablissementsResponsable()->contains($frequentation->getBassin()->getEtablissement())) {return true;}if ($this->security->isGranted('ROLE_OPERATEUR')) {foreach ($user->getTaches() as $tachesBassin) {if ($tachesBassin->getBassin() === $frequentation->getBassin() &&$tachesBassin->isFrequentation()) {return true;}}}break;}return false;}}