Error on dependency injection in permission callbacks

Created on 8 August 2023, over 1 year ago

Problem/Motivation

Dependency injection error on permission callback.

The PermissionHandler.php get all callbacks from the yml of the modules (installed or not).

    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
      // 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);
          if ($callback_permissions = call_user_func($callback)) {
            // Add any callback permissions to the array of permissions. Any
            // defaults can then get processed below.
            foreach ($callback_permissions as $name => $callback_permission) {
              if (!is_array($callback_permission)) {
                $callback_permission = [
                  'title' => $callback_permission,
                ];
              }

              $callback_permission += [
                'description' => NULL,
                'provider' => $provider,
              ];

              $all_callback_permissions[$name] = $callback_permission;
            }
          }
        }

In theory, the dependency injection is supported, but it throws an error on the line

$callback = $this->controllerResolver->getControllerFromDefinition($permission_callback);

if the service required is defined by a module not installed (yet) on the drush si process.

Steps to reproduce

Define a custom permission callback and inject a service of a contrib module, in my case is https://github.com/KEY-TEC/content_translation_access.git with the patches:

            "drupal/content_translation_access":  {
                "https://github.com/Metadrop/content_translation_access/pull/1 - Update routing priority as there are more modules with the same priority": "https://github.com/Metadrop/content_translation_access/compare/fb-drupal9-compatibility...5f38b51bead9b0e6316dcafda250dbb9e7b280bc.patch",
                "https://github.com/Metadrop/content_translation_access/pull/2 - Check permissions in translations overview using Permissions class": "https://github.com/Metadrop/content_translation_access/compare/fb-drupal9-compatibility...2b66f34f32abc2256cf4db569bf6b51ef52b5d45.patch",
                "https://github.com/KEY-TEC/content_translation_access/pull/8 - Override node access to use the content translation access instead": "contrib_patches/content_translation_access-node-access-override-pr-8.patch"
            },

The module define a custom permission:

permission_callbacks:
  - \Drupal\content_translation_access\Permissions::ctaPermissions

That requires the service content_translation.service.

Add the module content_translation_access to the core.extension.

Install the site from config.

./vendor/drush/drush/drush si --existing-config -y

Callstack of the error:

In ContainerBuilder.php line 1030:
                                                                              
  [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]  
  You have requested a non-existent service "content_translation.manager".    
                                                                              

Exception trace:
  at /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php:1030
 Symfony\Component\DependencyInjection\ContainerBuilder->getDefinition() at /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php:600
 Symfony\Component\DependencyInjection\ContainerBuilder->doGet() at /var/www/html/vendor/symfony/dependency-injection/ContainerBuilder.php:558
 Symfony\Component\DependencyInjection\ContainerBuilder->get() at /var/www/html/docroot/modules/contrib/content_translation_access/src/Permissions.php:53
 Drupal\content_translation_access\Permissions::create() at /var/www/html/docroot/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php:28
 Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition() at /var/www/html/docroot/core/lib/Drupal/Core/Controller/ControllerResolver.php:117
 Drupal\Core\Controller\ControllerResolver->createController() at /var/www/html/docroot/core/lib/Drupal/Core/Controller/ControllerResolver.php:69
 Drupal\Core\Controller\ControllerResolver->getControllerFromDefinition() at /var/www/html/docroot/core/modules/user/src/PermissionHandler.php:152
 Drupal\user\PermissionHandler->buildPermissionsYaml() at /var/www/html/docroot/core/modules/user/src/PermissionHandler.php:112
 Drupal\user\PermissionHandler->getPermissions() at /var/www/html/docroot/core/modules/user/src/Entity/Role.php:203
 Drupal\user\Entity\Role->calculateDependencies() at /var/www/html/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:320
 Drupal\Core\Config\Entity\ConfigEntityBase->preSave() at /var/www/html/docroot/core/modules/user/src/Entity/Role.php:179
 Drupal\user\Entity\Role->preSave() at /var/www/html/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php:562
 Drupal\Core\Entity\EntityStorageBase->doPreSave() at /var/www/html/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php:517
 Drupal\Core\Entity\EntityStorageBase->save() at /var/www/html/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php:253
 Drupal\Core\Config\Entity\ConfigEntityStorage->save() at /var/www/html/docroot/core/lib/Drupal/Core/Entity/EntityBase.php:339
 Drupal\Core\Entity\EntityBase->save() at /var/www/html/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:608
 Drupal\Core\Config\Entity\ConfigEntityBase->save() at /var/www/html/docroot/core/modules/user/user.module:996
 user_role_grant_permissions() at /var/www/html/docroot/core/modules/node/node.install:133
 node_install() at n/a:n/a
 call_user_func_array() at /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php:417
 Drupal\Core\Extension\ModuleHandler->invoke() at /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleInstaller.php:357
 Drupal\Core\Extension\ModuleInstaller->install() at /var/www/html/docroot/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php:83
 Drupal\Core\ProxyClass\Extension\ModuleInstaller->install() at /var/www/html/docroot/core/lib/Drupal/Core/Config/ConfigImporter.php:853
 Drupal\Core\Config\ConfigImporter->processExtension() at /var/www/html/docroot/core/lib/Drupal/Core/Config/ConfigImporter.php:605
 Drupal\Core\Config\ConfigImporter->processExtensions() at /var/www/html/docroot/core/lib/Drupal/Core/Config/ConfigImporter.php:542
 Drupal\Core\Config\ConfigImporter->doSyncStep() at /var/www/html/docroot/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php:31
 Drupal\Core\Config\Importer\ConfigImporterBatch::process() at /var/www/html/docroot/core/includes/batch.inc:295
 _batch_process() at /var/www/html/docroot/core/includes/form.inc:955
 batch_process() at /var/www/html/docroot/core/includes/install.core.inc:653
 install_run_task() at /var/www/html/docroot/core/includes/install.core.inc:571
 install_run_tasks() at /var/www/html/docroot/core/includes/install.core.inc:119
 install_drupal() at /var/www/html/vendor/drush/drush/includes/drush.inc:129
 drush_call_user_func_array() at /var/www/html/vendor/drush/drush/includes/drush.inc:113
 drush_op() at /var/www/html/vendor/drush/drush/src/Commands/core/SiteInstallCommands.php:152
 Drush\Commands\core\SiteInstallCommands->install() at n/a:n/a
 call_user_func_array() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:276
 Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:212
 Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:176
 Consolidation\AnnotatedCommand\CommandProcessor->process() at /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php:391
 Consolidation\AnnotatedCommand\AnnotatedCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1039
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:275
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php:124
 Drush\Runtime\Runtime->doRun() at /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php:51
 Drush\Runtime\Runtime->run() at /var/www/html/vendor/drush/drush/drush.php:77
 require() at /var/www/html/vendor/drush/drush/drush:4

πŸ› Bug report
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component
User systemΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ͺπŸ‡ΈSpain eduardo morales alberti Spain, πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024