vendor/ibexa/product-catalog/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\ProductCatalog\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.                 /** @var \Ibexa\Contracts\ProductCatalog\Permission\Policy\PolicyInterface $attribute */
  38.                 if ($this->permissionResolver->hasAccess($attribute->getModule(), $attribute->getFunction()) === false) {
  39.                     return VoterInterface::ACCESS_DENIED;
  40.                 }
  41.                 return VoterInterface::ACCESS_GRANTED;
  42.             }
  43.         }
  44.         return VoterInterface::ACCESS_ABSTAIN;
  45.     }
  46. }