<?php
namespace MedBrief\MSR\Security\Voter;
use MedBrief\MSR\Entity\SystemSetting;
use MedBrief\MSR\Entity\User;
use Override;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class SystemSettingVoter extends Voter
{
public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker)
{
}
#[Override]
protected function supports($attribute, $subject)
{
return in_array($attribute, [
'SYSTEM_SETTING_READ',
'SYSTEM_SETTING_UPDATE',
])
&& $subject instanceof SystemSetting;
}
#[Override]
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var User $user */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// For now, just having the Technical Admin role is sufficient
return $this->authorizationChecker->isGranted('ROLE_TECHNICAL_ADMIN');
}
}