Download & Extend

User Management Batch Update Options should include assign and remove role

Project:Role Delegation
Version:7.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

On a default Drupal install, users with 'administer permissions' and 'administer users' permissions can go to Administer » User management (/admin/user/user), select multiple users, and then either add or remove roles by using the "Update options" drop down and selecting a role under "Add a role to the selected users" or "Remove a role from the selected users".

However, with Role Delegation this is not possible. Instead, the individual users accounts have to be edited one at a time (on the user edit pages /user/[uid]/edit).

The RoleAssign module is able to implement this functionality, so I assume it must be possible for a module to do. Also, since Role Delegation claims to supersede RoleAssign, implementing this functionality is important in order for that claim to remain true.

I'm happy to provide more info or test patches if needed. Thanks!

Comments

#1

shark, I agree that this is an essential feature.

I'm attaching a patch that I believe implements this. Please review and test. After applying the patch, be sure to clear your Drupal cache before going to admin/user/user to see the new batch operations.

Note to role_delegation maintainer(s): I borrowed most of role_delegation_user_operations() from user_user_operations() in user.module.

AttachmentSize
role_delegation_batch_update.patch 2.09 KB

#2

Status:active» needs review

#3

Status:needs review» needs work

Hm. Although this works, it causes the user module to emit a 'Detected malicious attempt to alter protected user fields' warning in the log. I'll look into this.

#4

OK, here's a revised patch that fixes the log warnings. I had to change the operation names from e.g. add_role to role_delegation_add_role, so that user_user_operations() wouldn't worry about the permissions. Please test.

AttachmentSize
role_delegation_batch_update-2.patch 2.36 KB

#5

Status:needs work» needs review

#6

I tested the patch in #4. Works great!

Thanks Andrew.

#7

Status:needs review» reviewed & tested by the community

#8

Comment cleanups.

AttachmentSize
role_delegation_batch_update.patch 2.33 KB

#9

This patch is tested and ready. Can we get it incorporated into a new release? Thanks, Andrew.

#10

Version:6.x-1.2» 7.x-1.x-dev
Status:reviewed & tested by the community» patch (to be ported)

Committed to the DRUPAL-6--1 branch. Thanks for not giving up! :)

This will need to be ported to Drupal 7.

#11

Status:patch (to be ported)» needs review

Here's a port of this feature to D7.

The approach I used here for providing the operations is a little different than in D6. In the D6 patch I submitted, role_delegation_user_operations() returns a nested array of operations, e.g.

array(
  'Add a role to the selected users' => array(
    'role_delegation_add_role-1' => 'Role 1',
  ),
  'Remove a role from the selected users' => array(
    'role_delegation_remove_role-1' => 'Role 1',
  ),
)

This is actually not right. The API for hook_user_operations (not available for D7 yet, but it's the same) is clear that hook_user_operations() should only return a simple array of operations, not a nested array. However the above works, because in the form API, form_select_options() does accept nested arrays of options, which it renders as optgroups.

A better approach is to return a simple array, as the API requires, of properly labeled operations, e.g.

array(
  'role_delegation_add_role-1' => 'Add role: Role 1',
  'role_delegation_remove_role-1' => 'Remove role: Role 1',
)

Then separately alter the user bulk update form to present these as nested options, as before. The user sees the same thing, but we follow the API, so the operations can also be used elsewhere (e.g. in VBO) and be properly labeled there.

This is what I did for D7. The correction could also be applied in D6, but it's probably not worth the trouble since the existing code works.

AttachmentSize
role_delegation_batch_update_D7.patch 3.44 KB

#12

Here's a slightly corrected patch:

  • Still checks for unauthorized role assignment even if the operation list is empty.
  • Checks array_key_exists() so as not to cause an 'undefined index' error in case of a watchdog warning.
AttachmentSize
role_delegation_batch_update_D7_2.patch 3.42 KB

#13

Status:needs review» fixed

Fixed committed in 7.x-1.x-dev.

#14

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.