src/Security/Voter/HelpArticleVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use MedBrief\MSR\Entity\HelpArticle;
  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 HelpArticleVoter extends Voter
  11. {
  12. public const CREATE = 'CREATE';
  13. public const UPDATE = 'UPDATE';
  14. public const VIEW = 'VIEW';
  15. public const DELETE = 'DELETE';
  16. public const ADMINISTRATION = 'ADMINISTRATION';
  17. public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker)
  18. {
  19. }
  20. #[Override]
  21. protected function supports($attribute, $subject): bool
  22. {
  23. return in_array($attribute, [
  24. self::CREATE,
  25. self::UPDATE,
  26. self::VIEW,
  27. self::DELETE,
  28. self::ADMINISTRATION,
  29. ])
  30. && $subject instanceof HelpArticle;
  31. }
  32. #[Override]
  33. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  34. {
  35. $user = $token->getUser();
  36. // if the user is anonymous, do not grant access
  37. if (!$user instanceof UserInterface) {
  38. return false;
  39. }
  40. /** @var HelpArticle $helpArticle */
  41. $helpArticle = $subject;
  42. // ... (check conditions and return true to grant permission) ...
  43. return match ($attribute) {
  44. self::CREATE, self::UPDATE, self::DELETE => $this->canCreate($user),
  45. self::VIEW => $this->canView($user, $helpArticle),
  46. self::ADMINISTRATION => $this->isAdministration(),
  47. default => false,
  48. };
  49. }
  50. protected function canCreate(User $user)
  51. {
  52. // MEDBRIEF HELP ADMIN
  53. return $this->authorizationChecker->isGranted('ROLE_HELP_ADMIN');
  54. }
  55. protected function canView(User $user, HelpArticle $helpArticle)
  56. {
  57. // MEDBRIEF ADMIN
  58. if ($this->isAdministration()) {
  59. return true;
  60. }
  61. // No one except admins should see hidden articles
  62. if ($helpArticle->getHidden() === true) {
  63. return false;
  64. }
  65. // If the user is a client level user...
  66. // they should only ever see client content types.
  67. if ($user->isAccountAdministrator()
  68. || $user->isAccountProjectManager()
  69. || $user->isAccountTechnicalAdmin()
  70. || $user->isProjectManager()
  71. || $user->isAccountSorter()
  72. ) {
  73. // If the user has both a client level and matter level access, we use their client level access to determine which
  74. // articles they see.
  75. return $helpArticle->isClientContentType();
  76. }
  77. // Third Party Articles can be seen by everyone else
  78. return $helpArticle->isThirdPartyContentType();
  79. }
  80. /**
  81. * Returns true if the user has an admin related role
  82. *
  83. * @return bool
  84. */
  85. protected function isAdministration()
  86. {
  87. // MEDBRIEF ADMIN
  88. return $this->authorizationChecker->isGranted('ROLE_ADMIN');
  89. }
  90. }