You are here

function autoassignrole_user_presave in Auto Assign Role 7.2

Same name and namespace in other branches
  1. 7 autoassignrole.module \autoassignrole_user_presave()

Implements hook_user_presave().

File

./autoassignrole.module, line 228
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_roles()) {

    // 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, _autoassignrole_convert_roles(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, _autoassignrole_convert_roles($edit['user_roles']));
      }

      // Add page-specific roles.
      $page_roles = autoassignrole_get_active_path_roles();
      if ($page_roles) {
        $roles_to_add = array_intersect_key($user_roles, _autoassignrole_convert_roles($page_roles));
      }

      // 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'];
    }
  }
}