src/Security/Voter/OptionVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Inscription;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class OptionVoter extends Voter
  10. {
  11. public const EDIT = 'POST_EDIT';
  12. public const VIEW = 'POST_VIEW';
  13. private EntityManagerInterface $em;
  14. public function __construct(EntityManagerInterface $em){
  15. $this->em = $em;
  16. }
  17. protected function supports(string $attribute, mixed $subject): bool
  18. {
  19. // replace with your own logic
  20. // https://symfony.com/doc/current/security/voters.html
  21. return in_array($attribute, [self::EDIT, self::VIEW])
  22. && $subject instanceof \App\Entity\EnterpriseOptionSejourSession;
  23. }
  24. protected function voteOnAttribute(string $attribute, mixed $option, TokenInterface $token): bool
  25. {
  26. $user = $token->getUser();
  27. // if the user is anonymous, do not grant access
  28. if(in_array(User::ROLE_ADMIN,$user->getRoles()))
  29. return true;
  30. if($option->getEnterprise() == $user->getContact()->getEnterprise())
  31. return true;
  32. return false;
  33. return false;
  34. }
  35. }