src/Security/Voter/SortingSessionMemoVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use InvalidArgumentException;
  4. use MedBrief\MSR\Entity\Project;
  5. use MedBrief\MSR\Entity\SortingSession;
  6. use MedBrief\MSR\Entity\SortingSessionMemo;
  7. use MedBrief\MSR\Entity\User;
  8. use MedBrief\MSR\Traits\Security\Authorization\Voter\ClientSortingSessionTrait;
  9. use Override;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. class SortingSessionMemoVoter implements VoterInterface
  15. {
  16. use ClientSortingSessionTrait;
  17. public const READ = 'READ';
  18. public const DELETE = 'DELETE';
  19. public const SYNC = 'SYNC';
  20. public function __construct(private AuthorizationCheckerInterface $authorizationChecker)
  21. {
  22. }
  23. public function supportsAttribute($attribute): bool
  24. {
  25. return in_array($attribute, [
  26. self::READ,
  27. self::DELETE,
  28. self::SYNC,
  29. ]);
  30. }
  31. public function supportsClass($class): bool
  32. {
  33. $supportedClass = SortingSessionMemo::class;
  34. return $supportedClass === $class || is_subclass_of($class, $supportedClass);
  35. }
  36. /**
  37. *
  38. * @param mixed $entity
  39. */
  40. #[Override]
  41. public function vote(TokenInterface $token, $entity, array $attributes)
  42. {
  43. /**
  44. * START: This is common code for all Voter::vote() methods
  45. */
  46. // check if class of this object is supported by this voter
  47. if (!$this->supportsClass($entity && !is_array($entity) ? $entity::class : '')) {
  48. return VoterInterface::ACCESS_ABSTAIN;
  49. }
  50. // check if the voter is used correct, only allow one attribute
  51. // this isn't a requirement, it's just one easy way for you to
  52. // design your voter
  53. if (1 !== count($attributes)) {
  54. throw new InvalidArgumentException(
  55. 'Only one attribute is allowed for Medbrief Voters.'
  56. );
  57. }
  58. // set the attribute to check against
  59. $attribute = $attributes[0];
  60. // check if the given attribute is covered by this voter
  61. if (!$this->supportsAttribute($attribute)) {
  62. return VoterInterface::ACCESS_ABSTAIN;
  63. }
  64. /** @var SortingSessionMemo $sortingSessionMemo */
  65. $sortingSessionMemo = $entity;
  66. // get current logged in user
  67. /** @var User $user */
  68. $user = $token->getUser();
  69. // get current project
  70. /** @var Project $project */
  71. $project = $sortingSessionMemo->getProject();
  72. // get current sorting session
  73. /** @var SortingSession $sortingSession */
  74. $sortingSession = $sortingSessionMemo->getSortingSession();
  75. // make sure there is a user object (i.e. that the user is logged in)
  76. if (!$user instanceof UserInterface) {
  77. return VoterInterface::ACCESS_DENIED;
  78. }
  79. // Admin users can do everything
  80. if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  81. return VoterInterface::ACCESS_GRANTED;
  82. }
  83. /**
  84. * END: Common code for all Voter:vote() methods. Put custom logic below.
  85. */
  86. // For all permissions on a memo/file note, if it is a client sorting session,
  87. // and client is ADMIN, ProjectManager for the Account, or is granted an invite
  88. // as an external ProjectManager to the specific project.... grant full access
  89. if ($this->hasClientSessionAccess($project, $user, $sortingSession)) {
  90. return VoterInterface::ACCESS_GRANTED;
  91. };
  92. // If we get to the end of this function, then no decisions have been
  93. // made so we deny access
  94. return VoterInterface::ACCESS_DENIED;
  95. }
  96. }