You are here

function role_delegation_user_operations in Role Delegation 7

Same name and namespace in other branches
  1. 6 role_delegation.module \role_delegation_user_operations()

Implements hook_user_operations().

File

./role_delegation.module, line 251
This module allows site administrators to grant some roles the authority to change roles assigned to users, without them needing the 'administer access control' permission.

Code

function role_delegation_user_operations($form = array(), $form_state = array()) {

  // Only provide role add/remove operations when user can't assign permissions
  // without Role Delegation.
  if (user_access('administer permissions')) {
    return;
  }

  // Provide add/remove operations for delegated roles.
  $add_roles = array();
  $remove_roles = array();
  foreach (_role_delegation_roles() as $rid => $role) {
    if (user_access('assign all roles') || user_access("assign {$role} role")) {
      $add_roles['role_delegation_add_role-' . $rid]['label'] = t('Add role: !role', array(
        '!role' => $role,
      ));
      $remove_roles['role_delegation_remove_role-' . $rid]['label'] = t('Remove role: !role', array(
        '!role' => $role,
      ));
    }
  }
  $operations = $add_roles + $remove_roles;

  // If the form has been posted, insert the proper data for role editing.
  if (!empty($form_state['submitted'])) {
    $operation_rid = explode('-', $form_state['values']['operation']);
    $operation = $operation_rid[0];
    if ($operation == 'role_delegation_add_role' || $operation == 'role_delegation_remove_role') {
      if (array_key_exists($form_state['values']['operation'], $operations)) {
        $rid = $operation_rid[1];
        $operations[$form_state['values']['operation']] += array(
          // use the standard add_role and remove_role operations:
          'callback' => 'user_multiple_role_edit',
          'callback arguments' => array(
            str_replace('role_delegation_', '', $operation),
            $rid,
          ),
        );
      }
      else {
        watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
        return;
      }
    }
  }
  return $operations;
}