src/Security/Voter/AppelAProjetVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AppelAProjet;
  4. use DateTime;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use function PHPUnit\Framework\returnArgument;
  10. class AppelAProjetVoter extends Voter
  11. {
  12.     private $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         return in_array($attribute, ['CREATE''EDIT''VIEW''DELETE''ADD_PROJECT''PREEVAL_SEND_EMAIL''RAPPORT_SEND_EMAIL''RECAP_PROJECT''GENERATE_EXPERTS'])
  20.             && ($subject instanceof AppelAProjet || $subject == AppelAProjet::class);
  21.     }
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         /** @var $subject AppelAProjet */
  25.         switch ($attribute) {
  26.             case 'CREATE':
  27.             case 'EDIT':
  28.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  29.                     return true;
  30.                 }
  31.                 break;
  32.             case 'VIEW':
  33.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  34.                     return true;
  35.                 }
  36.                 /** @var $subject AppelAProjet */
  37.                 if ($subject->isPublished()) {
  38.                     return true;
  39.                 }
  40.                 break;
  41.             case 'DELETE':
  42.                 // todo : peut-être rajouter d'autres critères
  43.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  44.                     return true;
  45.                 }
  46.                 break;
  47.             case 'ADD_PROJECT':
  48.                 // il faut que le projet soit publié
  49.                 if (!$subject->isPublished()) {
  50.                     return false;
  51.                 }
  52.                 // seuls les porteurs peuvent soumettre des projets en 2 phases
  53.                 if ($this->security->isGranted('ROLE_PORTEUR')) {
  54.                     // et qu'on soit dans la date de dépôt
  55.                     $now = new DateTime();
  56.                     if ($subject->getDateOpenLi() && $subject->getDateOpenLi() < $now && $subject->getDateCloseLi() > $now) {
  57.                         return true;
  58.                     }
  59.                 }
  60.                 // les projets en une phase sont déposés par les drci
  61.                 if ($this->security->isGranted('ROLE_DRCI')) {
  62.                     // et qu'on soit dans la date de dépôt
  63.                     $now = new DateTime();
  64.                     if (!$subject->getDateOpenLi() && $subject->getDateOpenDc() && $subject->getDateOpenDc() < $now && $subject->getDateCloseDc() > $now) {
  65.                         return true;
  66.                     }
  67.                 }
  68.                 break;
  69.             case 'PREEVAL_SEND_EMAIL':
  70.                 if (!$this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  71.                     return false;
  72.                 }
  73.                 // en fonction de l'état de l'appel à projet
  74.                 $status $subject->getStatus();
  75.                 if ($status == AppelAProjet::AAP_STATUS_POST_LI || $status == AppelAProjet::AAP_STATUS_PRE_EVAL) {
  76.                     return true;
  77.                 }
  78.                 break;
  79.             case 'RAPPORT_SEND_EMAIL':
  80.                 if (!$this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  81.                     return false;
  82.                 }
  83.                 // en fonction de l'état de l'appel à projet
  84.                 $status $subject->getStatus();
  85.                 if ($status == AppelAProjet::AAP_STATUS_POST_DC || $status == AppelAProjet::AAP_STATUS_RAPPORT || $status == AppelAProjet::AAP_STATUS_POST_EXPERT) {
  86.                     return true;
  87.                 }
  88.                 break;
  89.             case 'RECAP_PROJECT':
  90.                 // pour le moment, juste interdire les anonymes
  91.                 $user $token->getUser();
  92.                 // if the user is anonymous, do not grant access
  93.                 if (!$user instanceof UserInterface) {
  94.                     return false;
  95.                 } else {
  96.                     return true;
  97.                 }
  98.             case 'GENERATE_EXPERTS':
  99.                 if (!$this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  100.                     return false;
  101.                 }
  102.                 // en fonction de l'état de l'appel à projet
  103.                 $status $subject->getStatus();
  104.                 if ($status == AppelAProjet::AAP_STATUS_POST_DC || $status == AppelAProjet::AAP_STATUS_EXPERT) {
  105.                     return true;
  106.                 }
  107.                 break;
  108.         }
  109.         return false;
  110.     }
  111. }