src/Security/Voter/InventoryVoter.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\Inventory;
  9. use App\Entity\JIT\Tenancy;
  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 InventoryVoter
  17.  *
  18.  * @author NOUTCHEU Blaise
  19.  */
  20. class InventoryVoter extends Voter {
  21.     // these strings are just invented: you can use anything
  22. //    const LIST = 'ROLE_JIT_INVENTORY_INDEX';
  23.     const ADD 'ROLE_JIT_INVENTORY_ADD';
  24.     const VIEW 'ROLE_JIT_INVENTORY_VIEW';
  25.     const CLONE = 'ROLE_JIT_INVENTORY_CLONE';
  26.     const EDIT 'ROLE_JIT_INVENTORY_EDIT';
  27.     const DELETE 'ROLE_JIT_INVENTORY_DELETE';
  28.     private $security;
  29.     public function __construct(Security $security) {
  30.         $this->security $security;
  31.     }
  32.     protected function supports(string $attribute$subject) {
  33.         // only a teacher can add inventory on his subject
  34.         if (in_array($attribute, [
  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 `Inventory` objects
  49.         if (!$subject instanceof Inventory) {
  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_INVENTORY_MANAGE can do anything on inventory! The power!
  61.         if ($this->security->isGranted('ROLE_MANAGER')) {
  62.             return true;
  63.         }
  64.         switch ($attribute) {
  65.             case self::ADD:
  66.                 return $this->canAdd($subject$user);
  67.             case self::VIEW:
  68.                 return $this->canView($subject$user);
  69.             case self::CLONE:
  70.                 return $this->canClone($subject$user);
  71.             case self::EDIT:
  72.                 return $this->canEdit($subject$user);
  73.             case self::DELETE:
  74.                 return $this->canDelete($subject$user);
  75.         }
  76.         throw new LogicException('This code should not be reached!');
  77.     }
  78.     private function canAdd(Tenancy $tenancyUser $user) {
  79.         return true;
  80.     }
  81.     private function canView(Inventory $inventoryUser $user) {
  82.        
  83.         return true;
  84.     }
  85.     private function canClone(Inventory $inventoryUser $user) {
  86.         return true;
  87.     }
  88.     
  89.     private function canEdit(Inventory $inventoryUser $user) {
  90.         //Uniquement le chef d'agence peut modifier une inventoryne
  91. //        if ($user->getMyAgencies()->contains($inventory->getAgency())) {
  92. //            return true;
  93. //        }
  94.         return $this->canView($inventory$user);
  95.     }
  96.     private function canDelete(Inventory $inventoryUser $user) {
  97.         //Uniquement le chef d'agence peut modifier une inventoryne
  98. //        if ($user->getMyAgencies()->contains($inventory->getAgency())) {
  99. //            return true;
  100. //        }
  101.         return $this->canView($inventory$user);
  102.     }
  103. }