src/Security/SimpleVoter.php line 12

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Security/SimpleVoter.php
  4. //----------------------------------------------------------------------
  5. namespace App\Security;
  6. use App\Entity\Access;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class SimpleVoter extends Voter
  10. {
  11.     //--------------------------------------------------------------------------------
  12.     const OPEN_SESAME "open_sesame";
  13.     //--------------------------------------------------------------------------------
  14.     const ACCESS_BUDGET "access_budget";
  15.     const ACCESS_READING "access_reading";
  16.     const ACCESS_TRACKING "access_tracking";
  17.     const ACCESS_VEHICLE "access_vehicle";
  18.     //--------------------------------------------------------------------------------
  19.     const ACCESS_GLOBALS = array(
  20.         self::OPEN_SESAME,
  21.         self::ACCESS_BUDGET,
  22.         self::ACCESS_READING,
  23.         self::ACCESS_TRACKING,
  24.         self::ACCESS_VEHICLE,
  25.     );
  26.     //--------------------------------------------------------------------------------
  27.     protected function supports(string $attribute$subject): bool
  28.     {
  29.         // if the attribute isn't one we support, return false
  30.         if (!in_array($attributeself::ACCESS_GLOBALS))
  31.         {
  32.             return false;
  33.         }
  34.         // only vote on Post objects
  35.         // if (!$subject instanceof Post)
  36.         // {
  37.         //     return false;
  38.         // }
  39.         return true;
  40.     }
  41.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  42.     {
  43.         $user $token->getUser();
  44.         if (!$user instanceof Access)
  45.         {
  46.             // the user must be logged in; if not, deny access
  47.             return false;
  48.         }
  49.         // The user must be the owner of the object
  50.         if ($subject !== null)
  51.         {
  52.             $owner $subject->getOwner();
  53.             if ($owner === null)
  54.                 return false;
  55.             if (!$owner->equals($user))
  56.                 return false;
  57.         }
  58.         switch ($attribute)
  59.         {
  60.             case self::OPEN_SESAME:
  61.                 return $this->canOpenSesame($user);
  62.             case self::ACCESS_BUDGET:
  63.                 return $this->canAccessBudget($user$subject);
  64.             case self::ACCESS_READING:
  65.                 return $this->canAccessReading($user$subject);
  66.             case self::ACCESS_TRACKING:
  67.                 return $this->canAccessTracking($user$subject);
  68.             case self::ACCESS_VEHICLE:
  69.                 return $this->canAccessVehicle($user$subject);
  70.         }
  71.         throw new \LogicException('This code should not be reached!');
  72.     }
  73.     private function canOpenSesame(Access $user): bool
  74.     {
  75.         if ($user->getId() === 2)
  76.         {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.     private function canAccessBudget(Access $user$subject): bool
  82.     {
  83.         return true;
  84.     }
  85.     private function canAccessReading(Access $user$subject): bool
  86.     {
  87.         return true;
  88.     }
  89.     private function canAccessTracking(Access $user$subject): bool
  90.     {
  91.         return true;
  92.     }
  93.     private function canAccessVehicle(Access $user$subject): bool
  94.     {
  95.         return true;
  96.     }
  97. }