src/Security/UserVoter.php line 13

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Security/UserVoter.php
  4. //----------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use App\Entity\Access;
  9. class UserVoter extends Voter
  10. {
  11.     //--------------------------------------------------------------------------------
  12.     const DASHBOARD "user_dashboard";
  13.     const ADD "user_add";
  14.     const EDIT "user_edit";
  15.     const DELETE "user_delete";
  16.     //--------------------------------------------------------------------------------
  17.     const PERMISSIONS = array(
  18.         self::DASHBOARD,
  19.         self::ADD,
  20.         self::EDIT,
  21.         self::DELETE,
  22.     );
  23.     //--------------------------------------------------------------------------------
  24.     protected function supports(string $attribute$subject): bool
  25.     {
  26.         // if the attribute isn't one we support, return false
  27.         if (!in_array($attributeself::PERMISSIONS))
  28.         {
  29.             return false;
  30.         }
  31.         // Only vote on Access objects (if subject is not null)
  32.         if ($subject !== null && !$subject instanceof Access)
  33.         {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  39.     {
  40.         $user $token->getUser();
  41.         if (!$user instanceof Access)
  42.         {
  43.             // the user must be logged in; if not, deny access
  44.             return false;
  45.         }
  46.         // The user must be the owner of the object
  47.         // if ($subject !== null)
  48.         // {
  49.         //     $owner = $subject->getOwner();
  50.         //     if ($owner === null)
  51.         //         return false;
  52.         //     if (!$owner->equals($user))
  53.         //         return false;
  54.         // }
  55.         switch ($attribute)
  56.         {
  57.             case self::DASHBOARD:
  58.                 return $this->canAccessDashboard($user);
  59.             case self::ADD:
  60.                 return $this->canAdd($user);
  61.             case self::EDIT:
  62.                 return $this->canEdit($user$subject);
  63.             case self::DELETE:
  64.                 return $this->canDelete($user$subject);
  65.         }
  66.         throw new \LogicException('This code should not be reached!');
  67.     }
  68.     private function canAccessDashboard(Access $user): bool
  69.     {
  70.         return true;
  71.     }
  72.     private function canAdd(Access $user): bool
  73.     {
  74.         return true;
  75.     }
  76.     private function canEdit(Access $userAccess $subject): bool
  77.     {
  78.         return true;
  79.     }
  80.     private function canDelete(Access $userAccess $subject): bool
  81.     {
  82.         return true;
  83.     }
  84. }