src/Security/Voter/SystemSettingVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use MedBrief\MSR\Entity\SystemSetting;
  4. use MedBrief\MSR\Entity\User;
  5. use Override;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class SystemSettingVoter extends Voter
  11. {
  12. public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker)
  13. {
  14. }
  15. #[Override]
  16. protected function supports($attribute, $subject)
  17. {
  18. return in_array($attribute, [
  19. 'SYSTEM_SETTING_READ',
  20. 'SYSTEM_SETTING_UPDATE',
  21. ])
  22. && $subject instanceof SystemSetting;
  23. }
  24. #[Override]
  25. protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  26. {
  27. /** @var User $user */
  28. $user = $token->getUser();
  29. // if the user is anonymous, do not grant access
  30. if (!$user instanceof UserInterface) {
  31. return false;
  32. }
  33. // For now, just having the Technical Admin role is sufficient
  34. return $this->authorizationChecker->isGranted('ROLE_TECHNICAL_ADMIN');
  35. }
  36. }