src/Http/Admin/Security/AdminVoter.php line 10

  1. <?php
  2. namespace App\Http\Admin\Security;
  3. use App\Domain\Auth\Entity\Group;
  4. use App\Domain\Auth\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class AdminVoter extends Voter
  8. {
  9.     protected function supports(string $attribute$subject): bool
  10.     {
  11.         if($attribute == '') return false;
  12.         if(str_contains($attribute'ROLE_')) return false;
  13.         return true;
  14.     }
  15.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  16.     {
  17.         $user $token->getUser();
  18.         if (!$user instanceof User) {
  19.             return false;
  20.         }
  21.         if($user->hasRole('ROLE_ADMIN')){
  22.              if($user->getGroup()->getId() == Group::ADMIN_ID  && $subject != 'only') return true;
  23.             $rights $user->getGroup()->getModulesRightSlug();
  24.             foreach ($rights as $r) {
  25.                 if ($attribute == $r) return true;
  26.             }
  27.         }
  28.         return false;
  29.     }
  30. }