vendor/se7enxweb/legacy-bridge/mvc/Security/Voter/VoterDecorator.php line 16

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace eZ\Publish\Core\MVC\Legacy\Security\Voter;
  7. use Closure;
  8. use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
  9. use eZUser;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  12. use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
  13. class VoterDecorator implements VoterInterface
  14. {
  15. private $innerVoter;
  16. private $legacyKernelClosure;
  17. public function __construct(
  18. VoterInterface $innerVoter,
  19. Closure $legacyKernelClosure
  20. ) {
  21. $this->innerVoter = $innerVoter;
  22. $this->legacyKernelClosure = $legacyKernelClosure;
  23. }
  24. public function supportsAttribute($attribute)
  25. {
  26. return $attribute instanceof AuthorizationAttribute;
  27. }
  28. public function supportsClass($class)
  29. {
  30. return true;
  31. }
  32. /**
  33. * Decorates the built in eZ kernel voters to allow eZ Publish legacy
  34. * to vote on modules/functions available only in legacy kernel.
  35. *
  36. * @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
  37. * @param mixed $object
  38. * @param array $attributes
  39. *
  40. * @return int
  41. */
  42. public function vote(TokenInterface $token, $object, array $attributes)
  43. {
  44. try {
  45. return $this->innerVoter->vote($token, $object, $attributes);
  46. } catch (InvalidArgumentException $e) {
  47. $legacyResult = $this->voteLegacy($token, $object, $attributes);
  48. if ($legacyResult === static::ACCESS_ABSTAIN) {
  49. throw $e;
  50. }
  51. return $legacyResult;
  52. }
  53. }
  54. private function voteLegacy(TokenInterface $token, $object, array $attributes)
  55. {
  56. $legacyKernel = \call_user_func($this->legacyKernelClosure);
  57. foreach ($attributes as $attribute) {
  58. if (!$this->supportsAttribute($attribute)) {
  59. continue;
  60. }
  61. $result = $legacyKernel->runCallback(
  62. static function () use ($attribute) {
  63. $currentUser = eZUser::currentUser();
  64. return $currentUser->hasAccessTo($attribute->module, $attribute->function);
  65. },
  66. false
  67. );
  68. if ($result['accessWord'] === 'no') {
  69. return static::ACCESS_DENIED;
  70. }
  71. return static::ACCESS_GRANTED;
  72. }
  73. return static::ACCESS_ABSTAIN;
  74. }
  75. }