vendor/datenwerk/ginger-bundle/Security/UserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace DW\GingerBundle\Security;
  3. use DW\GingerBundle\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. class UserVoter extends Voter
  8. {
  9.     const VIEW 'view';
  10.     const EDIT 'edit';
  11.     const CREATE 'create';
  12.     const DELETE 'delete';
  13.     private AccessDecisionManagerInterface $decisionManager;
  14.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  15.     {
  16.         $this->decisionManager $decisionManager;
  17.     }
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         // if the attribute isn't one we support, return false
  21.         if (!in_array($attribute, array(self::VIEWself::EDITself::CREATEself::DELETE))) {
  22.             return false;
  23.         }
  24.         // only vote on user objects inside this voter
  25.         if (!$subject instanceof User) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  31.     {
  32.         $user $token->getUser();
  33.         if (!$user instanceof User) {
  34.             // the user must be logged in; if not, deny access
  35.             return false;
  36.         }
  37.         $user $subject;
  38.         switch ($attribute) {
  39.             case self::VIEW:
  40.                 return $this->canView($user$token);
  41.             case self::EDIT:
  42.                 return $this->canEdit($user$token);
  43.             case self::CREATE:
  44.                 return $this->canCreate($user$token);
  45.             case self::DELETE:
  46.                 return $this->canDelete($user$token);
  47.         }
  48.         throw new \LogicException('This code should not be reached!');
  49.     }
  50.     private function canCreate(User $userTokenInterface $token): bool
  51.     {
  52.         if ($this->decisionManager->decide($token, ['ROLE_GINGER_USER_CREATE'])) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.     private function canView(User $userTokenInterface $token): bool
  58.     {
  59.         // if they can edit, they can view
  60.         if ($this->canEdit($tag$token)) {
  61.             return true;
  62.         }
  63.         return false;
  64.     }
  65.     private function canEdit(User $userTokenInterface $token): bool
  66.     {
  67.         if ($this->decisionManager->decide($token, ['ROLE_GINGER_USER_EDIT'])) {
  68.             return true;
  69.         }
  70.         return false;
  71.     }
  72.     private function canDelete(User $userTokenInterface $token): bool
  73.     {
  74.         if ($this->decisionManager->decide($token, ['ROLE_GINGER_USER_DELETE'])) {
  75.             return true;
  76.         }
  77.         return false;
  78.     }
  79. }