<?php
namespace App\Security\Voter;
use App\Entity\Eg\Admin\Role;
use App\Entity\Eg\Admin\UserGroup;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
/**
* Dostęp do grupy uprawnień, z wyjątkiem grup systemowych.
* Grupa systemowa (SYSTEM_FULL_ADMIN) ma być niewidoczna dla administratorów bez pełnego dostępu
*
*/
final class UserGroupVoter extends Voter
{
public const VIEW = 'EG_GROUP_VIEW';
public const ATTRIBUTES = [self::VIEW];
public function __construct(private readonly Security $security)
{
}
protected function supports(string $attribute, $subject): bool
{
return $attribute === self::VIEW;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if ($subject instanceof UserGroup
&& $subject->isSystem()
&& !$this->security->isGranted(Role::ROLE_SUPER_ADMIN)
) {
return false;
}
return $this->security->isGranted(Role::ROLE_R_GROUPS_VIEW);
}
}