src/V4/Voters/CustomActionVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\V4\Voters;
  3. use App\Security\User;
  4. use App\V4\Entity\CustomAction;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class CustomActionVoter extends Voter
  9. {
  10.     const CUSTOM_ACTION_SHOW_LIST 'custom_action_show_list';
  11.     const CUSTOM_ACTION_ADD_EDIT 'custom_action_add_edit';
  12.     const CUSTOM_ACTION_SHOW 'custom_action_show';
  13.     /**
  14.      * @param $attribute
  15.      * @param $subject
  16.      *
  17.      * @return bool
  18.      */
  19.     protected function supports($attribute$subject): bool
  20.     {
  21.         if ($subject instanceof CustomAction && in_array($attribute, [
  22.                 self::CUSTOM_ACTION_SHOW,
  23.             ], true)) {
  24.             return true;
  25.         }
  26.         return in_array($attribute, [
  27.             self::CUSTOM_ACTION_SHOW_LIST,
  28.             self::CUSTOM_ACTION_ADD_EDIT,
  29.         ], true);
  30.     }
  31.     /**
  32.      * @param $attribute
  33.      * @param $subject
  34.      * @param TokenInterface $token
  35.      *
  36.      * @return bool
  37.      *
  38.      * @throws LogicException
  39.      */
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  41.     {
  42.         $user $token->getUser();
  43.         if (!$user instanceof User) {
  44.             return false;
  45.         }
  46.         switch ($attribute) {
  47.             case self::CUSTOM_ACTION_SHOW_LIST:
  48.                 return $this->canShowListCustomAction($user);
  49.             case self::CUSTOM_ACTION_ADD_EDIT:
  50.                 return $this->canAddEditCustomAction($user);
  51.             case self::CUSTOM_ACTION_SHOW:
  52.                 return $this->canAccessCustomAction($user);
  53.         }
  54.         throw new LogicException('This should never happen');
  55.     }
  56.     /**
  57.      * @param User $user
  58.      *
  59.      * @return bool
  60.      */
  61.     private function canShowListCustomAction(User $user): bool
  62.     {
  63.         return $user->isSuperAdmin();
  64.     }
  65.     /**
  66.      * @param User $user
  67.      *
  68.      * @return bool
  69.      */
  70.     private function canAddEditCustomAction(User $user): bool
  71.     {
  72.         return $user->isSuperAdmin();
  73.     }
  74.     /**
  75.      * @param User $user
  76.      *
  77.      * @return bool
  78.      */
  79.     private function canAccessCustomAction(User $user): bool
  80.     {
  81.         return $user->isSuperAdmin();
  82.     }
  83. }