src/Http/Admin/Voter/User/UserVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Http\Admin\Voter\User;
  3. use App\Domain\User\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class UserVoter extends Voter
  7. {
  8.     final public const EDIT_USER 'admin_user_edit';
  9.     final public const BAN_USER 'admin_user_ban';
  10.     final public const UNBAN_USER 'admin_user_unban';
  11.     final public const DELETE_USER 'admin_user_delete';
  12.     protected function supports(string $attributemixed $subject): bool
  13.     {
  14.         return match ($attribute) {
  15.             self::EDIT_USERself::BAN_USERself::UNBAN_USERself::DELETE_USER => true,
  16.             default => false,
  17.         };
  18.     }
  19.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  20.     {
  21.         /** @var ?User $user */
  22.         $user $token->getUser();
  23.         if (is_null($user)) {
  24.             return false;
  25.         }
  26.         /* @var User $subject */
  27.         return match ($attribute) {
  28.             self::EDIT_USERself::DELETE_USER => !$subject->isDeleted(),
  29.             self::BAN_USER => !$subject->isBanned(),
  30.             self::UNBAN_USER => $subject->isBanned(),
  31.             default => true,
  32.         };
  33.     }
  34. }