src/Security/Voter/ServiceVoter.php line 23

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\Security\User;
  10. use LogicException;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Security;
  14. /**
  15.  * Description of ServiceVoter
  16.  *
  17.  * @author NOUTCHEU Blaise
  18.  */
  19. class ServiceVoter extends Voter {
  20.     // these strings are just invented: you can use anything
  21. //    const LIST = 'ROLE_JIT_SERVICE_INDEX';
  22.     const LIST = 'ROLE_JIT_SERVICE_INDEX';
  23.     const ADD 'ROLE_JIT_SERVICE_ADD';
  24.     const ADDSS 'ROLE_JIT_SERVICE_ADDSS';
  25.     const VIEW 'ROLE_JIT_SERVICE_VIEW';
  26.     const CLONE = 'ROLE_JIT_SERVICE_CLONE';
  27.     const EDIT 'ROLE_JIT_SERVICE_EDIT';
  28.     const DELETE 'ROLE_JIT_SERVICE_DELETE';
  29.     private $security;
  30.     public function __construct(Security $security) {
  31.         $this->security $security;
  32.     }
  33.     protected function supports(string $attribute$subject) {
  34.         // only a teacher can add service on his subject
  35.         if (in_array($attribute, [
  36.                     self::LIST,
  37.                     self::ADD,
  38.                     self::VIEW,
  39.                     self::CLONE,
  40.                     self::EDIT,
  41.                     self::DELETE,
  42.                 ])) {
  43.             return true;
  44.         }
  45.         if (!in_array($attribute, [
  46. //                    self::VIEW,
  47.                 ])) {
  48.             return false;
  49.         }
  50.         // only vote on `Service` objects
  51.         if (!$subject instanceof Service) {
  52.             return false;
  53.         }
  54.         return true;
  55.     }
  56.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  57.         $user $token->getUser();
  58.         if (!$user instanceof User) {
  59.             // the user must be logged in; if not, deny access
  60.             return false;
  61.         }
  62.         // ROLE_JIT_SERVICE_MANAGE can do anything on service! The power!
  63.         if ($this->security->isGranted('ROLE_MANAGER')) {
  64.             return true;
  65.         }
  66.         switch ($attribute) {
  67.             case self::LIST:
  68.                 return $this->canList($user);
  69.             case self::ADD:
  70.                 return $this->canAdd($user);
  71.             case self::VIEW:
  72.                 return $this->canView($subject$user);
  73.             case self::CLONE:
  74.                 return $this->canClone($subject$user);
  75.             case self::EDIT:
  76.                 return $this->canEdit($subject$user);
  77.             case self::DELETE:
  78.                 return $this->canDelete($subject$user);
  79.         }
  80.         throw new LogicException('This code should not be reached!');
  81.     }
  82.     private function canList(User $user) {
  83.         return true;
  84.     }
  85.     private function canAdd(User $user) {
  86.         return true;
  87.     }
  88.     private function canView(Service $serviceUser $user) {
  89.         return true;
  90.     }
  91.     private function canClone(Service $serviceUser $user) {
  92.         return $this->canAdd($user) && $this->canView($service$user);
  93.     }
  94.     private function canEdit(Service $serviceUser $user) {
  95.         //Uniquement le chef d'agence peut modifier une servicene
  96. //        if ($user->getMyAgencies()->contains($service->getService())) {
  97. //            return true;
  98. //        }
  99.         return $this->canView($service$user);
  100.     }
  101.     private function canDelete(Service $serviceUser $user) {
  102.         //Uniquement le chef d'agence peut modifier une servicene
  103. //        if ($user->getMyAgencies()->contains($service->getService())) {
  104. //            return true;
  105. //        }
  106.         return $this->canView($service$user);
  107.     }
  108. }