src/Security/Voter/PropertyVoter.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\Property;
  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 PropertyVoter
  16.  *
  17.  * @author NOUTCHEU Blaise
  18.  */
  19. class PropertyVoter extends Voter {
  20.     // these strings are just invented: you can use anything
  21.     const LIST = 'ROLE_JIT_PROPERTY_INDEX';
  22.     const ADD 'ROLE_JIT_PROPERTY_ADD';
  23.     const ADDBP 'ROLE_JIT_PROPERTY_ADDBP';
  24.     const ADDSP 'ROLE_JIT_PROPERTY_ADDSP';
  25.     const VIEW 'ROLE_JIT_PROPERTY_VIEW';
  26.     const CLONE = 'ROLE_JIT_PROPERTY_CLONE';
  27.     const EDIT 'ROLE_JIT_PROPERTY_EDIT';
  28.     const DELETE 'ROLE_JIT_PROPERTY_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 property on his subject
  35.         if (in_array($attribute, [
  36.                     self::LIST,
  37.                     self::ADD,
  38.                     self::ADDBP,
  39.                     self::ADDSP,
  40.                     self::VIEW,
  41.                     self::CLONE,
  42.                     self::EDIT,
  43.                     self::DELETE,
  44.                 ])) {
  45.             return true;
  46.         }
  47.         if (!in_array($attribute, [
  48. //                    self::VIEW,
  49.                 ])) {
  50.             return false;
  51.         }
  52.         // only vote on `Property` objects
  53.         if (!$subject instanceof Property) {
  54.             return false;
  55.         }
  56.         return true;
  57.     }
  58.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  59.         $user $token->getUser();
  60.         if (!$user instanceof User) {
  61.             // the user must be logged in; if not, deny access
  62.             return false;
  63.         }
  64.         // ROLE_JIT_PROPERTY_MANAGE can do anything on property! The power!
  65.         if ($this->security->isGranted('ROLE_MANAGER')) {
  66.             return true;
  67.         }
  68.         switch ($attribute) {
  69.             case self::LIST:
  70.                 return true;
  71.             case self::ADD:
  72.                 return $this->canAdd($user);
  73.             case self::ADDBP:
  74.                 return $this->canAddBP($subject$user);
  75.             case self::ADDSP:
  76.                 return $this->canAddSP($subject$user);
  77.             case self::VIEW:
  78.                 return $this->canView($subject$user);
  79.             case self::CLONE:
  80.                 return $this->canClone($subject$user);
  81.             case self::EDIT:
  82.                 return $this->canEdit($subject$user);
  83.             case self::DELETE:
  84.                 return $this->canDelete($subject$user);
  85.         }
  86.         throw new LogicException('This code should not be reached!');
  87.     }
  88.     private function canAdd(User $user) {
  89.         return true;
  90.     }
  91.     private function canAddBP(\App\Entity\JIT\Person $personUser $user) { 
  92.         return true;
  93.     }
  94.     private function canAddSP(Property $propertyUser $user) { 
  95.         return true;
  96.     }
  97.     
  98.     private function canView(Property $propertyUser $user) {
  99.        
  100.         return true;
  101.     }
  102.     private function canClone(Property $propertyUser $user) {
  103. //        if($property->getAffair() !== null){
  104. //            return $this->canAdd($property->getAffair(),$user) && $this->canView($property, $user);
  105. //        }else{
  106. //            return false;
  107. //        }
  108.         return true;
  109.     }
  110.     
  111.     private function canEdit(Property $propertyUser $user) {
  112.         //Uniquement le chef d'agence peut modifier une propertyne
  113. //        if ($user->getMyAgencies()->contains($property->getAgency())) {
  114. //            return true;
  115. //        }
  116.         return $this->canView($property$user);
  117.     }
  118.     private function canDelete(Property $propertyUser $user) {
  119.         //Uniquement le chef d'agence peut modifier une propertyne
  120. //        if ($user->getMyAgencies()->contains($property->getAgency())) {
  121. //            return true;
  122. //        }
  123.         return $this->canView($property$user);
  124.     }
  125. }