src/Security/Voter/SejourSessionVoter.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 SejourSessionVoter 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\SejourSession;
  23. }
  24. protected function voteOnAttribute(string $attribute, mixed $sejourSession, TokenInterface $token): bool
  25. {
  26. $user = $token->getUser();
  27. // if the user is anonymous, do not grant access
  28. if($user->getIsActive()===0)
  29. return false;
  30. if (!$user instanceof UserInterface) {
  31. return false;
  32. }
  33. $contact = $user->getContact();
  34. //TODO: enlever les cancelled
  35. $inscription = $this->em->getRepository(Inscription::class)->findOneBy([
  36. 'sejourSession' => $sejourSession,
  37. 'parent' => $contact
  38. ]);
  39. if($inscription !== null ){
  40. return true;
  41. }
  42. if(in_array(User::ROLE_ADMIN,$user->getRoles()))
  43. return true;
  44. return false;
  45. return false;
  46. }
  47. }