vendor/datenwerk/ginger-bundle/Security/GroupVoter.php line 11

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