vendor/sulu/sulu/src/Sulu/Component/Security/Authorization/SecurityContextVoter.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Component\Security\Authorization;
  11. use Sulu\Bundle\SecurityBundle\Entity\User;
  12. use Sulu\Component\Security\Authorization\AccessControl\AccessControlManagerInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  15. /**
  16. * Checks the Sulu security.
  17. */
  18. class SecurityContextVoter implements VoterInterface
  19. {
  20. /**
  21. * @param mixed[] $permissions
  22. */
  23. public function __construct(
  24. private AccessControlManagerInterface $accessControlManager,
  25. /**
  26. * The permissions available, defined by config.
  27. */
  28. private $permissions
  29. ) {
  30. }
  31. public function supportsAttribute($attribute)
  32. {
  33. return \in_array($attribute, \array_keys($this->permissions));
  34. }
  35. public function supportsClass($class)
  36. {
  37. return SecurityCondition::class === $class || \is_subclass_of($class, SecurityCondition::class);
  38. }
  39. public function vote(TokenInterface $token, $object, array $attributes): int
  40. {
  41. /** @var User $user */
  42. $user = $token->getUser();
  43. if (!\is_object($object)
  44. || !$this->supportsClass(\get_class($object))
  45. ) {
  46. return VoterInterface::ACCESS_ABSTAIN;
  47. }
  48. $userPermissions = $this->accessControlManager->getUserPermissions($object, $user);
  49. if (0 === \count($userPermissions)) {
  50. return VoterInterface::ACCESS_DENIED;
  51. }
  52. // only if all attributes are granted the access is granted
  53. foreach ($attributes as $attribute) {
  54. if (isset($userPermissions[$attribute]) && !$userPermissions[$attribute]) {
  55. return VoterInterface::ACCESS_DENIED;
  56. }
  57. }
  58. return VoterInterface::ACCESS_GRANTED;
  59. }
  60. }