src/Security/Voter/LinkedEmailAddressInvitationVoter.php line 14

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