vendor/ibexa/cart/src/lib/Security/Authorization/PolicyVoter.php line 16

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Cart\Security\Authorization;
  8. use Ibexa\Contracts\Core\Repository\PermissionResolver;
  9. use Ibexa\Contracts\ProductCatalog\Permission\Policy\PolicyInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  12. final class PolicyVoter implements VoterInterface
  13. {
  14. private PermissionResolver $permissionResolver;
  15. public function __construct(PermissionResolver $permissionResolver)
  16. {
  17. $this->permissionResolver = $permissionResolver;
  18. }
  19. /**
  20. * Checks if the voter supports the given attribute.
  21. *
  22. * @param mixed $attribute An attribute
  23. *
  24. * @return bool true if this Voter supports the attribute, false otherwise
  25. */
  26. public function supportsAttribute($attribute): bool
  27. {
  28. return $attribute instanceof PolicyInterface;
  29. }
  30. /**
  31. * @param array<mixed> $attributes
  32. */
  33. public function vote(TokenInterface $token, $object, array $attributes): int
  34. {
  35. foreach ($attributes as $attribute) {
  36. if ($this->supportsAttribute($attribute)) {
  37. if ($this->hasAccess($attribute)) {
  38. return VoterInterface::ACCESS_DENIED;
  39. }
  40. return VoterInterface::ACCESS_GRANTED;
  41. }
  42. }
  43. return VoterInterface::ACCESS_ABSTAIN;
  44. }
  45. private function hasAccess(PolicyInterface $attribute): bool
  46. {
  47. return $this->permissionResolver->hasAccess(
  48. $attribute->getModule(),
  49. $attribute->getFunction()
  50. ) === false;
  51. }
  52. }