Fatal error due to permission_callbacks in GroupPermissionHandler::buildPermissionsYaml

Created on 6 January 2023, over 2 years ago
Updated 17 December 2024, 4 months ago

Problem/Motivation

After updating the module from 1.4 to 1.5, I get the following error when visiting group permissions page:

InvalidArgumentException: $string ("") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).

Drupal\group\Access\GroupPermissionHandler->t() (Line: 183)
Drupal\group\Access\GroupPermissionHandler->completePermission() (Line: 237)
Drupal\group\Access\GroupPermissionHandler->buildPermissionsYaml() (Line: 137)
Drupal\group\Access\GroupPermissionHandler->getPermissionsIncludingPlugins() (Line: 124)
Drupal\group\Access\GroupPermissionHandler->getPermissionsByGroupType() (Line: 105)
Drupal\group\Form\GroupPermissionsForm->getPermissions() (Line: 172)
Drupal\group\Form\GroupPermissionsForm->buildForm() (Line: 112)
Drupal\group\Form\GroupPermissionsTypeSpecificForm->buildForm()
call_user_func_array() (Line: 531)
Drupal\Core\Form\FormBuilder->retrieveForm() (Line: 278)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
Drupal\Core\Controller\FormController->getContentResult()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 48)
Drupal\ucpkn_core\StackMiddleware\RedirectMiddleware->handle() (Line: 58)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
Stack\StackedHttpKernel->handle() (Line: 709)
Drupal\Core\DrupalKernel->handle() (Line: 19)

This is happening due to the changes introduced in https://www.drupal.org/node/3236626
Line 206, sets $permission_sets = [$permissions];
This assigns the $permissions including permission_callbacks (if exists) to $permission_sets.
Line 218 unset the permission_callbacks from the $permissions array. However, the permission_callbacks still exists in the $permission_sets due to the first assignment.
When merging (Line 221) the elements of $permission_sets subarrays, besides the permissions defined in yml and the permissions generated by the permission_callback, we also have "permission_callbacks => [...]"

Steps to reproduce

  • In your custom module, generate some permissions using permission_callbacks.
  • Visit any group type permissions page.

Proposed resolution

Solution #1: Simply unset the permission_callbacks from $permission_sets

      if (isset($permissions['permission_callbacks'])) {
       unset($permission_sets[0]['permission_callbacks']);
       ....
      }

Solution #2: Reset $permissions_sets when having permissions_callbacks

      if (isset($permissions['permission_callbacks'])) {
        $permission_callbacks = $permissions['permission_callbacks'];
        unset($permissions['permission_callbacks']);
        $permission_sets = [$permissions];
        foreach ($permission_callbacks as $permission_callback) {
          $callback = $this->controllerResolver->getControllerFromDefinition($permission_callback);
          $callback_permissions = call_user_func($callback);
          assert(is_array($callback_permissions));
          $permission_sets[] = $callback_permissions;
        }
      }

Remaining tasks

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇧🇪Belgium msnassar

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇭Switzerland OlivierG

    Same issue with Group1 when I'm using Group permissions and Group Permissions Template.
    Finally I created another patch that write the function GroupPermissionHandler::buildPermissionsYaml the same way it was write in Group 2.

Production build 0.71.5 2024