src/V4/Voters/UserInfoVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\V4\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. use Symfony\Component\Security\Core\Security;
  8. class UserInfoVoter extends Voter
  9. {
  10.     const USER_INFO_SHOW_LIST 'user_info_show_list';
  11.     /**
  12.      * @var Security
  13.      */
  14.     private $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     /**
  20.      * @param $attribute
  21.      * @param $subject
  22.      *
  23.      * @return bool
  24.      */
  25.     protected function supports($attribute$subject): bool
  26.     {
  27.         return in_array($attribute, [
  28.             self::USER_INFO_SHOW_LIST,
  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::USER_INFO_SHOW_LIST:
  48.                 return $this->canUserInfoShowList();
  49.         }
  50.         throw new LogicException('This should never happen');
  51.     }
  52.     /**
  53.      * @return bool
  54.      */
  55.     private function canUserInfoShowList(): bool
  56.     {
  57.         return true;
  58.     }
  59. }