Creating Custom User Admin Actions In Drupal 7 Organic Groups

26th January 2013 - 7 minutes read time

Organic Groups (OG) in Drupal 7 has a role based permission system that works on a group by group basis. This permissions system works separately to the main Drupal permission system, which can cause a couple of issues. For example, if you want to give a group role access to give other users roles then you'll need to give them the 'Administer groups' permission. The downside of this is that it overrides Drupal's core permissions to do with node deletion and allows the role to delete the group. Allowing any user to delete groups can lead to all sorts of problems so an alternative is needed.

The group people admin page (found at group/node/%nid%/admin/people) has a bulk operations form that allows users with access to the form to manage user group membership. To allow or deny a member a user just needs to select them from the list, select the action required and click Update. Here is the select statement from that page.

  1. <select>
  2. <option value="unblock" selected="selected">Approve membership of the selected users</option>
  3. <option value="deny">Deny membership from the selected users</option>
  4. <option value="block">Block membership from the selected users</option>
  5. </select>

The Administer group permission also adds a few options to this action list to allow them to assign roles to particular users. It is possible to add options to this list by using the hook_og_user_operations() hook. The normal contents of the people admin page is controlled via the function og_ui_og_user_operations(), which is an implementation of this hook. The function creates an array of operations and returns this.

What I wanted to do was add a couple of options to this list so that users who had the group permission 'update group' but didn't have the group permission 'administer group' would be able to give other users the role of 'group leader'. This is just a special role that allows them to update the group and administer people within the group.

The easiest way to accomplish this is to create an associative array of operations. Each array contains a 'label' for the option and a 'callback' for the function that is used to process the action of the command. The following function adds two elements to the select list, one for adding the role 'group leader' and one for taking it away again.

  1. /**
  2.  * Implements hook_og_user_operations().
  3.  */
  4. function mymodule_og_user_operations($form = array(), $form_state = array()) {
  5. $operations = array();
  6.  
  7. if (og_user_access($gid, 'update group') && !og_user_access($gid, 'administer group')) {
  8. 'group_lead_grant' => array(
  9. 'label' => t('Grant group lead role to user'),
  10. 'callback' => 'group_leader_grant',
  11. ),
  12. 'group_lead_revoke' => array(
  13. 'label' => t('Revoke group lead role to user'),
  14. 'callback' => 'group_leader_revoke',
  15. )
  16. }
  17.  
  18. return $operations;
  19. }

This creates a select list like this.

  1. <select>
  2. <option value="unblock" selected="selected">Approve membership of the selected users</option>
  3. <option value="deny">Deny membership from the selected users</option>
  4. <option value="block">Block membership from the selected users</option>
  5. <option value="group_lead_grant">Grant group lead role to user </option>
  6. <option value="group_lead_revoke"> Revoke group lead role to user </option>
  7. </select>

The callback functions for the above operations take the group ID (not the node ID) and an array of user ID's as a default. These functions use a function I wrote to load organic group roles by their name, rather than using the gid. There is also a small check to make sure that a user doesn't revoke their own role.

  1. /**
  2.  * Callback function from mymodule_og_user_operations().
  3.  */
  4. function group_leader_grant($gid, $uids) {
  5. if (og_user_access($gid, 'update group')) {
  6. $accounts = user_load_multiple($uids);
  7. $group = mymodule_og_roles_by_name('group leader');
  8. foreach ($accounts as $account) {
  9. og_role_grant($gid, $account->uid, $group['rid']);
  10. }
  11. }
  12. }
  13.  
  14. /**
  15.  * Callback function from mymodule_og_user_operations().
  16.  */
  17. function group_leader_revoke($gid, $uids) {
  18. global $user;
  19.  
  20. if (og_user_access($gid, 'update group')) {
  21. $accounts = user_load_multiple($uids);
  22. $group = mymodule_og_roles_by_name('group leader');
  23. foreach ($accounts as $account) {
  24. if ($user->uid == $account->uid) {
  25. form_set_error('', "You can't revoke your own user account!");
  26. break;
  27. }
  28. og_role_revoke($gid, $account->uid, $group['rid']);
  29. }
  30. }
  31. }

It is also possible to create a group of options by passing an associative array of items as the label. The index of the array is the group label in the select list. This approach is slightly more complex as it requires assigning the correct action from within the hook function itself. The index of the label array that was selected is passed to the function in the $form_state and an action is used to decide on what action to take. You can either add the code into this function, use the same callback functions as before or even use a single callback function that accepts arguments.

The following function implements a group of options that allow the user to select an action, and then defines a callback function to run in order to process the change.

  1. /**
  2.  * Implements hook_og_user_operations().
  3.  */
  4. function mymodule_og_user_operations($form = array(), $form_state = array()) {
  5. $operations = array();
  6.  
  7. if (og_user_access($gid, 'update group') && !og_user_access($gid, 'administer group')) {
  8. $operations['Group leader administration'] = array(
  9. 'label' => array(
  10. 'group_leader_grant' => 'Grant group leader status',
  11. 'group_leader_revoke' => 'Revoke group leader status'
  12. )
  13. );
  14. }
  15.  
  16. if (!empty($form_state['submitted'])) {
  17. switch ($form_state['values']['operation']) {
  18. case 'group_leader_grant':
  19. $operations[$form_state['values']['operation']] = array(
  20. 'callback' => 'group_leader_role_edit',
  21. 'callback arguments' => array('grant'),
  22. );
  23. break;
  24. case 'group_leader_revoke':
  25. $operations[$form_state['values']['operation']] = array(
  26. 'callback' => 'group_leader_role_edit',
  27. 'callback arguments' => array('revoke'),
  28. );
  29. break;
  30. }
  31. }
  32.  
  33. return $operations;
  34. }

This creates a select list like the following.

  1. <select>
  2. <option value="unblock" selected="selected">Approve membership of the selected users</option>
  3. <option value="deny">Deny membership from the selected users</option>
  4. <option value="block">Block membership from the selected users</option>
  5. <optgroup label="Group leader administration">
  6. <option value="group_leader_grant">Grant group leader status </option>
  7. <option value="group_leader_revoke">Revoke group leader status </option>
  8. </optgroup>
  9. </select>

Here is the callback function used in the above hook. It is a single function that will either grant or revoke the group leader role to a user depending on what operation was selected from the mymodule_og_user_operations() function.

  1. function group_leader_role_edit($gid, $uids, $op) {
  2. global $user;
  3.  
  4. if (og_user_access($gid, 'update group')) {
  5. $accounts = user_load_multiple($uids);
  6. $group = mymodule_og_roles_by_name('group leader');
  7. foreach ($accounts as $account) {
  8. switch ($op) {
  9. case 'grant':
  10. og_role_grant($gid, $account->uid, $group['rid']);
  11. break;
  12. case 'revoke':
  13. if ($user->uid == $account->uid) {
  14. form_set_error('', "You can't revoke your own user account!");
  15. break;
  16. }
  17. og_role_revoke($gid, $account->uid, $group['rid']);
  18. break;
  19. }
  20. }
  21. }
  22. }

With this in place I can now setup the group role permissions correctly so that users can't delete groups but can manage user roles within their group.

Add new comment

The content of this field is kept private and will not be shown publicly.