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