vendor/ibexa/core/src/lib/MVC/Symfony/Security/Authorization/Voter/ValueObjectVoter.php line 17

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. namespace Ibexa\Core\MVC\Symfony\Security\Authorization\Voter;
  7. use Ibexa\Contracts\Core\Repository\PermissionResolver;
  8. use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  11. /**
  12.  * Voter to test access to a ValueObject from Repository (e.g. Content, Location...).
  13.  */
  14. class ValueObjectVoter implements VoterInterface
  15. {
  16.     /** @var \Ibexa\Contracts\Core\Repository\PermissionResolver */
  17.     private $permissionResolver;
  18.     public function __construct(PermissionResolver $permissionResolver)
  19.     {
  20.         $this->permissionResolver $permissionResolver;
  21.     }
  22.     public function supportsAttribute($attribute)
  23.     {
  24.         return $attribute instanceof AuthorizationAttribute && isset($attribute->limitations['valueObject']);
  25.     }
  26.     public function supportsClass($class)
  27.     {
  28.         return true;
  29.     }
  30.     /**
  31.      * Returns the vote for the given parameters.
  32.      * Checks if user has access to a given action on a given value object.
  33.      *
  34.      * $attributes->limitations is a hash that contains:
  35.      *  - 'valueObject' - The {@see \Ibexa\Contracts\Core\Repository\Values\ValueObject} to check access on. e.g. Location or Content.
  36.      *  - 'targets' - The location, parent or "assignment" value object, or an array of the same.
  37.      *
  38.      * This method must return one of the following constants:
  39.      * ACCESS_GRANTED, ACCESS_DENIED, or ACCESS_ABSTAIN.
  40.      *
  41.      * @see \Ibexa\Contracts\Core\Repository\PermissionResolver::canUser()
  42.      *
  43.      * @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token      A TokenInterface instance
  44.      * @param object         $object     The object to secure
  45.      * @param array          $attributes An array of attributes associated with the method being invoked
  46.      *
  47.      * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
  48.      */
  49.     public function vote(TokenInterface $token$object, array $attributes)
  50.     {
  51.         foreach ($attributes as $attribute) {
  52.             if ($this->supportsAttribute($attribute)) {
  53.                 $targets = isset($attribute->limitations['targets']) ? $attribute->limitations['targets'] : [];
  54.                 if (
  55.                     $this->permissionResolver->canUser(
  56.                         $attribute->module,
  57.                         $attribute->function,
  58.                         $attribute->limitations['valueObject'],
  59.                         $targets
  60.                     ) === false
  61.                 ) {
  62.                     return VoterInterface::ACCESS_DENIED;
  63.                 }
  64.                 return VoterInterface::ACCESS_GRANTED;
  65.             }
  66.         }
  67.         return VoterInterface::ACCESS_ABSTAIN;
  68.     }
  69. }
  70. class_alias(ValueObjectVoter::class, 'eZ\Publish\Core\MVC\Symfony\Security\Authorization\Voter\ValueObjectVoter');