Commit 15ac0b4e authored by catch's avatar catch
Browse files

Issue #3363424 by _shY, vbouchet, kim.pepper, smustgrave: Use CallableResolver...

Issue #3363424 by _shY, vbouchet, kim.pepper, smustgrave: Use CallableResolver for \Drupal\user\PermissionHandler::buildPermissionsYaml() instead of the controller resolver
parent 30dbdff2
Loading
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Utility\CallableResolver;

/**
 * Provides the available permissions based on yml files.
@@ -68,11 +69,11 @@ class PermissionHandler implements PermissionHandlerInterface {
  protected $yamlDiscovery;

  /**
   * The controller resolver.
   * The callable resolver.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface
   * @var \Drupal\Core\Utility\CallableResolver
   */
  protected $controllerResolver;
  protected CallableResolver $callableResolver;

  /**
   * Constructs a new PermissionHandler.
@@ -81,15 +82,20 @@ class PermissionHandler implements PermissionHandlerInterface {
   *   The module handler.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
   *   The controller resolver.
   * @param \Drupal\Core\Utility\CallableResolver|\Drupal\Core\Controller\ControllerResolverInterface $callable_resolver
   *   The callable resolver.
   */
  public function __construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, ControllerResolverInterface $controller_resolver) {
  public function __construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, ControllerResolverInterface|CallableResolver $callable_resolver) {
    if ($callable_resolver instanceof ControllerResolverInterface) {
      @trigger_error('Calling ' . __METHOD__ . '() with an argument of ControllerResolverInterface is deprecated in drupal:10.2.0 and is removed in drupal:11.0.0. Use \Drupal\Core\Utility\CallableResolver instead. See https://www.drupal.org/node/3397954', E_USER_DEPRECATED);
      $callable_resolver = \Drupal::service('callable_resolver');
    }
    $this->callableResolver = $callable_resolver;

    // @todo It would be nice if you could pull all module directories from the
    //   container.
    $this->moduleHandler = $module_handler;
    $this->stringTranslation = $string_translation;
    $this->controllerResolver = $controller_resolver;
  }

  /**
@@ -144,12 +150,12 @@ protected function buildPermissionsYaml() {
    $all_callback_permissions = [];

    foreach ($this->getYamlDiscovery()->findAll() as $provider => $permissions) {
      // The top-level 'permissions_callback' is a list of methods in controller
      // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods
      // The top-level 'permissions_callback' is a list of methods in callable
      // syntax, see \Drupal\Core\Utility\CallableResolver. These methods
      // should return an array of permissions in the same structure.
      if (isset($permissions['permission_callbacks'])) {
        foreach ($permissions['permission_callbacks'] as $permission_callback) {
          $callback = $this->controllerResolver->getControllerFromDefinition($permission_callback);
          $callback = $this->callableResolver->getCallableFromDefinition($permission_callback);
          if ($callback_permissions = call_user_func($callback)) {
            // Add any callback permissions to the array of permissions. Any
            // defaults can then get processed below.
+14 −14
Original line number Diff line number Diff line
@@ -45,11 +45,11 @@ class PermissionHandlerTest extends UnitTestCase {
  protected $stringTranslation;

  /**
   * The mocked controller resolver.
   * The mocked callable resolver.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Utility\CallableResolver|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $controllerResolver;
  protected $callableResolver;

  /**
   * {@inheritdoc}
@@ -58,7 +58,7 @@ protected function setUp(): void {
    parent::setUp();

    $this->stringTranslation = new TestTranslationManager();
    $this->controllerResolver = $this->createMock('Drupal\Core\Controller\ControllerResolverInterface');
    $this->callableResolver = $this->createMock('Drupal\Core\Utility\CallableResolver');
  }

  /**
@@ -127,10 +127,10 @@ public function testBuildPermissionsYaml() {
      ->method('getModuleList')
      ->willReturn(array_flip($modules));

    $this->controllerResolver->expects($this->never())
      ->method('getControllerFromDefinition');
    $this->callableResolver->expects($this->never())
      ->method('getCallableFromDefinition');

    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver);

    $actual_permissions = $this->permissionHandler->getPermissions();
    $this->assertPermissions($actual_permissions);
@@ -191,7 +191,7 @@ public function testBuildPermissionsSortPerModule() {
      ->method('getModuleList')
      ->willReturn(array_flip($modules));

    $permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
    $permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver);
    $actual_permissions = $permissionHandler->getPermissions();
    $this->assertEquals(['access_module_a4', 'access_module_a1', 'access_module_a2', 'access_module_a3'],
      array_keys($actual_permissions));
@@ -245,8 +245,8 @@ public function testBuildPermissionsYamlCallback() {
      ->method('getModuleList')
      ->willReturn(array_flip($modules));

    $this->controllerResolver->expects($this->exactly(4))
      ->method('getControllerFromDefinition')
    $this->callableResolver->expects($this->exactly(4))
      ->method('getCallableFromDefinition')
      ->willReturnMap([
        ['Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription', [new TestPermissionCallbacks(), 'singleDescription']],
        ['Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription', [new TestPermissionCallbacks(), 'titleDescription']],
@@ -254,7 +254,7 @@ public function testBuildPermissionsYamlCallback() {
        ['Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess', [new TestPermissionCallbacks(), 'titleDescriptionRestrictAccess']],
      ]);

    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver);

    $actual_permissions = $this->permissionHandler->getPermissions();
    $this->assertPermissions($actual_permissions);
@@ -292,12 +292,12 @@ public function testPermissionsYamlStaticAndCallback() {
      ->method('getModuleList')
      ->willReturn(array_flip($modules));

    $this->controllerResolver->expects($this->once())
      ->method('getControllerFromDefinition')
    $this->callableResolver->expects($this->once())
      ->method('getCallableFromDefinition')
      ->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')
      ->willReturn([new TestPermissionCallbacks(), 'titleDescription']);

    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver);

    $actual_permissions = $this->permissionHandler->getPermissions();

+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ services:
  Drupal\user\UserAuthInterface: '@user.auth'
  user.permissions:
    class: Drupal\user\PermissionHandler
    arguments: ['@module_handler', '@string_translation', '@controller_resolver']
    arguments: ['@module_handler', '@string_translation', '@callable_resolver']
  Drupal\user\PermissionHandlerInterface: '@user.permissions'
  user.current_user_context:
    class: Drupal\user\ContextProvider\CurrentUserContext