src/Security/Voter/AttributeOnlyVoter.php line 25

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use InvalidArgumentException;
  4. use MedBrief\MSR\Entity\Account;
  5. use MedBrief\MSR\Entity\Expert;
  6. use MedBrief\MSR\Entity\Firm;
  7. use MedBrief\MSR\Entity\Project;
  8. use MedBrief\MSR\Entity\User;
  9. use MedBrief\MSR\Repository\ExpertUserRepository;
  10. use MedBrief\MSR\Role\ParsedRole;
  11. use MedBrief\MSR\Service\EntityHelper\UserHelper;
  12. use Override;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. /**
  18. * This Voter handles the voting on checks that don't involve a specific
  19. * entity. Things like checking if a user can do some task like add a new
  20. * Account, or Project etc
  21. */
  22. class AttributeOnlyVoter implements VoterInterface
  23. {
  24. // these should be expanded over time as we need them
  25. public const CREATE_ACCOUNT = 'CREATE_ACCOUNT';
  26. public const CREATE_PROJECT = 'CREATE_PROJECT';
  27. public const CREATE_SERVICE_OPTION = 'CREATE_SERVICE_OPTION';
  28. public const CREATE_BILLING_ITEM = 'CREATE_BILLING_ITEM';
  29. public const CREATE_INVOICE = 'CREATE_INVOICE';
  30. public const CREATE_SORTING_MEMO_PHRASE = 'CREATE_SORTING_MEMO_PHRASE';
  31. public const CREATE_MATTER_REQUEST = 'CREATE_MATTER_REQUEST';
  32. public const CREATE_RECORDS_REQUEST_CENTRE_PARENT = 'CREATE_RECORDS_REQUEST_CENTRE_PARENT';
  33. public const CREATE_RECORDS_REQUEST_CENTRE_CHILD = 'CREATE_RECORDS_REQUEST_CENTRE_CHILD';
  34. public const CREATE_RECORDS_REQUEST_CENTRE_CONTACT = 'CREATE_RECORDS_REQUEST_CENTRE_CONTACT';
  35. public const CREATE_RECORDS_REQUEST_LETTER_TEMPLATE = 'CREATE_RECORDS_REQUEST_LETTER_TEMPLATE';
  36. public const CREATE_FIRM = 'CREATE_FIRM';
  37. // Manage records request centre contact
  38. public const MANAGE_RECORDS_REQUEST_CENTRE_CONTACT = 'MANAGE_RECORDS_REQUEST_CENTRE_CONTACT';
  39. public const LIST_ACCOUNT = 'LIST_ACCOUNT';
  40. public const LIST_PROJECT = 'LIST_PROJECT';
  41. public const LIST_PROJECT_CLOSED = 'LIST_PROJECT_CLOSED';
  42. public const LIST_USER = 'LIST_USER';
  43. public const LIST_EXPERT = 'LIST_EXPERT';
  44. public const LIST_SERVICE_OPTION = 'LIST_SERVICE_OPTION';
  45. public const LIST_BILLING_ITEM = 'LIST_BILLING_ITEM';
  46. public const LIST_PENDING_INVOICE = 'LIST_PENDING_INVOICE';
  47. public const LIST_INVOICE = 'LIST_INVOICE';
  48. public const LIST_SORTING_MEMO_PHRASE = 'LIST_SORTING_MEMO_PHRASE';
  49. public const LIST_MATTER_REQUEST = 'LIST_MATTER_REQUEST';
  50. public const LIST_RECORDS_REQUEST_CENTRE_PARENT = 'LIST_RECORDS_REQUEST_CENTRE_PARENT';
  51. public const LIST_RECORDS_REQUEST_CENTRE_CHILD = 'LIST_RECORDS_REQUEST_CENTRE_CHILD';
  52. public const LIST_RECORDS_REQUEST_CENTRE_ARCHIVED = 'LIST_RECORDS_REQUEST_CENTRE_ARCHIVED';
  53. public const LIST_RECORDS_REQUEST_LETTER_TEMPLATE = 'LIST_RECORDS_REQUEST_LETTER_TEMPLATE';
  54. public const LIST_FIRM = 'LIST_FIRM';
  55. public const LIST_PROJECT_TYPE = 'LIST_PROJECT_TYPE';
  56. public const LIST_PROJECT_RENEWALS = 'LIST_PROJECT_RENEWALS';
  57. public const LIST_MATCH_LETTER = 'LIST_MATCH_LETTER';
  58. public const LIST_MATCH_LETTER_RESPONSE = 'LIST_MATCH_LETTER_RESPONSE';
  59. public const USER_ADMINISTRATION = 'USER_ADMINISTRATION'; // indicates full user administration rights (CRUD)
  60. public const HUMAN_RESOURCE_ADMINISTRATION = 'HUMAN_RESOURCE_ADMINISTRATION'; // indicates full human resource administration rights (CRUD)
  61. public const USER_HELP_ADMINISTRATION = 'USER_HELP_ADMINISTRATION'; // indicates user Help Guide administration rights (CRUD)
  62. public const PRIORITISE_DISCS = 'PRIORITISE_DISCS';
  63. // Allow specific Users to mark stuffs as billed
  64. public const MARK_AS_BILLED = 'MARK_AS_BILLED';
  65. // Grants access to the 'Advanced' fields of all ServiceableInterface entities.
  66. public const SERVICE_REQUEST_ADVANCED = 'SERVICE_REQUEST_ADVANCED';
  67. public const ALLOW_USER_TYPE_CHANGE = 'ALLOW_USER_TYPE_CHANGE';
  68. // Migrated from ReportsBundle
  69. public const CREATE_REPORT = 'CREATE_REPORT';
  70. public const LIST_REPORT = 'LIST_REPORT';
  71. public const DOWNLOAD_REPORT = 'DOWNLOAD_REPORT';
  72. // Download Lists
  73. public const DOWNLOAD_PROJECT_LIST = 'DOWNLOAD_PROJECT_LIST';
  74. // User profile access
  75. public const VIEW_MY_PROFILE = 'VIEW_MY_PROFILE';
  76. public const VIEW_ACCOUNT_EXPERT_PROFILE = 'VIEW_ACCOUNT_EXPERT_PROFILE';
  77. public const ADMIN_VIEW_EXPERT = 'ADMIN_VIEW_EXPERT';
  78. public function __construct(
  79. private readonly AuthorizationCheckerInterface $authorizationChecker,
  80. private readonly UserHelper $userHelper,
  81. private readonly ExpertUserRepository $expertUserRepository,
  82. ) {
  83. }
  84. public function supportsAttribute($attribute): bool
  85. {
  86. return in_array($attribute, [
  87. self::CREATE_ACCOUNT,
  88. self::CREATE_PROJECT,
  89. self::LIST_ACCOUNT,
  90. self::LIST_PROJECT,
  91. self::LIST_USER,
  92. self::LIST_EXPERT,
  93. self::USER_ADMINISTRATION,
  94. self::USER_HELP_ADMINISTRATION,
  95. self::LIST_PROJECT_CLOSED,
  96. self::PRIORITISE_DISCS,
  97. self::LIST_SERVICE_OPTION,
  98. self::CREATE_SERVICE_OPTION,
  99. self::LIST_BILLING_ITEM,
  100. self::CREATE_BILLING_ITEM,
  101. self::CREATE_INVOICE,
  102. self::LIST_PENDING_INVOICE,
  103. self::LIST_INVOICE,
  104. self::HUMAN_RESOURCE_ADMINISTRATION,
  105. self::SERVICE_REQUEST_ADVANCED,
  106. self::MARK_AS_BILLED,
  107. self::CREATE_SORTING_MEMO_PHRASE,
  108. self::LIST_SORTING_MEMO_PHRASE,
  109. self::LIST_MATTER_REQUEST,
  110. self::LIST_MATCH_LETTER,
  111. self::LIST_MATCH_LETTER_RESPONSE,
  112. self::CREATE_MATTER_REQUEST,
  113. self::CREATE_RECORDS_REQUEST_CENTRE_PARENT,
  114. self::LIST_RECORDS_REQUEST_CENTRE_PARENT,
  115. self::CREATE_RECORDS_REQUEST_CENTRE_CHILD,
  116. self::LIST_RECORDS_REQUEST_CENTRE_CHILD,
  117. self::CREATE_RECORDS_REQUEST_CENTRE_CONTACT,
  118. self::MANAGE_RECORDS_REQUEST_CENTRE_CONTACT,
  119. self::LIST_RECORDS_REQUEST_CENTRE_ARCHIVED,
  120. self::CREATE_RECORDS_REQUEST_LETTER_TEMPLATE,
  121. self::LIST_RECORDS_REQUEST_LETTER_TEMPLATE,
  122. self::ALLOW_USER_TYPE_CHANGE,
  123. self::CREATE_REPORT,
  124. self::LIST_REPORT,
  125. self::DOWNLOAD_REPORT,
  126. self::DOWNLOAD_PROJECT_LIST,
  127. self::LIST_FIRM,
  128. self::CREATE_FIRM,
  129. self::LIST_PROJECT_TYPE,
  130. self::LIST_PROJECT_RENEWALS,
  131. self::VIEW_MY_PROFILE,
  132. self::VIEW_ACCOUNT_EXPERT_PROFILE,
  133. self::ADMIN_VIEW_EXPERT,
  134. 'EA_EXECUTE_ACTION',
  135. ]);
  136. }
  137. public function supportsClass($class): bool
  138. {
  139. // the class is inconsequential for this Voter
  140. return true;
  141. }
  142. /**
  143. *
  144. * @param mixed $entity
  145. */
  146. #[Override]
  147. public function vote(TokenInterface $token, $entity, array $attributes)
  148. {
  149. /**
  150. * START: This is common code for all Voter::vote() methods
  151. */
  152. // check if class of this object is supported by this voter
  153. if (!$this->supportsClass($entity && !is_array($entity) ? $entity::class : '')) {
  154. return VoterInterface::ACCESS_ABSTAIN;
  155. }
  156. // set the attribute to check against
  157. $attribute = $attributes[0];
  158. // check if the given attribute is covered by this voter
  159. if (!$this->supportsAttribute($attribute)) {
  160. return VoterInterface::ACCESS_ABSTAIN;
  161. }
  162. // check if the voter is used correct, only allow one attribute
  163. // this isn't a requirement, it's just one easy way for you to
  164. // design your voter
  165. if (1 !== count($attributes)) {
  166. throw new InvalidArgumentException(
  167. 'Only one attribute is allowed for medbrief Voters.'
  168. );
  169. }
  170. // get current logged in user
  171. /** @var User $user */
  172. $user = $token->getUser();
  173. // make sure there is a user object (i.e. that the user is logged in)
  174. if (!$user instanceof UserInterface) {
  175. return VoterInterface::ACCESS_DENIED;
  176. }
  177. // Check if the user can view the 'My Profile' section
  178. if ($attribute === self::VIEW_MY_PROFILE) {
  179. return $this->canViewMyProfile($user);
  180. }
  181. // Check if the user can see their reviewed Expert profile in the account section
  182. if ($attribute === self::VIEW_ACCOUNT_EXPERT_PROFILE) {
  183. return $this->canViewAccountExpertProfile($user);
  184. }
  185. // Universally disable this for now, as we will launch without the ability to add new ones.
  186. if ($attribute === self::CREATE_SORTING_MEMO_PHRASE && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
  187. return VoterInterface::ACCESS_DENIED;
  188. }
  189. // Only ROLE_ADMIN and ROLE_SUPER_ADMIN can PRIORITISE_DISCS at this stage.
  190. if ($attribute === self::PRIORITISE_DISCS && $this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  191. return VoterInterface::ACCESS_GRANTED;
  192. }
  193. // Only ROLE_HR_ADMIN can HUMAN_RESOURCE_ADMINISTRATION at this stage.
  194. // The ROLE_HR_ADMIN role can only be added via a command.
  195. if ($attribute === self::HUMAN_RESOURCE_ADMINISTRATION && (!$this->authorizationChecker->isGranted('ROLE_HR_ADMIN') || $this->authorizationChecker->isGranted('IS_IMPERSONATOR'))) {
  196. return VoterInterface::ACCESS_DENIED;
  197. }
  198. // Only SUPER_ADMIN can CREATE_SERVICE_OPTION at this stage.
  199. if ($attribute === self::CREATE_SERVICE_OPTION && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
  200. return VoterInterface::ACCESS_DENIED;
  201. }
  202. // Only SUPER_ADMIN can CREATE_BILLING_ITEM at this stage.
  203. if ($attribute === self::CREATE_BILLING_ITEM && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
  204. return VoterInterface::ACCESS_DENIED;
  205. }
  206. // Only SUPER_ADMIN can CREATE_RECORDS_REQUEST_LETTER_TEMPLATE.
  207. if ($attribute === self::CREATE_RECORDS_REQUEST_LETTER_TEMPLATE && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
  208. return VoterInterface::ACCESS_DENIED;
  209. }
  210. // Only SUPER_ADMIN can LIST_RECORDS_REQUEST_LETTER_TEMPLATE.
  211. if ($attribute === self::LIST_RECORDS_REQUEST_LETTER_TEMPLATE && !$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
  212. return VoterInterface::ACCESS_DENIED;
  213. }
  214. // Only Super Admins and those with isBillingAdmin can mark ServiceRequests as billed.
  215. if ($attribute === self::MARK_AS_BILLED) {
  216. if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') || $user->isBillingAdmin()) {
  217. return VoterInterface::ACCESS_GRANTED;
  218. }
  219. return VoterInterface::ACCESS_DENIED;
  220. }
  221. // Only Help Guide Admins can create, edit or delete.
  222. if ($attribute === self::USER_HELP_ADMINISTRATION) {
  223. if ($this->authorizationChecker->isGranted('ROLE_HELP_ADMIN')) {
  224. return VoterInterface::ACCESS_GRANTED;
  225. }
  226. return VoterInterface::ACCESS_DENIED;
  227. }
  228. // Super Admin users can do everything
  229. if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  230. return VoterInterface::ACCESS_GRANTED;
  231. }
  232. /**
  233. * END: Common code for all Voter:vote() methods. Put custom logic below.
  234. */
  235. $this->userHelper->setUser($user);
  236. // if we are looking if the user may create a matter ('Classic' matter creation process)
  237. // if the user is a client administrator for at least one account
  238. if ($attribute == self::CREATE_PROJECT && ($user->isAccountAdministrator() || $user->isAccountProjectManager())) {
  239. // Grab all accounts that the user has a role on
  240. $accountsWithRole = $this->userHelper->getAccountsWithAnyRole();
  241. foreach ($accountsWithRole as $account) {
  242. if ($account->hasAccessToClassicMatterCreation()) {
  243. // Look for at least one account that has access to the 'Classic' matter creation form
  244. return VoterInterface::ACCESS_GRANTED;
  245. }
  246. }
  247. }
  248. // If the User is a Super Admin, Admin, Client SuperAdmin, Client Admin, Client PM, Matter PM
  249. if ($attribute == self::LIST_PROJECT_RENEWALS && ($user->isAccountAdministrator() || $user->isAccountProjectManager() || $user->isProjectManager())) {
  250. return VoterInterface::ACCESS_GRANTED;
  251. }
  252. // if we are looking if the user may create a matter ('Standard' matter creation process)
  253. if ($attribute === self::CREATE_MATTER_REQUEST || $attribute === self::LIST_MATTER_REQUEST) {
  254. if ($user instanceof Firm) {
  255. // If a Firm is authenticated, they by default have access to create/list matter requests
  256. return VoterInterface::ACCESS_GRANTED;
  257. }
  258. // if the user is a client administrator for at least one account
  259. if ($user->isAccountAdministrator() || $user->isAccountProjectManager() || $user->isAccountTechnicalAdmin()) {
  260. // Grab all accounts that the user has a role on
  261. $accountsWithRole = $this->userHelper->getAccountsWithAnyRole();
  262. foreach ($accountsWithRole as $account) {
  263. if ($account->hasAccessToStandardMatterCreation()) {
  264. // Look for at least one account that has access to the 'Standard' matter creation form
  265. return VoterInterface::ACCESS_GRANTED;
  266. }
  267. }
  268. }
  269. }
  270. // if we are looking if the user may view a list of accounts
  271. if ($attribute == self::LIST_ACCOUNT) {
  272. // if the user is a client administrator for at least one account,
  273. // then they may se a list of Accounts
  274. if ($user->isAccountAdministrator()) {
  275. return VoterInterface::ACCESS_GRANTED;
  276. }
  277. if ($user->isAccountTechnicalAdmin()) {
  278. return VoterInterface::ACCESS_GRANTED;
  279. }
  280. }
  281. //if we are looking if the user may view the 'Type' search filter in the matter dashboard, matter list, closed matters list
  282. if ($attribute == self::LIST_PROJECT_TYPE) {
  283. if ($user->isAccountAdministrator() && !$user->isAccountTechnicalAdmin()) {
  284. return VoterInterface::ACCESS_GRANTED;
  285. }
  286. //if the user has an expert role but not an expert viewer role
  287. if ($user->isExpertOnly() && !$user->isExpertViewerOnly()) {
  288. return VoterInterface::ACCESS_GRANTED;
  289. }
  290. return VoterInterface::ACCESS_DENIED;
  291. }
  292. // if we are looking if the user may view a list of projects
  293. if ($attribute == self::LIST_PROJECT) {
  294. // if the user is an expert agency administrator let them pass.
  295. // this should probably be done in a better way, tying permissions
  296. // to an entity in one place somehow....
  297. if ($user->isExpertAgencyAdministrator()) {
  298. return VoterInterface::ACCESS_GRANTED;
  299. }
  300. $accessibleAccounts = $this->userHelper->getAccountIdsWithAnyRole();
  301. $accessibleProjects = $this->userHelper->getProjectIdsWithAnyRole();
  302. // if the user has been assigned at least one Account or Project
  303. // role, then they have access
  304. if ($accessibleAccounts !== [] || $accessibleProjects !== []) {
  305. return VoterInterface::ACCESS_GRANTED;
  306. }
  307. }
  308. // Check if the User can download a list of projects for all the Accounts they have access to.
  309. if ($attribute === self::DOWNLOAD_PROJECT_LIST) {
  310. return $this->canDownloadProjectList($user);
  311. }
  312. if ($attribute === self::LIST_FIRM) {
  313. return $this->canListFirm($user);
  314. }
  315. // If we get to the end of this function, then no decisions have been
  316. // made so we deny access
  317. return VoterInterface::ACCESS_DENIED;
  318. }
  319. /**
  320. * Checks if a user can download a project list.
  321. *
  322. * @param User $user
  323. */
  324. protected function canDownloadProjectList(User $user): int
  325. {
  326. if ($user) {
  327. switch (true) {
  328. case $user->isAccountProjectManager():
  329. case $user->isAccountAdministrator():
  330. return self::ACCESS_GRANTED;
  331. }
  332. }
  333. return self::ACCESS_DENIED;
  334. }
  335. private function canListFirm(User $user): int
  336. {
  337. if ($user && $user->isAccountTechnicalAdmin()) {
  338. return self::ACCESS_GRANTED;
  339. }
  340. return self::ACCESS_DENIED;
  341. }
  342. /**
  343. * Determines if a user can view the 'My Profile' section of their account section.
  344. *
  345. * Access is granted only if the user has ONLY Expert or Scanner roles
  346. * (Expert, Expert View Only, Scanner, Scanner Download) that are NOT
  347. * for Disclosure matters.
  348. *
  349. * @param User $user
  350. *
  351. * @return int
  352. */
  353. private function canViewMyProfile(User $user): int
  354. {
  355. // Load the user into the UserHelper
  356. $this->userHelper->setUser($user);
  357. // If the user has an Expert profile already, grant access.
  358. $expertEntity = $this->expertUserRepository->getExpertByUser($user);
  359. if ($expertEntity instanceof Expert) {
  360. return self::ACCESS_GRANTED;
  361. }
  362. $parsedRoles = $this->userHelper->getParsedRoles();
  363. $hasExpertRole = false;
  364. /**
  365. * It was decided in June 2026 that we will only allow access
  366. * to the profile page for users we've already added to our expert DB.
  367. * This is so that we have more control over who gets included. Currently,
  368. * we do not have a review system for experts who complete their own profile.
  369. */
  370. // // The user may only view their profile if:
  371. // // 1) ALL of their roles are Project-level Expert or Scanner roles
  372. // // 2) At least one role is an Expert role on a non-Disclosure project
  373. // // (scanner-only users should not see this page)
  374. // // Disclosure and Scanner roles are permitted but do not qualify on their own.
  375. // /** @var ParsedRole $parsedRole */
  376. // foreach ($parsedRoles as $parsedRole) {
  377. // // If subject isn't a Project, deny
  378. // if (!($parsedRole->getSubject() instanceof Project)) {
  379. // return self::ACCESS_DENIED;
  380. // }
  381. // // If the role is neither Expert nor Scanner, deny
  382. // if ($parsedRole->isRoleLevelExpert() === false && $parsedRole->isRoleLevelScanner() === false) {
  383. // return self::ACCESS_DENIED;
  384. // }
  385. // // Only count non-Disclosure Expert roles as qualifying
  386. // if ($parsedRole->isRoleLevelExpert() && $parsedRole->isDisclosureSubject() === false) {
  387. // $hasExpertRole = true;
  388. // }
  389. // }
  390. // // Also check role invitations with the same criteria:
  391. // // All invitations must be Project-level Expert or Scanner roles.
  392. // // Only non-Disclosure Expert invitations count as qualifying.
  393. // /** @var ParsedRole $parsedRole */
  394. // foreach ($this->userHelper->getRoleInvitationsParsedRoles() as $parsedRole) {
  395. // // If subject isn't a Project, deny
  396. // if (!($parsedRole->getSubject() instanceof Project)) {
  397. // return self::ACCESS_DENIED;
  398. // }
  399. // // If it's not an Expert or Scanner role, deny
  400. // if ($parsedRole->isRoleLevelExpert() === false && $parsedRole->isRoleLevelScanner() === false) {
  401. // return self::ACCESS_DENIED;
  402. // }
  403. // // Only count non-Disclosure Expert invitations as qualifying
  404. // if ($parsedRole->isRoleLevelExpert() && $parsedRole->isDisclosureSubject() === false) {
  405. // $hasExpertRole = true;
  406. // }
  407. // }
  408. return $hasExpertRole ? self::ACCESS_GRANTED : self::ACCESS_DENIED;
  409. }
  410. /**
  411. * Determines if a user can see their Expert profile in the account section.
  412. *
  413. * Requires a linked Expert whose profile has been reviewed. Both internal and expert review count:
  414. * after an internal review the expert must be able to open the profile to confirm it. This is separate
  415. * from VIEW_MY_PROFILE, which still guards the verify-my-profile page (where unreviewed experts
  416. * confirm their profile after responding to a Letter of Approach) and the photo streams.
  417. *
  418. * @param User $user
  419. *
  420. * @return int
  421. */
  422. private function canViewAccountExpertProfile(User $user): int
  423. {
  424. $expertEntity = $this->expertUserRepository->getExpertByUser($user);
  425. return $expertEntity instanceof Expert && !$expertEntity->isUnreviewed()
  426. ? self::ACCESS_GRANTED
  427. : self::ACCESS_DENIED;
  428. }
  429. }