src/Security/Voters/RecruitVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Collab;
  4. use App\Entity\Equipment;
  5. use App\Entity\Recruit;
  6. use App\Entity\RecruitContract;
  7. use App\Entity\User;
  8. use App\Entity\Vehicule;
  9. use App\Security\SecurityTrait;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. class RecruitVoter extends Voter
  14. {
  15.     use SecurityTrait;
  16.     const LIST = "list_recruit";
  17.     const READ "read_recruit";
  18.     const CONTRACT "contract_recruit";
  19.     const READ_CONFIDENTIAL_CONTRACT "read_confidential_contract_recruit";
  20.     const TRANSFERT "transfert_recruit";
  21.     const SEND_SHEET "send_sheet_recruit";
  22.     const ADD "add_recruit";
  23.     const REMOVE "remove_recruit";
  24.     private $security;
  25.     public function __construct(Security $security)
  26.     {
  27.         $this->security $security;
  28.     }
  29.     protected function supports($attribute$subject)
  30.     {
  31.         return in_array($attribute, [self::LIST, self::ADD]) ||
  32.             (in_array($attribute, [
  33.                 self::READ,
  34.                 self::CONTRACT,
  35.                 self::TRANSFERT,
  36.                 self::SEND_SHEET,
  37.                 self::REMOVE
  38.             ]) && $subject instanceof Recruit) ||
  39.             (in_array($attribute, [
  40.                 self::READ_CONFIDENTIAL_CONTRACT
  41.             ]) && $subject instanceof RecruitContract);
  42.     }
  43.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  44.     {
  45.         $user $token->getUser();
  46.         if (!$user instanceof User) {
  47.             return false;
  48.         }
  49.         switch ($attribute) {
  50.             case self::LIST:
  51.                 return $this->isAdmin() || $this->isRH();
  52.             case self::READ:
  53.                 return $this->isAdmin() || $this->isRH();
  54.             case self::CONTRACT:
  55.                 return $this->isAdmin() || $this->isRh();
  56.             case self::READ_CONFIDENTIAL_CONTRACT:
  57.                 return (
  58.                     $this->isAdmin() ||
  59.                     $subject->getStatus() === null ||
  60.                     $subject->getStatus()->getGrade() < 5
  61.                 );
  62.             case self::TRANSFERT:
  63.                 return ($this->isAdmin() || $this->isRH()) && $subject->getRecruitContract() !== null;
  64.             case self::SEND_SHEET:
  65.                 return $this->isAdmin() || $this->isRH();
  66.             case self::ADD:
  67.                 return $this->isAdmin() || $this->isRH();
  68.             case self::REMOVE:
  69.                 return $this->isAdmin();
  70.         }
  71.         throw new \LogicException('This code should not be reached!');
  72.     }
  73. }