vendor/crea/hr-bundle/src/Voter/HRVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace Crea\HRBundle\Voter;
  3. use Crea\HRBundle\Entity\PlanningComment;
  4. use Crea\HRBundle\Entity\PlanningEvent;
  5. use Crea\HRBundle\Entity\PlanningLeave;
  6. use Crea\HRBundle\Provider\HRRightCodeProvider;
  7. use Exception;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Crea\SecurityBundle\Entity\User;
  12. class HRVoter extends Voter
  13. {
  14.     /**
  15.      * @inheritDoc
  16.      * @throws Exception
  17.      */
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         $attributes = [
  21.             HRRightCodeProvider::PLANNING_LEAVE_LIST,
  22.             HRRightCodeProvider::PLANNING_LEAVE_CREATE,
  23.             HRRightCodeProvider::PLANNING_EVENT_LIST,
  24.             HRRightCodeProvider::PLANNING_EVENT_CREATE,
  25.             HRRightCodeProvider::PLANNING_COMMENT_LIST,
  26.             HRRightCodeProvider::PLANNING_COMMENT_CREATE
  27.         ];
  28.         if ($subject instanceof PlanningLeave) {
  29.             $attributes[] = HRRightCodeProvider::PLANNING_LEAVE_UPDATE;
  30.             $attributes[] = HRRightCodeProvider::PLANNING_LEAVE_REMOVE;
  31.         }
  32.         if ($subject instanceof PlanningEvent) {
  33.             $attributes[] = HRRightCodeProvider::PLANNING_EVENT_UPDATE;
  34.             $attributes[] = HRRightCodeProvider::PLANNING_EVENT_REMOVE;
  35.         }
  36.         if ($subject instanceof PlanningComment) {
  37.             $attributes[] = HRRightCodeProvider::PLANNING_COMMENT_UPDATE;
  38.             $attributes[] = HRRightCodeProvider::PLANNING_COMMENT_REMOVE;
  39.         }
  40.         return in_array($attribute$attributes);
  41.     }
  42.     /**
  43.      * @inheritDoc
  44.      * @throws Exception
  45.      */
  46.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  47.     {
  48.         /** @var User $loggedUser */
  49.         $loggedUser $token->getUser();
  50.         if (!$loggedUser instanceof UserInterface) {
  51.             return false;
  52.         }
  53.         switch ($attribute) {
  54.             case HRRightCodeProvider::PLANNING_LEAVE_LIST:
  55.                 return $this->voteOnPlanningLeaveList($loggedUser);
  56.             case HRRightCodeProvider::PLANNING_LEAVE_CREATE:
  57.                 return $this->voteOnPlanningLeaveCreate($loggedUser);
  58.             case HRRightCodeProvider::PLANNING_EVENT_LIST:
  59.                 return $this->voteOnPlanningEventList($loggedUser);
  60.             case HRRightCodeProvider::PLANNING_EVENT_CREATE:
  61.                 return $this->voteOnPlanningEventCreate($loggedUser);
  62.             case HRRightCodeProvider::PLANNING_COMMENT_LIST:
  63.                 return $this->voteOnPlanningCommentList($loggedUser);
  64.             case HRRightCodeProvider::PLANNING_COMMENT_CREATE:
  65.                 return $this->voteOnPlanningCommentCreate($loggedUser);
  66.         }
  67.         if ($subject instanceof PlanningLeave) {
  68.             switch ($attribute) {
  69.                 case HRRightCodeProvider::PLANNING_LEAVE_UPDATE:
  70.                     return $this->voteOnPlanningLeaveUpdate($loggedUser);
  71.                 case HRRightCodeProvider::PLANNING_LEAVE_REMOVE:
  72.                     return $this->voteOnPlanningLeaveRemove($loggedUser);
  73.             }
  74.         }
  75.         if ($subject instanceof PlanningEvent) {
  76.             switch ($attribute) {
  77.                 case HRRightCodeProvider::PLANNING_EVENT_UPDATE:
  78.                     return $this->voteOnPlanningEventUpdate($loggedUser);
  79.                 case HRRightCodeProvider::PLANNING_EVENT_REMOVE:
  80.                     return $this->voteOnPlanningEventRemove($loggedUser);
  81.             }
  82.         }
  83.         if ($subject instanceof PlanningComment) {
  84.             switch ($attribute) {
  85.                 case HRRightCodeProvider::PLANNING_COMMENT_UPDATE:
  86.                     return $this->voteOnPlanningCommentUpdate($loggedUser);
  87.                 case HRRightCodeProvider::PLANNING_COMMENT_REMOVE:
  88.                     return $this->voteOnPlanningCommentRemove($loggedUser);
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.     /**
  94.      * @throws Exception
  95.      */
  96.     private function voteOnPlanningLeaveList(UserInterface $loggedUser): bool
  97.     {
  98.         return in_array(HRRightCodeProvider::PLANNING_LEAVE_LIST$loggedUser->getRoles());
  99.     }
  100.     /**
  101.      * @throws Exception
  102.      */
  103.     private function voteOnPlanningLeaveCreate(UserInterface $loggedUser): bool
  104.     {
  105.         return in_array(HRRightCodeProvider::PLANNING_LEAVE_CREATE$loggedUser->getRoles());
  106.     }
  107.     /**
  108.      * @throws Exception
  109.      */
  110.     private function voteOnPlanningLeaveUpdate(UserInterface $loggedUser): bool
  111.     {
  112.         return in_array(HRRightCodeProvider::PLANNING_LEAVE_UPDATE$loggedUser->getRoles());
  113.     }
  114.     /**
  115.      * @throws Exception
  116.      */
  117.     private function voteOnPlanningLeaveRemove(UserInterface $loggedUser): bool
  118.     {
  119.         return in_array(HRRightCodeProvider::PLANNING_LEAVE_REMOVE$loggedUser->getRoles());
  120.     }
  121.     /**
  122.      * @throws Exception
  123.      */
  124.     private function voteOnPlanningEventList(UserInterface $loggedUser): bool
  125.     {
  126.         return in_array(HRRightCodeProvider::PLANNING_EVENT_LIST$loggedUser->getRoles());
  127.     }
  128.     /**
  129.      * @throws Exception
  130.      */
  131.     private function voteOnPlanningEventCreate(UserInterface $loggedUser): bool
  132.     {
  133.         return in_array(HRRightCodeProvider::PLANNING_EVENT_CREATE$loggedUser->getRoles());
  134.     }
  135.     /**
  136.      * @throws Exception
  137.      */
  138.     private function voteOnPlanningEventUpdate(UserInterface $loggedUser): bool
  139.     {
  140.         return in_array(HRRightCodeProvider::PLANNING_EVENT_UPDATE$loggedUser->getRoles());
  141.     }
  142.     /**
  143.      * @throws Exception
  144.      */
  145.     private function voteOnPlanningEventRemove(UserInterface $loggedUser): bool
  146.     {
  147.         return in_array(HRRightCodeProvider::PLANNING_EVENT_REMOVE$loggedUser->getRoles());
  148.     }
  149.     /**
  150.      * @throws Exception
  151.      */
  152.     private function voteOnPlanningCommentList(UserInterface $loggedUser): bool
  153.     {
  154.         return in_array(HRRightCodeProvider::PLANNING_COMMENT_LIST$loggedUser->getRoles());
  155.     }
  156.     /**
  157.      * @throws Exception
  158.      */
  159.     private function voteOnPlanningCommentCreate(UserInterface $loggedUser): bool
  160.     {
  161.         return in_array(HRRightCodeProvider::PLANNING_COMMENT_CREATE$loggedUser->getRoles());
  162.     }
  163.     /**
  164.      * @throws Exception
  165.      */
  166.     private function voteOnPlanningCommentUpdate(UserInterface $loggedUser): bool
  167.     {
  168.         return in_array(HRRightCodeProvider::PLANNING_COMMENT_UPDATE$loggedUser->getRoles());
  169.     }
  170.     /**
  171.      * @throws Exception
  172.      */
  173.     private function voteOnPlanningCommentRemove(UserInterface $loggedUser): bool
  174.     {
  175.         return in_array(HRRightCodeProvider::PLANNING_COMMENT_REMOVE$loggedUser->getRoles());
  176.     }
  177. }