src/System/Authorization/Voter/AuthorizationVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\System\Authorization\Voter;
  4. use App\Entities\User\Type\TypeEnum;
  5. use App\Entities\User\UserAccess;
  6. use App\Entities\User\UserInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class AuthorizationVoter extends Voter
  10. {
  11.     protected function supports(string $attributemixed $subject): bool
  12.     {
  13.         return in_array($attribute, [TypeEnum::Admin->valueTypeEnum::User->value], true);
  14.     }
  15.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  16.     {
  17.         $user $token->getUser();
  18.         if (!$user instanceof UserInterface) {
  19.             return false;
  20.         }
  21.         return match ($attribute) {
  22.             TypeEnum::Admin->value => $user->isAdmin(),
  23.             TypeEnum::User->value => $user->isAdmin() || $user instanceof UserAccess,
  24.         };
  25.     }
  26. }