src/Security/Voter/ChronologyItemVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Security\Voter;
  3. use InvalidArgumentException;
  4. use MedBrief\MSR\Entity\ChronologyItem;
  5. use Override;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class ChronologyItemVoter implements VoterInterface
  11. {
  12. public const EDIT = 'EDIT';
  13. public const DELETE = 'DELETE';
  14. public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker)
  15. {
  16. }
  17. public function supportsAttribute($attribute): bool
  18. {
  19. return in_array($attribute, [
  20. self::EDIT,
  21. self::DELETE,
  22. ]);
  23. }
  24. public function supportsClass($class): bool
  25. {
  26. $supportedClass = ChronologyItem::class;
  27. return $supportedClass === $class || is_subclass_of($class, $supportedClass);
  28. }
  29. /**
  30. *
  31. * @param mixed $chronologyItem
  32. */
  33. #[Override]
  34. public function vote(TokenInterface $token, $chronologyItem, array $attributes)
  35. {
  36. /**
  37. * START: This is common code for all Voter::vote() methods
  38. */
  39. // check if class of this object is supported by this voter
  40. if (!$this->supportsClass($chronologyItem ? $chronologyItem::class : '')) {
  41. return VoterInterface::ACCESS_ABSTAIN;
  42. }
  43. // check if the voter is used correct, only allow one attribute
  44. // this isn't a requirement, it's just one easy way for you to
  45. // design your voter
  46. if (1 !== count($attributes)) {
  47. throw new InvalidArgumentException(
  48. 'Only one attribute is allowed for medbrief Voters.'
  49. );
  50. }
  51. // set the attribute to check against
  52. $attribute = $attributes[0];
  53. // check if the given attribute is covered by this voter
  54. if (!$this->supportsAttribute($attribute)) {
  55. return VoterInterface::ACCESS_ABSTAIN;
  56. }
  57. // get current logged in user
  58. $user = $token->getUser();
  59. // make sure there is a user object (i.e. that the user is logged in)
  60. if (!$user instanceof UserInterface) {
  61. return VoterInterface::ACCESS_DENIED;
  62. }
  63. // chronology item creator can do anything
  64. if ($user == $chronologyItem->getCreator()) {
  65. return VoterInterface::ACCESS_GRANTED;
  66. }
  67. // get the project
  68. $project = $chronologyItem->getChronology()->getProject();
  69. // check if the user is granted chronology administrative rights
  70. if ($this->authorizationChecker->isGranted(ProjectVoter::CHRONOLOGY_ADMINISTRATION, $project)) {
  71. return VoterInterface::ACCESS_GRANTED;
  72. }
  73. // If we get to the end of this function, then no decisions have been
  74. // made so we deny access
  75. return VoterInterface::ACCESS_DENIED;
  76. }
  77. }