On this page
function user_multiple_role_edit
user_multiple_role_edit($accounts, $operation, $rid)
Callback function for admin mass adding/deleting a user role.
File
- modules/user/user.module, line 3305
- 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_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchField();
switch ($operation) {
case 'add_role':
$accounts = user_load_multiple($accounts);
foreach ($accounts as $account) {
// 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);
// For efficiency manually save the original account before applying
// any changes.
$account->original = clone $account;
user_save($account, array('roles' => $roles));
}
}
break;
case 'remove_role':
$accounts = user_load_multiple($accounts);
foreach ($accounts as $account) {
// 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));
// For efficiency manually save the original account before applying
// any changes.
$account->original = clone $account;
user_save($account, array('roles' => $roles));
}
}
break;
}
}
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/modules!user!user.module/function/user_multiple_role_edit/7.x