src/Security/Voter/AgencyVoter.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\Agency;
  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 AgencyVoter
  16.  *
  17.  * @author NOUTCHEU Blaise
  18.  */
  19. class AgencyVoter extends Voter {
  20.     // these strings are just invented: you can use anything
  21.     const LIST = 'ROLE_JIT_AGENCY_INDEX';
  22.     const ADD 'ROLE_JIT_AGENCY_ADD';
  23.     const VIEW 'ROLE_JIT_AGENCY_VIEW';
  24.     const CLONE = 'ROLE_JIT_AGENCY_CLONE';
  25.     const EDIT 'ROLE_JIT_AGENCY_EDIT';
  26.     const DELETE 'ROLE_JIT_AGENCY_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 agency on his subject
  33.         if (in_array($attribute, [
  34.                     self::LIST,
  35.                     self::ADD,
  36.                     self::VIEW,
  37.                     self::CLONE,
  38.                     self::EDIT,
  39.                     self::DELETE,
  40.                 ])) {
  41.             return true;
  42.         }
  43.         if (!in_array($attribute, [
  44. //                    self::VIEW,
  45.                 ])) {
  46.             return false;
  47.         }
  48.         // only vote on `Agency` objects
  49.         if (!$subject instanceof Agency) {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  55.         $user $token->getUser();
  56.         if (!$user instanceof User) {
  57.             // the user must be logged in; if not, deny access
  58.             return false;
  59.         }
  60.         // ROLE_JIT_AGENCY_MANAGE can do anything on agency! The power!
  61.         if ($this->security->isGranted('ROLE_MANAGER')) {
  62.             return true;
  63.         }
  64.         switch ($attribute) {
  65.             case self::LIST:
  66.                 return $this->canList($user);
  67.             case self::ADD:
  68.                 return $this->canAdd($user);
  69.             case self::VIEW:
  70.                 return $this->canView($subject$user);
  71.             case self::CLONE:
  72.                 return $this->canClone($subject$user) ;
  73.             case self::EDIT:
  74.                 return $this->canEdit($subject$user);
  75.             case self::DELETE:
  76.                 return $this->canDelete($subject$user);
  77.         }
  78.         throw new LogicException('This code should not be reached!');
  79.     }
  80.     private function canList(User $user) {
  81.         return true;
  82.     }
  83.     private function canAdd(User $user) {
  84.         return true;
  85.     }
  86.     private function canView(Agency $agencyUser $user) {
  87.         return true;
  88.     }
  89.     private function canClone(Agency $agencyUser $user) {
  90.         return $this->canAdd($user) && $this->canView($agency$user);
  91.     }
  92.     private function canEdit(Agency $agencyUser $user) {
  93.         //Uniquement le chef d'agence peut modifier une agencyne
  94. //        if ($user->getMyAgencies()->contains($agency->getAgency())) {
  95. //            return true;
  96. //        }
  97.         return $this->canView($agency$user);
  98.     }
  99.     private function canDelete(Agency $agencyUser $user) {
  100.         //Uniquement le chef d'agence peut modifier une agencyne
  101. //        if ($user->getMyAgencies()->contains($agency->getAgency())) {
  102. //            return true;
  103. //        }
  104.         return $this->canView($agency$user);
  105.     }
  106. }