src/Security/Voter/UserVoter.php line 12

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\User\User;
  5. use App\Helper\UserHelper;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class UserVoter extends Voter
  9. {
  10.     const CHANGE_ROLE 'CHANGE_ROLE';
  11.     protected function supports(string $attributemixed $subject): bool
  12.     {
  13.         if (!\in_array($attribute, [self::CHANGE_ROLE])) {
  14.             return false;
  15.         }
  16.         if (!$subject instanceof User) {
  17.             return false;
  18.         }
  19.         return true;
  20.     }
  21.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  22.     {
  23.         $user $token->getUser();
  24.         if (!$user instanceof User) {
  25.             return false;
  26.         }
  27.         return match ($attribute) {
  28.             self::CHANGE_ROLE => UserHelper::isAdmin($user),
  29.             default => throw new \LogicException('This code should not be reached!'),
  30.         };
  31.     }
  32. }