src/Security/AbsenceVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Collaborator;
  4. use App\Entity\RH\Absence;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class AbsenceVoter extends Voter
  11. {
  12.     const VIEW          'view';
  13.     const EDIT          'edit';
  14.     const CHANGE_STATUS_PENDING     'changeStatus'.Absence::PENDING;
  15.     const CHANGE_STATUS_VALID       'changeStatus'.Absence::VALID;
  16.     const CHANGE_STATUS_REFUSED     'changeStatus'.Absence::REFUSED;
  17.     const CHANGE_STATUS_CANCELLED   'changeStatus'.Absence::CANCELLED;
  18.     const EDIT_MANAGER_COMMENT      'canEditManagerComment';
  19.     private $security;
  20.     private $translator;
  21.     public function __construct(Security $security,TranslatorInterface $translator)
  22.     {
  23.         $this->security $security;
  24.         $this->translator=$translator;
  25.     }
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         // if the attribute isn't one we support, return false
  29.         if (!in_array($attribute, [self::VIEWself::EDITself::EDIT_MANAGER_COMMENTself::CHANGE_STATUS_PENDING,self::CHANGE_STATUS_REFUSED,self::CHANGE_STATUS_VALID,self::CHANGE_STATUS_CANCELLED])) {
  30.             return false;
  31.         }
  32.         // only vote on `Absence` objects
  33.         if (!$subject instanceof Absence) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  39.     {
  40.         $collaborator $token->getUser()->getCollaborator();
  41.         if (!$collaborator instanceof Collaborator) {
  42.             return false;
  43.         }
  44.         // you know $subject is a Absence object, thanks to `supports()`
  45.         /** @var Absence $absence */
  46.         $absence $subject;
  47.         switch ($attribute) {
  48.             case self::VIEW:
  49.                 return $this->canView($absence$collaborator);
  50.             case self::CHANGE_STATUS_PENDING:
  51.             case self::CHANGE_STATUS_REFUSED:
  52.             case self::CHANGE_STATUS_VALID:
  53.             case self::CHANGE_STATUS_CANCELLED:
  54.                 return $this->canChangeStatus($absence$collaborator,$attribute);
  55.             case self::EDIT:
  56.                 return $this->canEdit($absence$collaborator);
  57.             case self::EDIT_MANAGER_COMMENT:
  58.                 return $this->canEditManagerComment($absence,$collaborator);
  59.         }
  60.         throw new \LogicException('This code should not be reached!');
  61.     }
  62.     private function canView(Absence $absenceCollaborator $collaborator): bool
  63.     {
  64.         if(
  65.             !$this->isGranted('ROLE_ADMIN')
  66.             and !$this->isGranted('ROLE_ASSISTANT_RH')
  67.             and $absence->getCollaborator()!=$collaborator
  68.             and !in_array($absence->getCollaborator(),$collaborator->getAllListNMoins()->toArray())
  69.         ){
  70.             return false;
  71.         }
  72.         return true;
  73.     }
  74.     private function canChangeStatus(Absence $absenceCollaborator $collaborator,$attribute): bool
  75.     {
  76.         if (!$this->canView($absence,$collaborator))return false;
  77.         /*
  78.          si statut existant => PENDING
  79.             =>VALID/REFUSED =>MANAGER ASSISTANT RH OU ADMIN
  80.             =>CANCEL => TOUS
  81.         */
  82.         /*
  83.          si statut existant => VALID
  84.             =>CANCEL => ASSISTANT RH OU ADMIN
  85.         */
  86.         /*
  87.          si statut existant => REFUSED
  88.             =>RIEN
  89.         */
  90.         /*
  91.          si statut existant => CANCELLED
  92.             =>RIEN
  93.         */
  94.         $new_status=str_replace('changeStatus','',$attribute);
  95.         if ($absence->getStatus()==$new_status){
  96.             return false;
  97.         }
  98.         if($absence->getStatus()==Absence::PENDING and in_array($new_status,[Absence::VALID,Absence::REFUSED]) and (!$this->isGranted('ROLE_ASSISTANT_RH') and !in_array$absence->getCollaborator(),$collaborator->getListNMoins1()->toArray()))){
  99.             return false;
  100.         }
  101.         if($absence->getStatus()==Absence::PENDING and $new_status==Absence::CANCELLED and (!$this->isGranted('ROLE_ASSISTANT_RH')) and ( $absence->getCollaborator()!=$collaborator and!$this->isGranted('ROLE_ASSISTANT_RH') and !in_array$absence->getCollaborator(),$collaborator->getListNMoins1()->toArray()))){
  102.             return false;
  103.         }
  104.         if($absence->getStatus()==Absence::VALID and $new_status==Absence::CANCELLED and (!$this->isGranted('ROLE_ASSISTANT_RH'))){
  105.             return false;
  106.         }
  107.         if(in_array($absence->getStatus(),[Absence::CANCELLEDAbsence::REFUSED])){
  108.             return false;
  109.         }
  110.         return true;
  111.     }
  112.     private function canEditManagerComment(Absence $absenceCollaborator $collaborator): bool
  113.     {
  114.         if(
  115.             !$this->isGranted('ROLE_ADMIN')
  116.             and !$this->isGranted('ROLE_ASSISTANT_RH')
  117.             and !$this->isGranted('ROLE_MANAGER_RH')
  118.             //and !in_array($absence->getCollaborator(),$collaborator->getAllListNMoins()->toArray())
  119.         ){
  120.             return false;
  121.         }
  122.         return true;
  123.     }
  124.     private function canEdit(Absence $absenceCollaborator $collaborator): bool
  125.     {
  126.         return false;
  127.     }
  128.     private function isGranted($role)
  129.     {
  130.         return $this->security->isGranted($role);
  131.     }
  132. }