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_READING "access_reading";
  15.     const ACCESS_TRACKING "access_tracking";
  16.     const ACCESS_VEHICLE "access_vehicle";
  17.     //--------------------------------------------------------------------------------
  18.     const ACCESS_GLOBALS = array(
  19.         self::OPEN_SESAME,
  20.         self::ACCESS_READING,
  21.         self::ACCESS_TRACKING,
  22.         self::ACCESS_VEHICLE,
  23.     );
  24.     //--------------------------------------------------------------------------------
  25.     protected function supports(string $attribute$subject): bool
  26.     {
  27.         // if the attribute isn't one we support, return false
  28.         if (!in_array($attributeself::ACCESS_GLOBALS))
  29.         {
  30.             return false;
  31.         }
  32.         // only vote on Post objects
  33.         // if (!$subject instanceof Post)
  34.         // {
  35.         //     return false;
  36.         // }
  37.         return true;
  38.     }
  39.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  40.     {
  41.         $user $token->getUser();
  42.         if (!$user instanceof Access)
  43.         {
  44.             // the user must be logged in; if not, deny access
  45.             return false;
  46.         }
  47.         // The user must be the owner of the object
  48.         if ($subject !== null)
  49.         {
  50.             $owner $subject->getOwner();
  51.             if ($owner === null)
  52.                 return false;
  53.             if (!$owner->equals($user))
  54.                 return false;
  55.         }
  56.         switch ($attribute)
  57.         {
  58.             case self::OPEN_SESAME:
  59.                 return $this->canOpenSesame($user);
  60.             case self::ACCESS_READING:
  61.                 return $this->canAccessReading($user$subject);
  62.             case self::ACCESS_TRACKING:
  63.                 return $this->canAccessTracking($user$subject);
  64.             case self::ACCESS_VEHICLE:
  65.                 return $this->canAccessVehicle($user$subject);
  66.         }
  67.         throw new \LogicException('This code should not be reached!');
  68.     }
  69.     private function canOpenSesame(Access $user): bool
  70.     {
  71.         if ($user->getId() === 2)
  72.         {
  73.             return true;
  74.         }
  75.         return false;
  76.     }
  77.     private function canAccessReading(Access $user$subject): bool
  78.     {
  79.         return true;
  80.     }
  81.     private function canAccessTracking(Access $user$subject): bool
  82.     {
  83.         return true;
  84.     }
  85.     private function canAccessVehicle(Access $user$subject): bool
  86.     {
  87.         return true;
  88.     }
  89. }