Wrong handling of roles when editing a user with Role Delegation enabled

Created on 12 August 2024, 4 months ago
Updated 31 August 2024, 4 months ago

Problem/Motivation

When editing a user as a non administrator user (Role Delegation module is required), the roles are not processed correctly on save because `$values['roles']` should be an array of role names and Role Delegation has the `target_id` property. Also, we are not processing all the roles, just the last one.

Steps to reproduce

  • Create three new custom roles: `roledelegationassign`, `subscriber` and `premium`
  • Enable Role Delegation module
  • Go to permissions page (Role Delegation section): give the permissions "Assign subscriber role" and "Assign premium role" to the `roledelegationassign` role
  • Create a user with the new `roledelegationassign` role
  • Login as the new user
  • Create a new user
  • Assign the user the `subscriber` and `premium` roles and assign an expiration of "2 months" on both
  • Save the user
  • Role expire won't be saved for both roles

Proposed resolution

Fix the code as seen in the first enclosed patch.

Remaining tasks

Create a MR against 4.x branch.

Original report by pearls

I get this error with Drupal 10.3.x and Role Expire 3.x versions.
Any idea, please?

TypeError: array_diff(): Argument #2 must be of type array, null given in array_diff() (line 194 of /var/www/html/mysite/web/modules/contrib/role_expire/role_expire.module).
#0 /var/www/html/mysite/web/modules/contrib/role_expire/role_expire.module(194): array_diff()
#1 [internal function]: role_expire_user_form_submit()
#2 /var/www/html/mysite/web/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array()
#3 /var/www/html/mysite/web/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers()
#4 /var/www/html/mysite/web/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm()
#5 /var/www/html/mysite/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm()
#6 /var/www/html/mysite/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm()
#7 [internal function]: Drupal\Core\Controller\FormController->getContentResult()
#8 /var/www/html/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#9 /var/www/html/mysite/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#10 /var/www/html/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext()
#11 /var/www/html/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#12 /var/www/html/mysite/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#13 /var/www/html/mysite/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#14 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle()
#15 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#16 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#17 /var/www/html/mysite/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#18 /var/www/html/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#19 /var/www/html/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#20 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#21 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#22 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#23 /var/www/html/mysite/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#24 /var/www/html/mysite/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#25 /var/www/html/mysite/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#26 {main}
🐛 Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @pearls
  • Status changed to Needs review 4 months ago
  • 🇮🇳India sarwan_verma

    Hi,

    I have resolved the 'array_diff(): Argument' issue and attached the patch.
    Please review and verify.

    Thanks.

  • Status changed to Postponed: needs info 4 months ago
  • 🇪🇸Spain rcodina Barcelona

    I need detailed steps to reproduce the problem.

  • Hi rcodina@

    When I changed some fields (for example first or last name - custom text field), the TypeError warning mentioned in the previous post appeared, even though I did not touch the role expiration date.
    This warning is gone after the patch .#2

  • Thanks @sarwan_verma,
    Patch is ok, it worked, the previous warning is gone. But just so you know, there are two php warnings in the log records. I don't know if it is related to the previous problem or the patch. But , it was one time warning. These two warnings did not appear in other changes.
    Just FYI.

  • Status changed to Needs work 4 months ago
  • 🇪🇸Spain rcodina Barcelona

    @pearls I recommend to use the latest 4.0.0 version.

    @sarwan_verma Thanks for the patch. Notice branch 3.x won't receive more updates.

  • Status changed to Needs review 4 months ago
  • 🇪🇸Spain rcodina Barcelona

    @pearls Are you using Role Delegation module?

  • 🇪🇸Spain rcodina Barcelona
  • Hi rcodina ,
    I use Role Delegation. Now I noticed a similar issue I had before with the Administer Users by Role module and Role expiration.
    Anyway.
    I tested Version 4. There's no TypeError: array_diff() ... issue mentioned before.
    15.patch (#8) worked without previous issue. Thanks for your effort.

    FYI : It was a bit hard to upgrade to v4. I don't use rules module. There were some problems with rules, role_expire_rules. So I completely uninstalled role expire and installed with a new config.
    Rules v 8.x-3.0-alpha8.
    Below is the log I captured when I tried to upgrade from 3.02 to V4 and update the site database.

    Drupal\Component\Plugin\Exception\PluginNotFoundException: The "request_path" plugin does not exist. Valid plugin IDs for Drupal\rules\Core\ConditionManager are: context, request_domain, request_path_exclusion, context_all, view_inclusion, user_status, http_status_code, rules_node_is_sticky, rules_entity_field_access, rules_user_is_blocked, rules_list_contains, rules_path_alias_exists, rules_entity_is_new, rules_data_is_empty, rules_data_comparison, rules_entity_has_field, rules_user_has_role, rules_node_is_published, rules_entity_is_of_type, rules_node_is_of_type, rules_path_has_alias, rules_list_count_is, rules_node_is_promoted, rules_text_comparison, rules_entity_is_of_bundle in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 53 of /var/www/html/d10test/web/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).
    
  • 🇪🇸Spain rcodina Barcelona

    @pearls With Role Expire 4.0.0 you may not see the error but the handling of roles while in combination with Role Delegation is wrong. So please, use the patch to avoid further issues.

    When updating module or core make sure you:

    drush cr
    drush updb -y
    drush cex -y

    So all hook_updates get executed and changes in configuration get stored.

    Thanks for the feedback!

  • 🇪🇸Spain rcodina Barcelona

    @pearls Follow the ECA integration progess on Make Rules integration a sub-module, provide ECA integration Active

  • 🇪🇸Spain rcodina Barcelona
  • 🇪🇸Spain rcodina Barcelona
  • 🇪🇸Spain rcodina Barcelona
  • Pipeline finished with Skipped
    4 months ago
    #256657
    • rcodina committed 124c686b on 4.x
      Issue #3467601 by rcodina, sarwan_verma, pearls:  Wrong handling of...
  • Status changed to Fixed 4 months ago
  • 🇪🇸Spain rcodina Barcelona
  • 🇪🇸Spain rcodina Barcelona
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024