src/Security/Voter/UserVoter.php line 14

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