src/FlexApp/Security/Voter/RoleSuperAdminVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace FlexApp\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. class RoleSuperAdminVoter extends _ExtendVoter
  6. {
  7.     protected array $methods = [];
  8.     protected function supports(string $attribute$subject): bool
  9.     {
  10.         //return $this->checkMethods($attribute);
  11.         // здесь отдаем стразу TRUE
  12.         return true;
  13.     }
  14.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  15.     {
  16.         // проверяем на роль ROLE_SUPER_ADMIN
  17.         $user $token->getUser();
  18.         if ($user instanceof UserInterface) {
  19.             // PREVIOUS_ADMIN проверяем отдельно от SUPER_ADMIN
  20.             if ($attribute == 'ROLE_PREVIOUS_ADMIN') {
  21.                 if (in_array('ROLE_PREVIOUS_ADMIN'$user->getRoles())) {
  22.                     return true;
  23.                 }
  24.             } else {
  25.                 if (in_array('ROLE_SUPER_ADMIN'$user->getRoles())) {
  26.                     return true;
  27.                 }
  28.             }
  29.         }
  30.         return false;
  31.     }
  32. }