src/Security/Voter/TrustedDeviceAdminVoter.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MedBrief\MSR\Security\Voter;
  4. use MedBrief\MSR\Entity\User;
  5. use MedBrief\MSR\Service\TrustedDevice\BrowserSession;
  6. use MedBrief\MSR\Service\TrustedDevice\Enforcement;
  7. use Override;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. final class TrustedDeviceAdminVoter extends Voter
  13. {
  14. /** Permission to view another account’s device security settings. */
  15. public const string VIEW = 'TRUSTED_DEVICE_ADMIN_VIEW';
  16. /** Permission to authorise Support recovery for another account. */
  17. public const string RECOVER = 'TRUSTED_DEVICE_ADMIN_RECOVER';
  18. /** Permission to change another account’s device requirement. */
  19. public const string ENFORCE = 'TRUSTED_DEVICE_ADMIN_ENFORCE';
  20. /** Permission to delink another account’s devices. */
  21. public const string REVOKE = 'TRUSTED_DEVICE_ADMIN_REVOKE';
  22. /** @param list<string> $securityAdministrators @param list<string> $recoveryOperators */
  23. public function __construct(
  24. private readonly RequestStack $requests,
  25. private readonly BrowserSession $browserSession,
  26. private readonly array $securityAdministrators,
  27. private readonly array $recoveryOperators,
  28. private readonly Enforcement $enforcement,
  29. ) {
  30. }
  31. /** Recognise only Trusted Device administration permissions for user subjects. */
  32. #[Override]
  33. protected function supports(string $attribute, mixed $subject): bool
  34. {
  35. return $subject instanceof User && in_array($attribute, [self::VIEW, self::RECOVER, self::ENFORCE, self::REVOKE], true);
  36. }
  37. /** Check the action's operator role, target restrictions and recent authentication. */
  38. #[Override]
  39. protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  40. {
  41. $actor = $token->getUser();
  42. if (!$this->enforcement->isFeatureEnabled() || !$actor instanceof User || $token instanceof SwitchUserToken || $actor->getId() === $subject->getId()
  43. || !$actor->isEnabled() || !$actor->isAccountNonLocked()
  44. ) {
  45. return false;
  46. }
  47. $id = (string) $actor->getId();
  48. $security = in_array('ROLE_SUPER_ADMIN', $token->getRoleNames(), true)
  49. && in_array($id, $this->securityAdministrators, true);
  50. $recovery = array_intersect(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'], $token->getRoleNames()) !== []
  51. && in_array($id, $this->recoveryOperators, true);
  52. if ($attribute === self::VIEW) {
  53. return $security || $recovery;
  54. }
  55. $request = $this->requests->getMainRequest();
  56. if ($request === null || !$request->hasSession()
  57. || !$this->browserSession->hasRecentAssurance($request->getSession(), $actor, time())
  58. ) {
  59. return false;
  60. }
  61. return match ($attribute) {
  62. self::RECOVER => $recovery,
  63. self::ENFORCE, self::REVOKE => $security,
  64. default => false,
  65. };
  66. }
  67. }