src/Security/Voter/InsightsVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use MedBrief\MSR\Entity\Account;
  4. use MedBrief\MSR\Entity\Project;
  5. use MedBrief\MSR\Entity\User;
  6. use Override;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class InsightsVoter extends Voter
  12. {
  13. public const VIEW_RECORDS_INSIGHTS = 'VIEW_RECORDS_INSIGHTS';
  14. public const EDIT_RECORDS_INSIGHTS = 'EDIT_RECORDS_INSIGHTS';
  15. public function __construct(private readonly AuthorizationCheckerInterface $auth)
  16. {
  17. }
  18. #[Override]
  19. protected function supports(string $attribute, mixed $subject): bool
  20. {
  21. return in_array($attribute, [self::VIEW_RECORDS_INSIGHTS, self::EDIT_RECORDS_INSIGHTS], true)
  22. && $subject instanceof Project;
  23. }
  24. #[Override]
  25. protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  26. {
  27. $user = $token->getUser();
  28. if (!$user instanceof UserInterface) {
  29. return false;
  30. }
  31. /** @var Project $project */
  32. $project = $subject;
  33. return match ($attribute) {
  34. self::VIEW_RECORDS_INSIGHTS => $this->canViewRecordsInsights($user, $project),
  35. self::EDIT_RECORDS_INSIGHTS => $this->canEditRecordsInsights($user, $project),
  36. default => false,
  37. };
  38. }
  39. /**
  40. * Determines whether the user may edit or delete AI-generated Key Events.
  41. *
  42. * Editing requires the existing Insights viewing eligibility plus an editor role. Experts
  43. * retain view access but are deliberately excluded from the editor-role check.
  44. *
  45. * @param UserInterface $user
  46. * @param Project $project
  47. */
  48. private function canEditRecordsInsights(UserInterface $user, Project $project): bool
  49. {
  50. if (!$this->canViewRecordsInsights($user, $project)) {
  51. return false;
  52. }
  53. // MedBrief admins always have editor access when the project is an eligible matter.
  54. if ($this->auth->isGranted('ROLE_SUPER_ADMIN') || $this->auth->isGranted('ROLE_ADMIN')) {
  55. return true;
  56. }
  57. $account = $project->getAccount();
  58. return $account instanceof Account && $this->hasEligibleInsightsEditorRole($user, $project, $account);
  59. }
  60. /**
  61. * Determines if the user can view the insights button and panel.
  62. * User roles allowed:
  63. * - MB super admins (ROLE_SUPER_ADMIN)
  64. * - MB admins (ROLE_ADMIN)
  65. * - Client super admins (ROLE_ACCOUNT_{accountId}_SUPERADMINISTRATOR)
  66. * - Client admins (ROLE_ACCOUNT_{accountId}_ADMINISTRATOR)
  67. * - Client project managers (ROLE_ACCOUNT_{accountId}_PROJECTMANAGER)
  68. * - Matter experts (ROLE_PROJECT_{projectId}_EXPERT)
  69. * - Matter expert viewers (ROLE_PROJECT_{projectId}_EXPERTVIEWER)
  70. * - Matter/Project managers:
  71. * - Role-based: ROLE_PROJECT_{projectId}_PROJECTMANAGER
  72. * - Entity-based: $project->getManager() === $user
  73. *
  74. * @param UserInterface $user
  75. * @param Project $project
  76. *
  77. * @return bool Returns true if the user can view records insights, false otherwise
  78. */
  79. private function canViewRecordsInsights(UserInterface $user, Project $project): bool
  80. {
  81. $account = $project->getAccount();
  82. // Only consider Client matters (no Disclosures or other non-matter projects)
  83. if ($project->getType() !== Project::TYPE_MATTER_FIRM) {
  84. return false;
  85. }
  86. // MB admins always have access to insights, regardless of account settings or user opt-in
  87. if ($this->auth->isGranted('ROLE_SUPER_ADMIN') || $this->auth->isGranted('ROLE_ADMIN')) {
  88. return true;
  89. }
  90. // If insights is not enabled at the account level, then only MB admins can access insights
  91. if (!$account instanceof Account || !$account->isInsightsEnabled()) {
  92. return false;
  93. }
  94. // For client users, check if they have insights access based on account settings
  95. if (!$this->hasUserInsightsAccess($account, $user)) {
  96. return false;
  97. }
  98. // For client users, they must have an eligible role and insights must be enabled for the account, then we check opt-in settings
  99. if (!$this->hasEligibleInsightsRole($user, $project, $account)) {
  100. return false;
  101. }
  102. // At this point, the project is eligible for Insights and the client user has
  103. // both an allowed role and the required opt-in access, so access is granted.
  104. return true;
  105. }
  106. /**
  107. * Checks if the user has an eligible role for insights access based on their relationship to the project and account.
  108. *
  109. * @param UserInterface $user The user for whom we are checking roles
  110. * @param Project $project The project associated with the insights access
  111. * @param Account $account The account associated with the project
  112. *
  113. * @return bool Returns true if the user has an eligible role for insights access, false otherwise
  114. */
  115. private function hasEligibleInsightsRole(UserInterface $user, Project $project, Account $account): bool
  116. {
  117. if ($this->hasEligibleInsightsEditorRole($user, $project, $account)) {
  118. return true;
  119. }
  120. // Experts and expert viewers may view Insights, but cannot edit Key Events.
  121. return $this->auth->isGranted('ROLE_PROJECT_' . $project->getId() . '_EXPERT')
  122. || $this->auth->isGranted('ROLE_PROJECT_' . $project->getId() . '_EXPERTVIEWER');
  123. }
  124. /**
  125. * Checks for an account- or matter-level administrator/manager role.
  126. *
  127. * @param UserInterface $user
  128. * @param Project $project
  129. * @param Account $account
  130. */
  131. private function hasEligibleInsightsEditorRole(UserInterface $user, Project $project, Account $account): bool
  132. {
  133. // Client-level admins
  134. if ($this->auth->isGranted('ROLE_ACCOUNT_' . $account->getId() . '_SUPERADMINISTRATOR')) {
  135. return true;
  136. }
  137. if ($this->auth->isGranted('ROLE_ACCOUNT_' . $account->getId() . '_ADMINISTRATOR')) {
  138. return true;
  139. }
  140. if ($this->auth->isGranted('ROLE_ACCOUNT_' . $account->getId() . '_PROJECTMANAGER')) {
  141. return true;
  142. }
  143. // Matter/Project manager (role-based)
  144. if ($this->auth->isGranted('ROLE_PROJECT_' . $project->getId() . '_PROJECTMANAGER')) {
  145. return true;
  146. }
  147. // Matter/Project manager (entity relationship fallback)
  148. if ($project->getManager() instanceof User && $project->getManager() === $user) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * Checks if the user has access to insights based on account level and user level opt-in settings.
  155. *
  156. * @param Account $account The account associated with the project for which we are checking access
  157. * @param UserInterface $user The user for whom we are checking access
  158. *
  159. * @return bool Returns true if the user has access to insights, false otherwise
  160. */
  161. private function hasUserInsightsAccess(Account $account, UserInterface $user): bool
  162. {
  163. // If insights is enabled for all client users, then any user with an eligible role
  164. // can access,otherwise we check the user's individual opt-in setting
  165. if ($account->isInsightsOptInAllClientUsers()) {
  166. return true;
  167. }
  168. // If insights is only enabled for specific users, then we check the user's opt-in setting
  169. if ($account->isInsightsOptInSpecificUsers() && $user instanceof User) {
  170. return $user->getInsightsOptIn();
  171. }
  172. return false;
  173. }
  174. }