function role_delegation_user in Role Delegation 6
Same name and namespace in other branches
- 5 role_delegation.module \role_delegation_user()
Implementation of hook_user().
File
- ./
role_delegation.module, line 216 - This module allows site administrators to grant some roles the authority to assign selected roles to users, without them needing the 'administer access control' permission.
Code
function role_delegation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'register' || $op == 'form' && $category == 'account') {
// Only alter user form when user can't assign permissions without Role Delegation.
if (!user_access('administer permissions')) {
// Split up roles based on whether they can be delegated or not.
$current_roles = isset($account->roles) ? $account->roles : array();
$rids_default = array();
$rids_preserve = array(
DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
);
$roles_preserve = array(
'authenticated user',
);
$roles_options = array();
$roles = _role_delegation_roles();
foreach ($roles as $rid => $role) {
if (user_access('assign all roles') || user_access(_role_delegation_make_perm($role))) {
if (array_key_exists($rid, $current_roles)) {
$rids_default[] = $rid;
}
$roles_options[$rid] = $role;
}
else {
if (array_key_exists($rid, $current_roles)) {
$rids_preserve[$rid] = $rid;
$roles_preserve[] = $role;
}
}
}
if (empty($roles_options)) {
// No role can be assigned.
return;
}
// Generate the form items.
$form['roles_preserve'] = array(
'#type' => 'value',
'#value' => $rids_preserve,
);
$roles_assign = array(
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#description' => t('The user receives the combined permissions of the %roles role(s), and all roles selected here. ', array(
'%roles' => implode(', ', $roles_preserve),
)),
'#options' => $roles_options,
'#default_value' => $rids_default,
'#weight' => 10,
);
if ($op == 'register') {
// Since the user module does array_merge() instead of array_merge_recursive() (see bug http://drupal.org/node/227690),
// we must move this under 'account' later at role_delegation_form_user_register_alter()
$form['roles_assign'] = $roles_assign;
}
else {
$form['account']['roles_assign'] = $roles_assign;
}
return $form;
}
}
elseif (isset($edit['roles_assign']) && ($op == 'insert' || $op == 'submit')) {
$edit['roles'] = $edit['roles_preserve'] + array_filter($edit['roles_assign']);
}
}