src/Security/Voter/MatterRequest/SupportingDocumentVoter.php line 13

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