user_multiple_role_edit

5 user.module user_multiple_role_edit($accounts, $operation, $rid)
6 user.module user_multiple_role_edit($accounts, $operation, $rid)
7 user.module user_multiple_role_edit($accounts, $operation, $rid)
8 user.module user_multiple_role_edit($accounts, $operation, $rid)

Callback function for admin mass adding/deleting a user role.

1 string reference to 'user_multiple_role_edit'

File

modules/user/user.module, line 1886
Enables the user registration and login system.

Code

function user_multiple_role_edit($accounts, $operation, $rid) {
  // The role name is not necessary as user_save() will reload the user
  // object, but some modules' hook_user() may look at this first.
  $role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $rid));

  switch ($operation) {
    case 'add_role':
      foreach ($accounts as $uid) {
        $account = user_load(array('uid' => (int) $uid));
        // Skip adding the role to the user if they already have it.
        if ($account !== FALSE && !isset($account->roles[$rid])) {
          $roles = $account->roles + array($rid => $role_name);
          user_save($account, array('roles' => $roles));
        }
      }
      break;
    case 'remove_role':
      foreach ($accounts as $uid) {
        $account = user_load(array('uid' => (int) $uid));
        // Skip removing the role from the user if they already don't have it.
        if ($account !== FALSE && isset($account->roles[$rid])) {
          $roles = array_diff($account->roles, array($rid => $role_name));
          user_save($account, array('roles' => $roles));
        }
      }
      break;
  }
}

Comments

$accounts

Note that the accounts variable we are passing is an array of uids, not user objects.

Login or register to post comments