src/Security/Voter/ProjectLicenseLevelVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use InvalidArgumentException;
  4. use MedBrief\MSR\Entity\Project;
  5. use Override;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class ProjectLicenseLevelVoter implements VoterInterface
  10. {
  11. public const FEATURE_VIEW_RADIOLOGY = 'FEATURE_VIEW_RADIOLOGY';
  12. public function supportsAttribute($attribute): bool
  13. {
  14. return $attribute == self::FEATURE_VIEW_RADIOLOGY;
  15. }
  16. public function supportsClass($class): bool
  17. {
  18. $supportedClass = Project::class;
  19. return $supportedClass === $class || is_subclass_of($class, $supportedClass);
  20. }
  21. /**
  22. *
  23. * @param mixed $entity
  24. */
  25. #[Override]
  26. public function vote(TokenInterface $token, $entity, array $attributes)
  27. {
  28. /**
  29. * START: This is common code for all Voter::vote() methods
  30. */
  31. // check if class of this object is supported by this voter
  32. if (!$this->supportsClass($entity && !is_array($entity) ? $entity::class : '')) {
  33. return VoterInterface::ACCESS_ABSTAIN;
  34. }
  35. // check if the voter is used correct, only allow one attribute
  36. // this isn't a requirement, it's just one easy way for you to
  37. // design your voter
  38. if (1 !== count($attributes)) {
  39. throw new InvalidArgumentException(
  40. 'Only one attribute is allowed for medbrief Voters.'
  41. );
  42. }
  43. // set the attribute to check against
  44. $attribute = $attributes[0];
  45. // check if the given attribute is covered by this voter
  46. if (!$this->supportsAttribute($attribute)) {
  47. return VoterInterface::ACCESS_ABSTAIN;
  48. }
  49. // get current logged in user
  50. $user = $token->getUser();
  51. // make sure there is a user object (i.e. that the user is logged in)
  52. if (!$user instanceof UserInterface) {
  53. return VoterInterface::ACCESS_DENIED;
  54. }
  55. // In the case of this voter, MedBrief Admin and Super Admin should not see the feature either,
  56. // as the functionality is effectively removed.
  57. // if ($this->authorizationChecker->isGranted('ROLE_ADMIN'))
  58. // {
  59. // return VoterInterface::ACCESS_GRANTED;
  60. // }
  61. /**
  62. * END: Common code for all Voter:vote() methods. Put custom logic below.
  63. */
  64. // An array containing the features we can check with this voter, and the corresponding license levels that
  65. // grant access to them.
  66. $featureLicenses = [];
  67. // FEATURE_VIEW_RADIOLOGY
  68. $featureLicenses[self::FEATURE_VIEW_RADIOLOGY] = $this->allLicenseLevelsExcept([
  69. Project::LICENSE_LEVEL_BASIC,
  70. ]);
  71. // If we have a set of allowed license levels for this attribute, and the Project's license level falls in that set,
  72. // we grant access.
  73. if (isset($featureLicenses[$attribute]) && in_array($entity->getLicenseLevel(), $featureLicenses[$attribute], true)) {
  74. return VoterInterface::ACCESS_GRANTED;
  75. }
  76. // If we get to the end of this function, then no decisions have been
  77. // made so we deny access
  78. return VoterInterface::ACCESS_DENIED;
  79. }
  80. /**
  81. * A convenience method for specifying all Project License Levels,
  82. * except the specified excluded ones.
  83. *
  84. * @param array $excludeLicenseLevels
  85. */
  86. private function allLicenseLevelsExcept(array $excludeLicenseLevels): array
  87. {
  88. return array_filter($this->allLicenseLevels(), fn ($licenseLevel): bool => !in_array($licenseLevel, $excludeLicenseLevels));
  89. }
  90. /**
  91. * A convenience method for specifying all Project License Levels.
  92. */
  93. private function allLicenseLevels(): array
  94. {
  95. return array_keys(Project::getILicenseLevelOptions());
  96. }
  97. }