src/Security/Voter/ProjetVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Acteur;
  4. use App\Entity\User;
  5. use App\Entity\Projet;
  6. use App\Repository\ActeurRepository;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. class ProjetVoter extends Voter
  12. {
  13. public const EDIT = 'PROJET_EDIT';
  14. public const VIEW = 'PROJET_VIEW';
  15. public const DELETE = 'PROJET_DELETE';
  16. private $security;
  17. private $acteurRepository;
  18. public function __construct(Security $security, ActeurRepository $acteurRepository)
  19. {
  20. $this->security = $security;
  21. $this->acteurRepository = $acteurRepository;
  22. }
  23. protected function supports(string $attribute, $projet): bool
  24. {
  25. // replace with your own logic
  26. // https://symfony.com/doc/current/security/voters.html
  27. return in_array($attribute, [self::EDIT, self::VIEW, self::DELETE])
  28. && $projet instanceof \App\Entity\Projet;
  29. }
  30. protected function voteOnAttribute(string $attribute, $projet, TokenInterface $token): bool
  31. {
  32. $user = $token->getUser();
  33. // if the user is anonymous, do not grant access
  34. if (!$user instanceof UserInterface) {
  35. return false;
  36. }
  37. //On vérifie si l'utilisateur est admin pour les projets
  38. if($this->security->isGranted('ROLE_PROJET_ADMIN'))
  39. {
  40. return true;
  41. }
  42. // On vérifie si le user a un utilisateur enregistré
  43. $this->existUtilisateur($user);
  44. // ... (check conditions and return true to grant permission) ...
  45. switch ($attribute) {
  46. case self::EDIT:
  47. // on vérifie si on peut éditer
  48. return $this->canEdit($projet, $user);
  49. break;
  50. case self::VIEW:
  51. // on vérifie si on peut visualiser
  52. return $this->canView($projet, $user);
  53. break;
  54. case self::DELETE:
  55. // on vérifie si on peut supprimer
  56. return $this->canDelete($projet, $user);
  57. break;
  58. }
  59. return false;
  60. }
  61. public function existUtilisateur(User $user)
  62. {
  63. if(null === $user->getUtilisateur()){
  64. return false;
  65. }
  66. }
  67. public function canEdit(Projet $projet, User $user)
  68. {
  69. if($this->security->isGranted('ROLE_PROJET_EDIT'))
  70. {
  71. // $acteurs_user = $user->getUtilisateur()->getActeurs();
  72. // $acteurs_projet = $projet->getActeurs();
  73. // foreach($acteurs_user as $acteur){
  74. // if(in_array($acteur,$acteurs_projet->toArray()))
  75. // {
  76. // return true;
  77. // }
  78. // }
  79. return is_a($this->acteurRepository->findOneBy(['projet' => $projet, 'utilisateur' => $user->getUtilisateur()]),Acteur::class);
  80. }
  81. return false;
  82. }
  83. public function canView(Projet $projet, User $user)
  84. {
  85. if($this->security->isGranted('ROLE_PROJET_USER'))
  86. {
  87. // $acteurs_user = $user->getUtilisateur()->getActeurs();
  88. // $acteurs_projet = $projet->getActeurs();
  89. // foreach($acteurs_user as $acteur){
  90. // if(in_array($acteur,$acteurs_projet->toArray()))
  91. // {
  92. // return true;
  93. // }
  94. // }
  95. return is_a($this->acteurRepository->findOneBy(['projet' => $projet, 'utilisateur' => $user->getUtilisateur()]),Acteur::class);
  96. }
  97. return false;
  98. }
  99. public function canDelete(Projet $projet, User $user)
  100. {
  101. }
  102. }