src/Security/Voter/MatterRequest/MatterRequestVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter\MatterRequest;
  3. use InvalidArgumentException;
  4. use MedBrief\MSR\Entity\MatterRequest\MatterRequest;
  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 MatterRequestVoter 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 const DOWNLOAD_SUMMARY = 'DOWNLOAD_SUMMARY';
  19. public const UPDATE_STATUS = 'UPDATE_STATUS';
  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::DOWNLOAD_SUMMARY,
  28. self::UPDATE_STATUS,
  29. ]);
  30. }
  31. public function supportsClass($class): bool
  32. {
  33. $supportedClass = MatterRequest::class;
  34. return $supportedClass === $class || is_subclass_of($class, $supportedClass);
  35. }
  36. /**
  37. *
  38. * @param mixed $entity
  39. */
  40. #[Override]
  41. public function vote(TokenInterface $token, $entity, array $attributes)
  42. {
  43. /**
  44. * START: This is common code for all Voter::vote() methods
  45. */
  46. // check if class of this object is supported by this voter
  47. if (!$this->supportsClass($entity && !is_array($entity) ? $entity::class : '')) {
  48. return VoterInterface::ACCESS_ABSTAIN;
  49. }
  50. // check if the voter is used correct, only allow one attribute
  51. // this isn't a requirement, it's just one easy way for you to
  52. // design your voter
  53. if (1 !== count($attributes)) {
  54. throw new InvalidArgumentException(
  55. 'Only one attribute is allowed for Medbrief Voters.'
  56. );
  57. }
  58. // set the attribute to check against
  59. $attribute = $attributes[0];
  60. // check if the given attribute is covered by this voter
  61. if (!$this->supportsAttribute($attribute)) {
  62. return VoterInterface::ACCESS_ABSTAIN;
  63. }
  64. if ($attribute === self::DOWNLOAD_SUMMARY) {
  65. return VoterInterface::ACCESS_DENIED;
  66. }
  67. // get current logged in user
  68. /** @var User $user */
  69. $user = $token->getUser();
  70. // make sure there is a user object (i.e. that the user is logged in)
  71. if (!$user instanceof UserInterface) {
  72. return VoterInterface::ACCESS_DENIED;
  73. }
  74. /**
  75. * END: Common code for all Voter:vote() methods. Put custom logic below.
  76. */
  77. $matterRequest = $entity;
  78. if ($attribute === self::CREATE) {
  79. return $this->canCreate($matterRequest, $user);
  80. }
  81. if ($attribute === self::READ) {
  82. return $this->canRead($matterRequest, $user);
  83. }
  84. if ($attribute === self::UPDATE) {
  85. return $this->canUpdate($matterRequest, $user);
  86. }
  87. if ($attribute === self::DELETE) {
  88. return $this->canDelete($matterRequest, $user);
  89. }
  90. if ($attribute === self::UPDATE_STATUS) {
  91. return $this->canUpdateStatus($matterRequest, $user);
  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. }