src/Security/Voter/BillingItemVoter.php line 14

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