<?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;
use Symfony\Component\Security\Core\User\UserInterface;
class InscriptionVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
&& $subject instanceof \App\Entity\Inscription;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if(in_array(User::ROLE_ADMIN,$user->getRoles()))
return true;
if ($user->getContact() === $subject->getParent())
return true;
if ($subject->getEnterpriseOptionSejourSession() !== null && $subject->getEnterpriseOptionSejourSession()->getEnterprise() !==null && $subject->getEnterpriseOptionSejourSession()->getEnterprise()->getContact() === $user->getContact() )
return true;
return false;
}
}