src/Security/Voter/InterpartyDisclosureVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use MedBrief\MSR\Entity\InterpartyDisclosure;
  4. use MedBrief\MSR\Entity\Project;
  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 InterpartyDisclosureVoter extends Voter
  11. {
  12. public const UPDATE = 'UPDATE';
  13. public const VIEW = 'VIEW';
  14. public const CANCEL = 'CANCEL';
  15. public const REACTIVATE = 'REACTIVATE';
  16. public const PUSH = 'PUSH';
  17. public const FORCE_EXPIRY_DATE = 'FORCE_EXPIRY_DATE';
  18. public const VIEW_THIRD_PARTY_ACCESS_REPORT = 'VIEW_THIRD_PARTY_ACCESS_REPORT';
  19. public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker)
  20. {
  21. }
  22. #[Override]
  23. protected function supports($attribute, $subject): bool
  24. {
  25. return in_array($attribute, [
  26. self::UPDATE,
  27. self::VIEW,
  28. self::CANCEL,
  29. self::REACTIVATE,
  30. self::PUSH,
  31. self::FORCE_EXPIRY_DATE,
  32. self::VIEW_THIRD_PARTY_ACCESS_REPORT,
  33. ])
  34. && $subject instanceof InterpartyDisclosure;
  35. }
  36. #[Override]
  37. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  38. {
  39. $user = $token->getUser();
  40. // if the user is anonymous, do not grant access
  41. if (!$user instanceof UserInterface) {
  42. return false;
  43. }
  44. /** @var InterpartyDisclosure $interpartyDisclosure */
  45. $interpartyDisclosure = $subject;
  46. $project = $interpartyDisclosure->getProject();
  47. // ... (check conditions and return true to grant permission) ...
  48. return match ($attribute) {
  49. self::UPDATE => $this->canUpdate($user, $project),
  50. self::VIEW => $this->canView($user, $project),
  51. self::CANCEL => $this->canCancel($user, $project),
  52. self::REACTIVATE => $this->canReactivate($user, $project),
  53. self::PUSH => $this->canPush($user),
  54. self::FORCE_EXPIRY_DATE => $this->canForceExpiryDate($user),
  55. self::VIEW_THIRD_PARTY_ACCESS_REPORT => $this->canThirdPartyAccessView($user, $project),
  56. default => false,
  57. };
  58. }
  59. protected function canUpdate(UserInterface $user, Project $project): bool
  60. {
  61. // MEDBRIEF ADMIN
  62. if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  63. return true;
  64. }
  65. // CLIENT SUPER ADMIN
  66. if ($this->authorizationChecker->isGranted('ROLE_ACCOUNT_' . $project->getAccount()->getId() . '_SUPERADMINISTRATOR')) {
  67. return true;
  68. }
  69. // CLIENT ADMIN
  70. if ($this->authorizationChecker->isGranted('ROLE_ACCOUNT_' . $project->getAccount()->getId() . '_ADMINISTRATOR')) {
  71. return true;
  72. }
  73. // MATTER LEVEL PROJECT MANAGER
  74. if ($this->authorizationChecker->isGranted('ROLE_PROJECT_' . $project->getId() . '_PROJECTMANAGER')) {
  75. return true;
  76. }
  77. // MANAGER ON A PROJECT/MATTER
  78. return $project->getManager() && $project->getManager() === $user;
  79. }
  80. protected function canView(UserInterface $user, Project $project): bool
  81. {
  82. // If the user can update, then they can also view.
  83. return $this->canUpdate($user, $project);
  84. }
  85. /**
  86. * Grants permission to users to access the 'Third party disclosure access modal'
  87. *
  88. * @param UserInterface $user
  89. * @param Project $project
  90. */
  91. protected function canThirdPartyAccessView(UserInterface $user, Project $project): bool
  92. {
  93. // If the user can update, then they can also view third party access.
  94. return $this->canUpdate($user, $project);
  95. }
  96. protected function canCancel(UserInterface $user, Project $project): bool
  97. {
  98. // If the user can update, then they can also cancel.
  99. return $this->canUpdate($user, $project);
  100. }
  101. protected function canReactivate(UserInterface $user, Project $project): bool
  102. {
  103. // If the user can update, then they can also reactivate.
  104. return $this->canUpdate($user, $project);
  105. }
  106. protected function canPush(UserInterface $user): bool
  107. {
  108. // MEDBRIEF ADMIN
  109. return $this->authorizationChecker->isGranted('ROLE_ADMIN');
  110. }
  111. protected function canForceExpiryDate(UserInterface $user): bool
  112. {
  113. // MEDBRIEF SUPER ADMIN
  114. return $this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN');
  115. }
  116. }