<?php
namespace App\Security\Voter;
use App\Entity\Inscription;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class OptionVoter extends Voter
{
public const EDIT = 'POST_EDIT';
public const VIEW = 'POST_VIEW';
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em){
$this->em = $em;
}
protected function supports(string $attribute, mixed $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::EDIT, self::VIEW])
&& $subject instanceof \App\Entity\EnterpriseOptionSejourSession;
}
protected function voteOnAttribute(string $attribute, mixed $option, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if(in_array(User::ROLE_ADMIN,$user->getRoles()))
return true;
if($option->getEnterprise() == $user->getContact()->getEnterprise())
return true;
return false;
return false;
}
}