src/Security/Voter/QuestionnaireResponseVoter.php line 16

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\Entity\User;
  7. use App\Repository\UserProjectRepository;
  8. use DateTime;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. class QuestionnaireResponseVoter extends Voter
  14. {
  15.     private Security $security;
  16.     private UserProjectRepository|null $userProjectRepository;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.     }
  21.     /**
  22.      * @required
  23.      *
  24.      * @param UserProjectRepository $userProjectRepository
  25.      */
  26.     public function setUserProjectRepository(UserProjectRepository $userProjectRepository)
  27.     {
  28.         $this->userProjectRepository $userProjectRepository;
  29.     }
  30.     protected function supports(string $attribute$subject): bool
  31.     {
  32.         return in_array($attribute, ['EDIT''VIEW''EDIT_STATUS''VIEW_PRIVATE'])
  33.             && ($subject instanceof QuestionnaireResponse || $subject == QuestionnaireResponse::class);
  34.     }
  35.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  36.     {
  37.         /** @var QuestionnaireResponse $subject */
  38.         /** @var User $user */
  39.         $user $token->getUser();
  40.         // if the user is anonymous, do not grant access
  41.         if (!$user instanceof UserInterface) {
  42.             return false;
  43.         }
  44.         switch ($attribute) {
  45.             case 'EDIT_STATUS':
  46.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  47.                     return true;
  48.                 }
  49.                 break;
  50.             case 'EDIT':
  51.                 $project $subject->getProject();
  52.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  53.                     return true;
  54.                 }
  55.                 if ($subject->getStatus() == QuestionnaireResponse::S_STATUS_DRAFT) {
  56.                     if ($subject->getAuthor() === $user) {
  57.                         // si li, li doc et dc, vérifier les dates de l'appel à projet
  58.                         if (in_array($subject->getQuestionnaireType(), [QuestionnaireResponse::QR_TYPE_LI_IQuestionnaireResponse::QR_TYPE_LI_DOC])) {
  59.                             if ($project->getAppelAProjet()->getStatus() == AppelAProjet::AAP_STATUS_LI) {
  60.                                 return true;
  61.                             }
  62.                         } elseif (in_array($subject->getQuestionnaireType(), [QuestionnaireResponse::QR_TYPE_LI_RQuestionnaireResponse::QR_TYPE_DC])) {
  63.                             if ($project->getAppelAProjet()->getStatus() == AppelAProjet::AAP_STATUS_DC) {
  64.                                 return true;
  65.                             }
  66.                         } else {
  67.                             return true;
  68.                         }
  69.                     }
  70.                     // pour LIR et DC, tous les membres de la drci doivent pouvoir intervenir
  71.                     if ($this->security->isGranted('ROLE_DRCI')) {
  72.                         if (in_array($subject->getQuestionnaireType(), [QuestionnaireResponse::QR_TYPE_LI_RQuestionnaireResponse::QR_TYPE_DC])) {
  73.                             if ($project->getDrci() === $user->getDrci()) {
  74.                                 if ($project->getAppelAProjet()->getStatus() == AppelAProjet::AAP_STATUS_DC) {
  75.                                     return true;
  76.                                 }
  77.                             }
  78.                         }
  79.                     }
  80.                 }
  81.                 break;
  82.             case 'VIEW':
  83.                 $project $subject->getProject();
  84.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  85.                     return true;
  86.                 }
  87.                 // si c'est l'auteur, oui)
  88.                 if ($subject->getAuthor() === $user) {
  89.                     return true;
  90.                 }
  91.                 if ($this->security->isGranted('ROLE_PRE_EVAL')
  92.                     || $this->security->isGranted('ROLE_RAPPORT')
  93.                     || $this->security->isGranted('ROLE_COORDONNATEUR')
  94.                     || $this->security->isGranted('ROLE_METHODO')
  95.                 ) {
  96.                     if ($project->getAppelAProjet()->getOuvert() === true) {
  97.                         // attention, lors de la deuxième phase d'ouverture, il semblerait qu'ils ne puissent plus voir les théma ? à confirmer
  98.                         return true;
  99.                     }
  100.                 }
  101.                 switch ($subject->getQuestionnaireType()) {
  102.                     case QuestionnaireResponse::QR_TYPE_LI_I:
  103.                     case QuestionnaireResponse::QR_TYPE_LI_R:
  104.                         // si on peut voir le projet, on peut voir la LI
  105.                         return ($this->security->isGranted('VIEW'$project));
  106.                     case QuestionnaireResponse::QR_TYPE_LI_DOC:
  107.                         // si on peut voir le projet, on peut voir la LI doc
  108.                         return ($this->security->isGranted('VIEW'$project));
  109.                     case QuestionnaireResponse::QR_TYPE_PREEVAL:
  110.                         // le porteur et le drci ont le droit de voir passé un certain statut du projet
  111.                         if ($this->security->isGranted('ROLE_PORTEUR') || $this->security->isGranted('ROLE_DRCI')) {
  112.                             if ($this->security->isGranted('VIEW'$project)) {
  113.                                 if (!in_array($project->getStatus(), [
  114.                                     Project::PROJECT_STATUS_BROUILLON,
  115.                                     Project::PROJECT_STATUS_DEPOSE,
  116.                                     Project::PROJECT_STATUS_ELIGIBLE,
  117.                                     Project::PROJECT_STATUS_NON_ELIGIBLE,
  118.                                     Project::PROJECT_STATUS_PRE_EVALUE
  119.                                 ])) {
  120.                                     return true;
  121.                                 }
  122.                             }
  123.                         }
  124.                         // les coordonnateur après le dépot dc
  125.                         if ($this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  126.                             if ($project->getAppelAProjet()->getDateCloseDc() < new \DateTime()) {
  127.                                 return true;
  128.                             }
  129.                         }
  130.                         // les rapporteurs
  131.                         if ($this->security->isGranted('ROLE_RAPPORT')) {
  132.                             // todo : est-ce qu'on tient compte de la date ?
  133.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_RAPPORT]);
  134.                             if (count($up) > 0) {
  135.                                 return true;
  136.                             }
  137.                         }
  138.                         break;
  139.                     case QuestionnaireResponse::QR_TYPE_EVAL_COORDO:
  140.                         // les coordonnateur après le dépot dc
  141.                         if ($this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  142.                             if ($project->getAppelAProjet()->getDateCloseDc() < new \DateTime()) {
  143.                                 return true;
  144.                             }
  145.                         }
  146.                         // les rapporteurs
  147.                         if ($this->security->isGranted('ROLE_RAPPORT')) {
  148.                             // todo : est-ce qu'on tient compte de la date ?
  149.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_RAPPORT]);
  150.                             if (count($up) > 0) {
  151.                                 return true;
  152.                             }
  153.                         }
  154.                         break;
  155. //                    case QuestionnaireResponse::QR_TYPE_EVAL_THEMA:
  156. //                        // personne
  157. //                        return false;
  158.                     case QuestionnaireResponse::QR_TYPE_DC:
  159.                         // le drci associé
  160.                         if ($this->security->isGranted('ROLE_DRCI')) {
  161.                             if ($project->getDrci() === $user->getDrci()) {
  162.                                 return true;
  163.                             }
  164.                         }
  165.                         // les girci partenaires dès qu'ils sont associés
  166.                         if ($this->security->isGranted('ROLE_GIRCI_PARTENAIRE')) {
  167.                             return ($this->security->isGranted('VIEW'$project));
  168.                         }
  169.                         // les coordo après la phase de dépot
  170.                         if ($this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  171.                             if ($project->getAppelAProjet()->getDateCloseDc() < new DateTime()) {
  172.                                 return true;
  173.                             }
  174.                         }
  175.                         // les experts associés à partir de la date de début d'expertise
  176.                         if ($this->security->isGranted('ROLE_EXPERT')) {
  177.                             if ($project->getAppelAProjet()->getDateOpenExpert() && $project->getAppelAProjet()->getDateOpenExpert() < new DateTime()) {
  178.                                 $up $this->userProjectRepository->findOneBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT]);
  179.                                 if ($up) {
  180.                                     return true;
  181.                                 }
  182.                             }
  183.                         }
  184.                         // les rapporteurs associés
  185.                         if ($this->security->isGranted('ROLE_RAPPORT')) {
  186.                             // todo : est-ce qu'on tient compte de la date ?
  187.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_RAPPORT]);
  188.                             if (count($up) > 0) {
  189.                                 return true;
  190.                             }
  191.                         }
  192.                         // l'expert 3
  193.                         if ($this->security->isGranted('ROLE_EXPERT_3')) {
  194.                             // todo : est-ce qu'on tient compte de la date ?
  195.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT_3]);
  196.                             if (count($up) > 0) {
  197.                                 return true;
  198.                             }
  199.                         }
  200.                         break;
  201.                     case QuestionnaireResponse::QR_TYPE_EXPERT:
  202.                         // les girci partenaires associés
  203.                         if ($this->security->isGranted('ROLE_GIRCI_PARTENAIRE')) {
  204.                             $up $this->userProjectRepository->findBy(['project' => $project'girci' => $user->getGirci(), 'questionnaireResponse' => $subject]);
  205.                             if (count($up) > 0) {
  206.                                 return true;
  207.                             }
  208.                         }
  209.                         // les coordo après la phase de dépot
  210.                         if ($this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  211.                             if ($subject->getStatus() == QuestionnaireResponse::S_STATUS_SUBMIT) {
  212.                                 return true;
  213.                             }
  214.                         }
  215.                         // les rapporteurs associés
  216.                         if ($this->security->isGranted('ROLE_RAPPORT')) {
  217.                             // todo : est-ce qu'on tient compte de la date ?
  218.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_RAPPORT]);
  219.                             if (count($up) > 0) {
  220.                                 return true;
  221.                             }
  222.                         }
  223.                         // l'expert 3
  224.                         if ($this->security->isGranted('ROLE_EXPERT_3')) {
  225.                             // todo : est-ce qu'on tient compte de la date ?
  226.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT_3]);
  227.                             if (count($up) > 0) {
  228.                                 return true;
  229.                             }
  230.                         }
  231.                         if ($this->security->isGranted('ROLE_PORTEUR')) {
  232.                             if ($project->getAuthor() === $user && $project->getDrci() === $user->getDrci()) {
  233.                                 if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  234.                                     return true;
  235.                                 }
  236.                             }
  237.                         }
  238.                         if ($this->security->isGranted('ROLE_DRCI')) {
  239.                             if ($project->getDrci() === $user->getDrci()) {
  240.                                 if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  241.                                     return true;
  242.                                 }
  243.                             }
  244.                         }
  245.                         if ($this->security->isGranted('ROLE_EXPERT')) {
  246.                             // pour l'autre expert, si le projet à un statut sélectionné ou non sélectionné
  247.                             if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  248.                                 $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT]);
  249.                                 if (count($up) > 0) {
  250.                                     return true;
  251.                                 }
  252.                             }
  253.                         }
  254.                         break;
  255.                     case QuestionnaireResponse::QR_TYPE_RAPPORT:
  256.                         // l'expert 3
  257.                         if ($this->security->isGranted('ROLE_EXPERT_3')) {
  258.                             // todo : est-ce qu'on tient compte de la date ?
  259.                             $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT_3]);
  260.                             if (count($up) > 0) {
  261.                                 return true;
  262.                             }
  263.                         }
  264.                         if ($this->security->isGranted('ROLE_PORTEUR')) {
  265.                             if ($project->getAuthor() === $user && $project->getDrci() === $user->getDrci()) {
  266.                                 if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  267.                                     return true;
  268.                                 }
  269.                             }
  270.                         }
  271.                         if ($this->security->isGranted('ROLE_DRCI')) {
  272.                             if ($project->getDrci() === $user->getDrci()) {
  273.                                 if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  274.                                     return true;
  275.                                 }
  276.                             }
  277.                         }
  278.                         // les coordo après la phase de dépot
  279.                         if ($this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  280.                             if ($subject->getStatus() == QuestionnaireResponse::S_STATUS_SUBMIT) {
  281.                                 return true;
  282.                             }
  283.                         }
  284.                         if ($this->security->isGranted('ROLE_EXPERT')) {
  285.                             if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  286.                                 $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT]);
  287.                                 if (count($up) > 0) {
  288.                                     return true;
  289.                                 }
  290.                             }
  291.                         }
  292.                         break;
  293.                     case QuestionnaireResponse::QR_TYPE_EXPERT_3:
  294.                         if ($this->security->isGranted('ROLE_PORTEUR')) {
  295.                             if ($project->getAuthor() === $user && $project->getDrci() === $user->getDrci()) {
  296.                                 if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  297.                                     return true;
  298.                                 }
  299.                             }
  300.                         }
  301.                         if ($this->security->isGranted('ROLE_DRCI')) {
  302.                             if ($project->getDrci() === $user->getDrci()) {
  303.                                 if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  304.                                     return true;
  305.                                 }
  306.                             }
  307.                         }
  308.                         // les coordo après la phase de dépot
  309.                         if ($this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  310.                             if ($subject->getStatus() == QuestionnaireResponse::S_STATUS_SUBMIT) {
  311.                                 return true;
  312.                             }
  313.                         }
  314.                         if ($this->security->isGranted('ROLE_EXPERT')) {
  315.                             if (in_array($project->getStatus(), [Project::PROJECT_STATUS_SELECTIONNEProject::PROJECT_STATUS_NON_SELECTIONNE])) {
  316.                                 $up $this->userProjectRepository->findBy(['user' => $user'project' => $project'type' => QuestionnaireResponse::QR_TYPE_EXPERT]);
  317.                                 if (count($up) > 0) {
  318.                                     return true;
  319.                                 }
  320.                             }
  321.                         }
  322.                         break;
  323.                 }
  324.                 break;
  325.             case 'VIEW_PRIVATE':
  326.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI') || $this->security->isGranted('ROLE_COORDONNATEUR') || $this->security->isGranted('ROLE_METHODO')) {
  327.                     return true;
  328.                 }
  329.                 if ($this->security->isGranted('ROLE_PRE_EVAL')
  330.                     || $this->security->isGranted('ROLE_RAPPORT')
  331.                 ){
  332.                     if ($subject->getProject()->getAppelAProjet()->getOuvert() === true) {
  333.                         return true;
  334.                     }
  335.                     $up $this->userProjectRepository->findBy(['user' => $user'project' => $subject->getProject()]);
  336.                     if (count($up) > 0) {
  337.                         return true;
  338.                     }
  339.                 }
  340.                 // a le droit de voir si évaluateur du projet
  341.                 if ($this->security->isGranted('ROLE_EXPERT')
  342.                     || $this->security->isGranted('ROLE_EXPERT_3')
  343.                 ) {
  344.                     $up $this->userProjectRepository->findBy(['user' => $user'project' => $subject->getProject()]);
  345.                     if (count($up) > 0) {
  346.                         return true;
  347.                     }
  348.                 }
  349.                 break;
  350.         }
  351.         return false;
  352.     }
  353. }