src/Security/TaskVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Collaborator;
  4. use App\Entity\Project\Task;
  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\Contracts\Translation\TranslatorInterface;
  9. class TaskVoter extends Voter
  10. {
  11.     const VIEW          'view';
  12.     const NEW          = 'new';
  13.     const EDIT          'edit';
  14.     private $security;
  15.     private $translator;
  16.     public function __construct(Security $security,TranslatorInterface $translator)
  17.     {
  18.         $this->security $security;
  19.         $this->translator=$translator;
  20.     }
  21.     protected function supports(string $attribute$subject): bool
  22.     {
  23.         // if the attribute isn't one we support, return false
  24.         if (!in_array($attribute, [self::VIEWself::EDITself::NEW])) {
  25.             return false;
  26.         }
  27.         // only vote on `Task` objects
  28.         if (!$subject instanceof Task) {
  29.             return false;
  30.         }
  31.         return true;
  32.     }
  33.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  34.     {   
  35.         $collaborator $token->getUser()->getCollaborator();
  36.         if (!$collaborator instanceof Collaborator) {
  37.             return false;
  38.         }
  39.         // you know $subject is a Task object, thanks to `supports()`
  40.         /** @var Task $task */
  41.         $task $subject;
  42.         switch ($attribute) {
  43.             case self::VIEW:
  44.                 return $this->canView($task$collaborator);
  45.             case self::EDIT:
  46.                 return $this->canEdit($task$collaborator);
  47.             case self::NEW:
  48.                 return $this->canNew($task$collaborator);
  49.         }
  50.         throw new \LogicException('This code should not be reached!');
  51.     }
  52.     private function canView(Task $taskCollaborator $collaborator): bool
  53.     {
  54.         if($project=$task->getProject()){
  55.             if(
  56.                 !$this->isGranted('ROLE_ADMIN')
  57.                 and !in_array($collaborator,$project->getCollaborators()->toArray())
  58.                 and $task->getManager()!=$collaborator
  59.             ){
  60.                 return false;
  61.             }
  62.         }else{
  63.             if(
  64.                 !$this->isGranted('ROLE_ADMIN')
  65.                 and !in_array($collaborator,$task->getCollaborators()->toArray())
  66.                 and $task->getManager()!=$collaborator
  67.             ){
  68.                 return false;
  69.             }
  70.         }
  71.         return true;
  72.     }
  73.     private function canEdit(Task $taskCollaborator $collaborator): bool
  74.     {
  75.         if(
  76.             !$this->isGranted('ROLE_ADMIN')
  77.             and $task->getManager()!=$collaborator
  78.         ){
  79.             return false;
  80.         }
  81.         return true;
  82.     }
  83.     private function canNew(Task $taskCollaborator $collaborator): bool
  84.     {
  85.         if(
  86.             !$this->isGranted('ROLE_ADMIN')
  87.             and !$this->isGranted('ROLE_MANAGER_RH')
  88.         ){
  89.             return false;
  90.         }
  91.         return true;
  92.     }
  93.     private function isGranted($role)
  94.     {
  95.         return $this->security->isGranted($role);
  96.     }
  97. }