src/Security/Voter/InscriptionVoter.php line 10

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