src/Security/Voter/UserGroupVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Eg\Admin\Role;
  4. use App\Entity\Eg\Admin\UserGroup;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. /**
  9.  * Dostęp do grupy uprawnień, z wyjątkiem grup systemowych.
  10.  * Grupa systemowa (SYSTEM_FULL_ADMIN) ma być niewidoczna dla administratorów bez pełnego dostępu
  11.  *
  12.  */
  13. final class UserGroupVoter extends Voter
  14. {
  15.     public const VIEW = 'EG_GROUP_VIEW';
  16.     public const ATTRIBUTES = [self::VIEW];
  17.     public function __construct(private readonly Security $security)
  18.     {
  19.     }
  20.     protected function supports(string $attribute, $subject): bool
  21.     {
  22.         return $attribute === self::VIEW;
  23.     }
  24.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  25.     {
  26.         if ($subject instanceof UserGroup
  27.             && $subject->isSystem()
  28.             && !$this->security->isGranted(Role::ROLE_SUPER_ADMIN)
  29.         ) {
  30.             return false;
  31.         }
  32.         return $this->security->isGranted(Role::ROLE_R_GROUPS_VIEW);
  33.     }
  34. }