src/Security/Voter/MediaVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use App\Entity\User;
  7. class MediaVoter 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\Media;
  15. }
  16. protected function voteOnAttribute(string $attribute, $media, TokenInterface $token): bool
  17. {
  18. $user = $token->getUser();
  19. // if the user is anonymous, do not grant access
  20. if($user->getIsActive()===0)
  21. return false;
  22. if (!$user instanceof UserInterface) {
  23. return false;
  24. }
  25. if($media->getMediaCategorie()->getType() == "candidature" && $media->getMediaCategorie()->getName() == "photo" && count($media->getContact()->getSejourSessionEquipiers())>0 )
  26. return true;
  27. if(in_array(User::ROLE_ADMIN,$user->getRoles()))
  28. return true;
  29. if(in_array('ROLE_DIRECTEUR',$user->getRoles())&&$media->getMediaCategorie()->getType()=="candidature")
  30. return true;
  31. if(in_array('ROLE_DIRECTEUR',$user->getRoles())&&$media->getMediaCategorie()->getType()=="App\Entity\Sejour")
  32. return true;
  33. if($media->getContact() !== null && $media->getContact()==$user->getContact())
  34. return true;
  35. if($media->getContact() !== null && $media->getContact()->getParent() == $user->getContact())
  36. return true;
  37. if($user->getContact() !== null && $media->getMediaCategorie()->getName() == "projet pedagogique")
  38. return true;
  39. if($media->getSejourSession()!== null && $media->getSejourSession()->getDirector()==$user->getContact())
  40. return true;
  41. if($media->getInscription() !== null && $media->getInscription()->getParent() == $user->getContact())
  42. return true;
  43. //todo voir si sur sessions
  44. if(in_array('ROLE_DIRECTEUR',$user->getRoles())){
  45. return $media->getMediaCategorie()->getIsEspaceDirecteur();
  46. if($media->getContact() !== null)
  47. {
  48. //mettre plus de sécurite
  49. return $media->getMediaCategorie()->getIsEspaceDirecteur();
  50. }
  51. }
  52. return false;
  53. }
  54. }