src/V4/Voters/ListingVoter.php line 10

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. class ListingVoter extends Voter
  8. {
  9.     const LISTING_MANAGE_LIST 'listing_manage_list';
  10.     /**
  11.      * @param $attribute
  12.      * @param $subject
  13.      *
  14.      * @return bool
  15.      */
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         return in_array($attribute, [
  19.             self::LISTING_MANAGE_LIST,
  20.         ], true);
  21.     }
  22.     /**
  23.      * @param $attribute
  24.      * @param $subject
  25.      * @param TokenInterface $token
  26.      *
  27.      * @return bool
  28.      *
  29.      * @throws LogicException
  30.      */
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  32.     {
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             return false;
  36.         }
  37.         switch ($attribute) {
  38.             case self::LISTING_MANAGE_LIST:
  39.                 return $this->canListingManageList($user);
  40.         }
  41.         throw new LogicException('This should never happen');
  42.     }
  43.     /**
  44.      * @param User $user
  45.      *
  46.      * @return bool
  47.      */
  48.     private function canListingManageList(User $user): bool
  49.     {
  50.         return $user->isAdmin();
  51.     }
  52. }