src/Security/TripVoter.php line 11

Open in your IDE?
  1. <?php 
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use App\Entity\TripEntities;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. class TripVoter extends Voter
  9. {
  10.     private $security;
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     
  16.     // these strings are just invented: you can use anything
  17.     const VIEW 'view';
  18.     const EDIT 'edit';
  19.     const NEW = 'create';
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         // if the attribute isn't one we support, return false
  23.         if (!in_array($attribute, [self::VIEWself::EDITself::NEW])) {
  24.             return false;
  25.         }
  26.         // only vote on `Post` objects
  27.         if (!$subject instanceof TripEntities) {
  28.             return false;
  29.         }
  30.         return true;
  31.     }
  32.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  33.     {
  34.         $loggedUser $token->getUser();
  35.        
  36.         if (!$loggedUser instanceof User) {
  37.             // the user must be logged in; if not, deny access
  38.             return false;
  39.         }
  40.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  41.             return true;
  42.         }
  43.         if ($this->security->isGranted('KOBEN_1_SUPER ADMIN')) {
  44.             return true;
  45.         }
  46.         // you know $subject is a Post object, thanks to `supports()`
  47.         /** @var User $user */
  48.         $user $subject;
  49.         switch ($attribute) {
  50.             case self::VIEW:
  51.                 return $this->canView($user$loggedUser);
  52.             case self::EDIT:
  53.                 return $this->canEdit($user$loggedUser);
  54.             case self::NEW:
  55.                 return $this->canCreate($loggedUser);
  56.         }
  57.         throw new \LogicException('This code should not be reached!');
  58.     }
  59.     private function canView(User $userUser $loggedUser): bool
  60.     {
  61.         // if they can edit, they can view
  62.         if ($this->canEdit($user$loggedUser)) {
  63.             return true;
  64.         }
  65.       
  66.             return true;
  67.      
  68.        
  69.     }
  70.     private function canEdit(User $companyUser $loggedUser): bool
  71.     {
  72.         if ($this->security->isGranted('KOBEN_2_MANAGEMENT')
  73.         ||$this->security->isGranted('KOBEN_4_HR')
  74.         ||$this->security->isGranted('KOBEN_3_MANAGER')
  75.         ||$this->security->isGranted('KOBEN_2_MANAGEMENT')
  76.         ||$this->security->isGranted('CLIENT_3_HR')
  77.         ||$this->security->isGranted('CLIENT_2_MANAGEMENT')
  78.         ||$this->security->isGranted('CLIENT_1_ADMIN')) {
  79.             return true;
  80.         }
  81.         return false;
  82.     }
  83.     private function canCreate(User $loggedUser): bool
  84.     {
  85.         if ($this->security->isGranted('KOBEN_2_MANAGEMENT')
  86.         ||$this->security->isGranted('KOBEN_4_HR')
  87.         ||$this->security->isGranted('KOBEN_3_MANAGER')
  88.         ||$this->security->isGranted('KOBEN_2_MANAGEMENT')
  89.         ||$this->security->isGranted('CLIENT_3_HR')
  90.         ||$this->security->isGranted('CLIENT_2_MANAGEMENT')
  91.         ||$this->security->isGranted('CLIENT_1_ADMIN')) {
  92.             return true;
  93.         }
  94.        return false;
  95.     }
  96. }