vendor/ibexa/core/src/lib/MVC/Symfony/Security/Authorization/Voter/CoreVoter.php line 14

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. class CoreVoter implements VoterInterface
  12. {
  13.     /** @var \Ibexa\Contracts\Core\Repository\PermissionResolver */
  14.     private $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 string $attribute An attribute
  23.      *
  24.      * @return bool true if this Voter supports the attribute, false otherwise
  25.      */
  26.     public function supportsAttribute($attribute)
  27.     {
  28.         return $attribute instanceof AuthorizationAttribute && empty($attribute->limitations);
  29.     }
  30.     /**
  31.      * Checks if the voter supports the given class.
  32.      *
  33.      * @param string $class A class name
  34.      *
  35.      * @return true if this Voter can process the class
  36.      */
  37.     public function supportsClass($class)
  38.     {
  39.         return true;
  40.     }
  41.     /**
  42.      * Returns the vote for the given parameters.
  43.      *
  44.      * This method must return one of the following constants:
  45.      * ACCESS_GRANTED, ACCESS_DENIED, or ACCESS_ABSTAIN.
  46.      *
  47.      * @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token A TokenInterface instance
  48.      * @param object $object The object to secure
  49.      * @param array $attributes An array of attributes associated with the method being invoked
  50.      *
  51.      * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
  52.      */
  53.     public function vote(TokenInterface $token$object, array $attributes)
  54.     {
  55.         foreach ($attributes as $attribute) {
  56.             if ($this->supportsAttribute($attribute)) {
  57.                 if ($this->permissionResolver->hasAccess($attribute->module$attribute->function) === false) {
  58.                     return VoterInterface::ACCESS_DENIED;
  59.                 }
  60.                 return VoterInterface::ACCESS_GRANTED;
  61.             }
  62.         }
  63.         return VoterInterface::ACCESS_ABSTAIN;
  64.     }
  65. }
  66. class_alias(CoreVoter::class, 'eZ\Publish\Core\MVC\Symfony\Security\Authorization\Voter\CoreVoter');