src/Security/Voter/MatterRequest/Details/PostProcessingVoter.php line 14

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