src/Security/Voter/RecordsRequestVoter.php line 13

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