function autoassignrole_user_presave in Auto Assign Role 7
Same name and namespace in other branches
- 7.2 autoassignrole.module \autoassignrole_user_presave()
Implements hook_user_presave().
File
- ./
autoassignrole.module, line 183 - The main autoassignrole.module file
Code
function autoassignrole_user_presave(&$edit, $account, $category) {
// Use case http://drupal.org/node/971622
// Make sure we only assign roles automatically when enabled to do so.
if (variable_get('autoassignrole_auto_active', 0) || variable_get("autoassignrole_user_active", 0) || variable_get('autoassignrole_admin_active', 0) && user_access('administer users') || autoassignrole_get_active_path_rid()) {
// Use case http://drupal.org/node/944864
// Only assign roles if this is a new account.
if (isset($account->is_new) && !empty($account->is_new)) {
// Get the existing user roles with the exception of the anonymous role.
$user_roles = user_roles(TRUE);
$roles_to_add = array();
// Add in automatic roles.
if (variable_get('autoassignrole_auto_active', 0) && !user_access('administer users') || variable_get('autoassignrole_admin_active', 0) && user_access('administer users')) {
$roles_to_add += array_intersect_key($user_roles, array_filter(variable_get('autoassignrole_auto_roles', array())));
}
// Add in user selected roles if any.
if (variable_get("autoassignrole_user_active", 0) && !empty($edit['user_roles'])) {
// Value is a single, convert to array.
if (!is_array($edit['user_roles'])) {
$edit['user_roles'] = array(
$edit['user_roles'] => $edit['user_roles'],
);
}
$roles_to_add += array_intersect_key($user_roles, array_filter($edit['user_roles']));
}
// Add page-specific roles.
$page_rids = autoassignrole_get_active_path_rid();
if ($page_rids) {
$roles_to_add = array_intersect_key($user_roles, array_filter($page_rids));
}
// Make sure the roles key exists.
if (!isset($edit['roles'])) {
$edit['roles'] = array();
}
// Add in the new roles to override the current roles.
$edit['roles'] = $roles_to_add + $edit['roles'];
}
}
}