src/Security/Voter/ProjectVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AppelAProjet;
  4. use App\Entity\Project;
  5. use App\Entity\QuestionnaireResponse;
  6. use App\Repository\UserProjectRepository;
  7. use DateTime;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class ProjectVoter extends Voter
  13. {
  14.     private Security $security;
  15.     private UserProjectRepository|null $userProjectRepository;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     /**
  21.      * @required
  22.      *
  23.      * @param UserProjectRepository $userProjectRepository
  24.      */
  25.     public function setUserProjectRepository(UserProjectRepository $userProjectRepository)
  26.     {
  27.         $this->userProjectRepository $userProjectRepository;
  28.     }
  29.     protected function supports(string $attribute$subject): bool
  30.     {
  31.         return in_array($attribute, ['EDIT''VIEW''ADD_FILES''SUBMIT''PRE_EVAL''COORDO''THEMA''CLONE_LI''ADD_DC''DC_SUBMIT''EXPERT''RAPPORT''EXPERT_3'])
  32.             && $subject instanceof Project;
  33.     }
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         /** @var Project $subject */
  37.         $user $token->getUser();
  38.         // if the user is anonymous, do not grant access
  39.         if (!$user instanceof UserInterface) {
  40.             return false;
  41.         }
  42.         // ... (check conditions and return true to grant permission) ...
  43.         switch ($attribute) {
  44.             case 'EDIT':
  45.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  46.                     return true;
  47.                 }
  48.                 break;
  49.             case 'VIEW':
  50.                 if ($this->security->isGranted('ROLE_PORTEUR')) {
  51.                     // si il est l'auteur et si il appartient toujours à la même drci
  52.                     if ($subject->getAuthor() === $user) {
  53.                         if ($subject->getDrci() === $user->getDrci()) {
  54.                             return true;
  55.                         }
  56.                     }
  57.                 }
  58.                 if ($this->security->isGranted('ROLE_DRCI')) {
  59.                     if ($subject->getDrci() === $user->getDrci()) {
  60.                         return true;
  61.                     }
  62.                 }
  63.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI') || $this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  64.                     return true;
  65.                 }
  66.                 if ($this->security->isGranted('ROLE_PRE_EVAL')
  67.                     || $this->security->isGranted('ROLE_RAPPORT'))
  68.                 {
  69.                     if ($subject->getAppelAProjet()->getOuvert() === true) {
  70.                         return true;
  71.                     } else {
  72.                         $up $this->userProjectRepository->findBy(['user' => $this->security->getUser(), 'project' => $subject]);
  73.                         if (count($up) > 0) {
  74.                             return true;
  75.                         }
  76.                     }
  77.                 }
  78.                 if ($this->security->isGranted('ROLE_THEMA')
  79.                     || $this->security->isGranted('ROLE_EXPERT')
  80.                     || $this->security->isGranted('ROLE_EXPERT_3'))
  81.                 {
  82.                     $up $this->userProjectRepository->findBy(['user' => $this->security->getUser(), 'project' => $subject]);
  83.                     if (count($up) > 0) {
  84.                         return true;
  85.                     }
  86.                 }
  87.                 if ($this->security->isGranted('ROLE_GIRCI_PARTENAIRE')) {
  88.                     $up $this->userProjectRepository->findBy(['project' => $subject'girci' => $this->security->getUser()->getGirci()]);
  89.                     if (count($up) > 0) {
  90.                         return true;
  91.                     }
  92.                 }
  93.                 return false;
  94.             case 'ADD_FILES':
  95.                 if ($this->security->isGranted('ROLE_PORTEUR')) {
  96.                     // si il est l'auteur et si il appartient toujours à la même drci
  97.                     if ($subject->getAuthor() !== $user) {
  98.                         return false;
  99.                     }
  100.                     if ($subject->getDrci() !== $user->getDrci()) {
  101.                         return false;
  102.                     }
  103.                     $aap $subject->getAppelAProjet();
  104.                     if ($aap->getStatus() !== AppelAProjet::AAP_STATUS_LI) {
  105.                         return false;
  106.                     }
  107.                     if ($subject->getStatus() !== Project::PROJECT_STATUS_BROUILLON) {
  108.                         return false;
  109.                     }
  110.                     return true;
  111.                 }
  112.                 break;
  113.             case 'SUBMIT':
  114.                 if ($this->security->isGranted('ROLE_PORTEUR')) {
  115.                     // si il est l'auteur et si il appartient toujours à la même drci
  116.                     if ($subject->getAuthor() !== $user) {
  117.                         return false;
  118.                     }
  119.                     if ($subject->getDrci() !== $user->getDrci()) {
  120.                         return false;
  121.                     }
  122.                     $aap $subject->getAppelAProjet();
  123.                     if ($aap->getStatus() !== AppelAProjet::AAP_STATUS_LI) {
  124.                         return false;
  125.                     }
  126.                     if ($subject->getStatus() !== Project::PROJECT_STATUS_BROUILLON) {
  127.                         return false;
  128.                     }
  129.                     return true;
  130.                 }
  131.                 if ($this->security->isGranted('ROLE_DRCI')) {
  132.                     if ($subject->getAuthor() !== $user && $subject->getDrci() !== $user->getDrci()) {
  133.                         return false;
  134.                     }
  135.                     $aap $subject->getAppelAProjet();
  136.                     if ($aap->getQuestionnaireLi()) {
  137.                         return false;
  138.                     }
  139.                     if ($aap->getStatus() !== AppelAProjet::AAP_STATUS_DC) {
  140.                         return false;
  141.                     }
  142.                     if ($subject->getStatus() !== Project::PROJECT_STATUS_BROUILLON) {
  143.                         return false;
  144.                     }
  145.                     return true;
  146.                 }
  147.                 break;
  148.             case 'PRE_EVAL':
  149.                 if (!$this->security->isGranted('ROLE_PRE_EVAL')) {
  150.                     return false;
  151.                 }
  152.                 if ($subject->getStatus() != Project::PROJECT_STATUS_ELIGIBLE) {
  153.                     return false;
  154.                 }
  155.                 // est-ce que l'utilisateur est associé au projet en tant que pré-évaluateur ?
  156.                 $up $this->userProjectRepository->findOneBy(['user' => $this->security->getUser(), 'project' => $subject'type' => QuestionnaireResponse::QR_TYPE_PREEVAL]);
  157.                 if (!$up) {
  158.                     return false;
  159.                 }
  160.                 $aap $subject->getAppelAProjet();
  161.                 if ($aap->getDateOpenPreeval() && $aap->getDateOpenPreeval() < new DateTime()) {
  162.                     // la date de fin n'est pas bloquante
  163.                     return true;
  164.                 }
  165.                 break;
  166.             case 'THEMA':
  167.                 if (!$this->security->isGranted('ROLE_THEMA')) {
  168.                     return false;
  169.                 }
  170.                 if ($subject->getStatus() != Project::PROJECT_STATUS_ELIGIBLE) {
  171.                     return false;
  172.                 }
  173.                 // est-ce que l'utilisateur est associé au projet en tant que évaluateur thématique ?
  174.                 $up $this->userProjectRepository->findOneBy(['user' => $this->security->getUser(), 'project' => $subject'type' => QuestionnaireResponse::QR_TYPE_EVAL_THEMA]);
  175.                 if (!$up) {
  176.                     return false;
  177.                 }
  178.                 $aap $subject->getAppelAProjet();
  179.                 if ($aap->getDateOpenPreeval() && $aap->getDateOpenPreeval() < new DateTime()) {
  180.                     // la date de fin n'est pas bloquante
  181.                     return true;
  182.                 }
  183.                 break;
  184.             case 'COORDO':
  185.                 if (!$this->security->isGranted('ROLE_COORDONNATEUR')) {
  186.                     return false;
  187.                 }
  188.                 if ($subject->getStatus() != Project::PROJECT_STATUS_ELIGIBLE) {
  189.                     return false;
  190.                 }
  191.                 $aap $subject->getAppelAProjet();
  192.                 if ($aap->getDateOpenPreeval() && $aap->getDateOpenPreeval() < new DateTime()) {
  193.                     // la date de fin n'est pas bloquante
  194.                     // et il faut un questionnaire coordo (il n'y en a pas forcément)
  195.                     if ($aap->getQuestionnaireEvalCoordo() !== null) {
  196.                         return true;
  197.                     }
  198.                 }
  199.                 break;
  200.             case 'CLONE_LI':
  201.                 if (!$this->security->isGranted('ROLE_DRCI')) {
  202.                     return false;
  203.                 }
  204.                 if ($subject->getDrci() !== $user->getDrci()) {
  205.                     return false;
  206.                 }
  207.                 if ($subject->getStatus() != Project::PROJECT_STATUS_PRE_SELECTIONNE) {
  208.                     return false;
  209.                 }
  210.                 $aap $subject->getAppelAProjet();
  211.                 if (!$aap->getQuestionnaireLi()) {
  212.                     return false;
  213.                 }
  214.                 $now = new DateTime();
  215.                 if ($aap->getDateOpenDc() < $now && $now $aap->getDateCloseDc()) {
  216.                     return true;
  217.                 }
  218.                 break;
  219.             case 'ADD_DC':
  220.             case 'DC_SUBMIT':
  221.                 if (!$this->security->isGranted('ROLE_DRCI')) {
  222.                     return false;
  223.                 }
  224.                 if ($subject->getDrci() !== $user->getDrci()) {
  225.                     return false;
  226.                 }
  227.                 // comme ça, pas de problème pour les aap en 1 étape
  228.                 if ($subject->getStatus() != Project::PROJECT_STATUS_PRE_SELECTIONNE) {
  229.                     return false;
  230.                 }
  231.                 $now = new DateTime();
  232.                 $aap $subject->getAppelAProjet();
  233.                 if ($aap->getDateOpenDc() < $now && $now $aap->getDateCloseDc()) {
  234.                     return true;
  235.                 }
  236.                 break;
  237.             case 'EXPERT':
  238.                 if (!$this->security->isGranted('ROLE_EXPERT')) {
  239.                     return false;
  240.                 }
  241.                 if ($subject->getStatus() != Project::PROJECT_STATUS_COMPLET) {
  242.                     return false;
  243.                 }
  244.                 // est-ce que l'utilisateur est associé au projet en tant que expert ?
  245.                 $up $this->userProjectRepository->findOneBy(['user' => $this->security->getUser(), 'project' => $subject'type' => QuestionnaireResponse::QR_TYPE_EXPERT]);
  246.                 if (!$up) {
  247.                     return false;
  248.                 }
  249.                 $aap $subject->getAppelAProjet();
  250.                 if ($aap->getDateOpenExpert() && $aap->getDateOpenExpert() < new DateTime()) {
  251.                     // la date de fin n'est pas bloquante
  252.                     return true;
  253.                 }
  254.                 break;
  255.             case 'RAPPORT':
  256.                 if (!$this->security->isGranted('ROLE_RAPPORT')) {
  257.                     return false;
  258.                 }
  259.                 $aap $subject->getAppelAProjet();
  260.                 if ($aap->getQuestionnaireExpert()) {
  261.                     if ($subject->getStatus() != Project::PROJECT_STATUS_EXPERTISE) {
  262.                         return false;
  263.                     }
  264.                 } else {
  265.                     if ($subject->getStatus() != Project::PROJECT_STATUS_COMPLET) {
  266.                         return false;
  267.                     }
  268.                 }
  269.                 // est-ce que l'utilisateur est associé au projet en tant que rapporteur ?
  270.                 $up $this->userProjectRepository->findOneBy(['user' => $this->security->getUser(), 'project' => $subject'type' => QuestionnaireResponse::QR_TYPE_RAPPORT]);
  271.                 if (!$up) {
  272.                     return false;
  273.                 }
  274.                 if ($aap->getDateOpenRapport() && $aap->getDateOpenRapport() < new DateTime()) {
  275.                     // la date de fin n'est pas bloquante
  276.                     return true;
  277.                 }
  278.                 break;
  279.             case 'EXPERT_3':
  280.                 if (!$this->security->isGranted('ROLE_EXPERT_3')) {
  281.                     return false;
  282.                 }
  283.                 if ($subject->getStatus() != Project::PROJECT_STATUS_RAPPORTE) {
  284.                     return false;
  285.                 }
  286.                 // est-ce que l'utilisateur est associé au projet en tant que expert 3 ?
  287.                 $up $this->userProjectRepository->findOneBy(['user' => $this->security->getUser(), 'project' => $subject'type' => QuestionnaireResponse::QR_TYPE_EXPERT_3]);
  288.                 if (!$up) {
  289.                     return false;
  290.                 }
  291.                 $aap $subject->getAppelAProjet();
  292.                 if ($aap->getDateOpenExpert3() && $aap->getDateOpenExpert3() < new DateTime()) {
  293.                     // la date de fin n'est pas bloquante
  294.                     return true;
  295.                 }
  296.                 break;
  297.         }
  298.         return false;
  299.     }
  300. }