src/Voters/ModuleConfigurationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Security\User;
  4. use LogicException;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ModuleConfigurationVoter extends Voter
  8. {
  9.     const MODULE_CONFIGURATION_SHOW_LIST 'module_configuration_show_list';
  10.     const MODULE_CONFIGURATION_ADD_EDIT 'module_configuration_add_edit';
  11.     /**
  12.      * @param $attribute
  13.      * @param $subject
  14.      *
  15.      * @return bool
  16.      */
  17.     protected function supports($attribute$subject): bool
  18.     {
  19.         return in_array($attribute, [
  20.             self::MODULE_CONFIGURATION_SHOW_LIST,
  21.             self::MODULE_CONFIGURATION_ADD_EDIT,
  22.         ], true);
  23.     }
  24.     /**
  25.      * @param $attribute
  26.      * @param $subject
  27.      * @param TokenInterface $token
  28.      *
  29.      * @return bool
  30.      *
  31.      * @throws LogicException
  32.      */
  33.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  34.     {
  35.         $user $token->getUser();
  36.         if (!$user instanceof User) {
  37.             return false;
  38.         }
  39.         switch ($attribute) {
  40.             case self::MODULE_CONFIGURATION_SHOW_LIST:
  41.                 return $this->canModuleConfigurationShowList($user);
  42.             case self::MODULE_CONFIGURATION_ADD_EDIT:
  43.                 return $this->canModuleConfigurationAddEdit($user);
  44.         }
  45.         throw new LogicException('This should never happen');
  46.     }
  47.     /**
  48.      * @param User $user
  49.      *
  50.      * @return bool
  51.      */
  52.     private function canModuleConfigurationShowList(User $user): bool
  53.     {
  54.         return true;
  55.     }
  56.     /**
  57.      * @param User $user
  58.      *
  59.      * @return bool
  60.      */
  61.     private function canModuleConfigurationAddEdit(User $user): bool
  62.     {
  63.         return true;
  64.     }
  65. }