vendor/lolautruche/ez-core-extra-bundle/Security/Voter/SimplifiedCoreVoter.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the EzCoreExtraBundle package.
  4. *
  5. * (c) Jérôme Vieilledent <jerome@vieilledent.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Lolautruche\EzCoreExtraBundle\Security\Voter;
  11. use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
  12. use Ibexa\Contracts\Core\Repository\Values\ValueObject;
  13. use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  16. class SimplifiedCoreVoter implements VoterInterface
  17. {
  18. const IBEXA_ROLE_PREFIX = 'ibexa:';
  19. /**
  20. * @var VoterInterface
  21. */
  22. private $coreVoter;
  23. /**
  24. * @var VoterInterface
  25. */
  26. private $valueObjectVoter;
  27. public function __construct(VoterInterface $coreVoter, VoterInterface $valueObjectVoter)
  28. {
  29. $this->coreVoter = $coreVoter;
  30. $this->valueObjectVoter = $valueObjectVoter;
  31. }
  32. public function supportsAttribute($attribute)
  33. {
  34. return is_string($attribute) && stripos($attribute, static::IBEXA_ROLE_PREFIX) === 0;
  35. }
  36. public function supportsClass($class)
  37. {
  38. return true;
  39. }
  40. public function vote(TokenInterface $token, $object, array $attributes)
  41. {
  42. foreach ($attributes as $attribute) {
  43. if (!$this->supportsAttribute($attribute)) {
  44. continue;
  45. }
  46. $attribute = substr($attribute, strlen(static::IBEXA_ROLE_PREFIX));
  47. list($module, $function) = explode(':', $attribute);
  48. $attributeObject = new AuthorizationAttribute($module, $function);
  49. try {
  50. if ($object instanceof ValueObject) {
  51. $attributeObject->limitations = ['valueObject' => $object];
  52. return $this->valueObjectVoter->vote($token, $object, [$attributeObject]);
  53. } else {
  54. return $this->coreVoter->vote($token, $object, [$attributeObject]);
  55. }
  56. } catch (InvalidArgumentException $e) {
  57. continue;
  58. }
  59. }
  60. return static::ACCESS_ABSTAIN;
  61. }
  62. }