src/Security/Voter/StepVoter.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. namespace App\Security\Voter;
  8. use App\Entity\JIT\Service;
  9. use App\Entity\JIT\Step;
  10. use App\Entity\Security\User;
  11. use LogicException;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\Security;
  15. /**
  16.  * Description of StepVoter
  17.  *
  18.  * @author NOUTCHEU Blaise
  19.  */
  20. class StepVoter extends Voter {
  21.     // these strings are just invented: you can use anything
  22.     const ADD 'ROLE_JIT_STEP_ADD';
  23.     const VIEW 'ROLE_JIT_STEP_VIEW';
  24.     const CLONE = 'ROLE_JIT_STEP_CLONE';
  25.     const EDIT 'ROLE_JIT_STEP_EDIT';
  26.     const DELETE 'ROLE_JIT_STEP_DELETE';
  27.     private $security;
  28.     public function __construct(Security $security) {
  29.         $this->security $security;
  30.     }
  31.     protected function supports(string $attribute$subject) {
  32.         // only a teacher can add step on his subject
  33.         if (in_array($attribute, [
  34.                     self::ADD,
  35.                     self::VIEW,
  36.                     self::CLONE,
  37.                     self::EDIT,
  38.                     self::DELETE,
  39.                 ])) {
  40.             return true;
  41.         }
  42.         if (!in_array($attribute, [
  43. //                    self::VIEW,
  44.                 ])) {
  45.             return false;
  46.         }
  47.         // only vote on `Step` objects
  48.         if (!$subject instanceof Step) {
  49.             return false;
  50.         }
  51.         return true;
  52.     }
  53.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  54.         $user $token->getUser();
  55.         if (!$user instanceof User) {
  56.             // the user must be logged in; if not, deny access
  57.             return false;
  58.         }
  59.         // ROLE_JIT_STEP_MANAGE can do anything on step! The power!
  60.         if ($this->security->isGranted('ROLE_MANAGER')) {
  61.             return true;
  62.         }
  63.         switch ($attribute) {
  64.             case self::ADD:
  65.                 return $this->canAdd($subject$user);
  66.             case self::VIEW:
  67.                 return $this->canView($subject$user);
  68.             case self::CLONE:
  69.                 return $this->canClone($subject$user);
  70.             case self::EDIT:
  71.                 return $this->canEdit($subject$user);
  72.             case self::DELETE:
  73.                 return $this->canDelete($subject$user);
  74.         }
  75.         throw new LogicException('This code should not be reached!');
  76.     }
  77.     private function canAdd(Service $serviceUser $user) {
  78.         return true;
  79.     }
  80.     private function canView(Step $stepUser $user) {
  81.        
  82.         return true;
  83.     }
  84.     private function canClone(Step $stepUser $user) {
  85.         if($step->getAffair() !== null){
  86.             return $this->canAdd($step->getAffair(),$user) && $this->canView($step$user);
  87.         }else{
  88.             return false;
  89.         }
  90.     }
  91.     
  92.     private function canEdit(Step $stepUser $user) {
  93.         //Uniquement le chef d'agence peut modifier une stepne
  94. //        if ($user->getMyAgencies()->contains($step->getAgency())) {
  95. //            return true;
  96. //        }
  97.         return $this->canView($step$user);
  98.     }
  99.     private function canDelete(Step $stepUser $user) {
  100.         //Uniquement le chef d'agence peut modifier une stepne
  101. //        if ($user->getMyAgencies()->contains($step->getAgency())) {
  102. //            return true;
  103. //        }
  104.         return $this->canView($step$user);
  105.     }
  106. }