vendor/crea/security-bundle/src/Voter/UserGroupVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace Crea\SecurityBundle\Voter;
  3. use Crea\SecurityBundle\Entity\UserGroup;
  4. use Crea\SecurityBundle\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\User\UserInterface;
  8. class UserGroupVoter extends Voter
  9. {
  10.     const USER_GROUP_LIST "SECURITY_USER_GROUP_LIST";
  11.     const USER_GROUP_CREATE "SECURITY_USER_GROUP_CREATE";
  12.     const USER_GROUP_UPDATE "SECURITY_USER_GROUP_UPDATE";
  13.     const USER_GROUP_REMOVE "SECURITY_USER_GROUP_REMOVE";
  14.     /**
  15.      * @inheritDoc
  16.      */
  17.     protected function supports($attribute$subject): bool
  18.     {
  19.         return in_array($attribute, [
  20.                 self::USER_GROUP_LIST,
  21.                 self::USER_GROUP_CREATE,
  22.                 self::USER_GROUP_UPDATE,
  23.                 self::USER_GROUP_REMOVE,
  24.             ]) && ($subject === null || $subject instanceof UserGroup);
  25.     }
  26.     /**
  27.      * @inheritDoc
  28.      */
  29.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  30.     {
  31.         /** @var User $loggedUser */
  32.         $loggedUser $token->getUser();
  33.         if (!$loggedUser instanceof UserInterface)
  34.             return false;
  35.         switch ($attribute) {
  36.             case self::USER_GROUP_LIST:
  37.                 return $this->voteOnList($loggedUser);
  38.             case self::USER_GROUP_CREATE:
  39.                 return $this->voteOnCreate($loggedUser);
  40.             case self::USER_GROUP_UPDATE:
  41.                 return $this->voteOnUpdate($subject$loggedUser);
  42.             case self::USER_GROUP_REMOVE:
  43.                 return $this->voteOnRemove($subject$loggedUser);
  44.         }
  45.         return false;
  46.     }
  47.     private function voteOnList(UserInterface $loggedUser): bool
  48.     {
  49.         if (in_array(self::USER_GROUP_LIST$loggedUser->getRoles())) {
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54.     private function voteOnCreate(UserInterface $loggedUser): bool
  55.     {
  56.         if (in_array(self::USER_GROUP_CREATE$loggedUser->getRoles())) {
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61.     private function voteOnUpdate(?UserGroup $subjectUser $loggedUser): bool
  62.     {
  63.         if (!in_array(self::USER_GROUP_UPDATE$loggedUser->getRoles()))
  64.             return false;
  65.         if (null === $subject)
  66.             return true;
  67.         if (in_array("ROLE_ADMIN"$loggedUser->getRoles()))
  68.             return true;
  69.         return false;
  70.     }
  71.     private function voteOnRemove(?UserGroup $subjectUser $loggedUser): bool
  72.     {
  73.         if (!in_array(self::USER_GROUP_REMOVE$loggedUser->getRoles()))
  74.             return false;
  75.         if (null === $subject)
  76.             return true;
  77.         if (in_array("ROLE_ADMIN"$loggedUser->getRoles()))
  78.             return true;
  79.         return false;
  80.     }
  81. }