src/Security/Voter/RecordsRequestLetterVoter.php line 13

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