src/Security/CollaboratorVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Collaborator;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. class CollaboratorVoter extends Voter
  10. {
  11.     const VIEW              'view';
  12.     const EDIT_PROFIL_LIGHT 'edit_profil_light';
  13.     const EDIT_INTERVIEW    'edit_interview';
  14.     const NEW               = 'new';
  15.     private $security;
  16.     private $translator;
  17.     public function __construct(Security $security,TranslatorInterface $translator)
  18.     {
  19.         $this->security $security;
  20.         $this->translator=$translator;
  21.     }
  22.     protected function supports(string $attribute$subject): bool
  23.     {
  24.         // if the attribute isn't one we support, return false
  25.         if (!in_array($attribute, [self::VIEWself::EDIT_PROFIL_LIGHT,self::EDIT_INTERVIEW,self::NEW])) {
  26.             return false;
  27.         }
  28.         // only vote on `Collaborator` objects
  29.         if (!$subject instanceof Collaborator) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         $collaborator_connected $token->getUser()->getCollaborator();
  37.         if (!$collaborator_connected instanceof Collaborator) {
  38.             return false;
  39.         }
  40.         // you know $subject is a Collaborator object, thanks to `supports()`
  41.         /** @var Collaborator $collaborator */
  42.         $collaborator $subject;
  43.         switch ($attribute) {
  44.             case self::VIEW:
  45.                 return $this->canView($collaborator,$collaborator_connected);
  46.             case self::NEW:
  47.                 return $this->canNew($collaborator,$collaborator_connected);
  48.             case self::EDIT_PROFIL_LIGHT:
  49.                 return $this->canEditProfileLight($collaborator,$collaborator_connected);
  50.             case self::EDIT_INTERVIEW:
  51.                 return $this->canEditInterview($collaborator,$collaborator_connected);
  52.         }
  53.         throw new \LogicException('This code should not be reached!');
  54.     }
  55.     private function canNew(Collaborator $collaborator,Collaborator $collaborator_connected): bool
  56.     {
  57.         return $this->isGranted('ROLE_ASSISTANT_RH');
  58.     }
  59.     private function canView(Collaborator $collaborator,Collaborator $collaborator_connected): bool
  60.     {
  61.         if(
  62.             !$this->isGranted('ROLE_ASSISTANT_RH')
  63.             and $collaborator_connected!=$collaborator
  64.             and !in_array($collaborator,$collaborator_connected->getAllListNMoins()->toArray())
  65.         ){
  66.             return false;
  67.         }
  68.         return true;
  69.     }
  70.     private function canEditProfileLight(Collaborator $collaborator,Collaborator $collaborator_connected)
  71.     {
  72.         return $this->isGranted('ROLE_ASSISTANT_RH') or $collaborator->getId()==$collaborator_connected->getId();
  73.     }
  74.     private function canEditInterview(Collaborator $collaborator,Collaborator $collaborator_connected)
  75.     {
  76.         return $this->isGranted('ROLE_ASSISTANT_RH') or in_array($collaborator,$collaborator_connected->getAllListNMoins()->toArray());
  77.     }
  78.     private function isGranted($role)
  79.     {
  80.         return $this->security->isGranted($role);
  81.     }
  82. }