function user_role_save

Save a user role to the database.

Parameters

$role: A role object to modify or add.

Return value

Status constant indicating if role was created or updated. Failure to write the user role record will return FALSE. Otherwise SAVED_NEW or SAVED_UPDATED is returned depending on the operation performed.

4 calls to user_role_save()
standard_install in drupal/core/profiles/standard/standard.install
Implements hook_install().
user_admin_roles_order_submit in drupal/core/modules/user/user.admin.inc
Form submit function. Update the role weights.
user_admin_role_submit in drupal/core/modules/user/user.admin.inc
Form submit handler for the user_admin_role() form.
WebTestBase::drupalCreateRole in drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
Internal helper function; Create a role with specified permissions.

File

drupal/core/modules/user/user.module, line 2120
Enables the user registration and login system.

Code

function user_role_save($role) {
  if ($role->name) {

    // Prevent leading and trailing spaces in role names.
    $role->name = trim($role->name);
  }
  if (!isset($role->weight)) {

    // Set a role weight to make this new role last.
    $query = db_select('role');
    $query
      ->addExpression('MAX(weight)');
    $role->weight = $query
      ->execute()
      ->fetchField() + 1;
  }

  // Let modules modify the user role before it is saved to the database.
  module_invoke_all('user_role_presave', $role);
  $exists = db_select('role', 'r')
    ->fields('r', array(
    'rid',
  ))
    ->condition('rid', $role->rid)
    ->execute()
    ->fetchAll();
  if (empty($exists)) {
    $status = drupal_write_record('role', $role);
    module_invoke_all('user_role_insert', $role);
  }
  else {
    $status = drupal_write_record('role', $role, 'rid');
    module_invoke_all('user_role_update', $role);
  }

  // Clear the user access cache.
  drupal_static_reset('user_access');
  drupal_static_reset('user_role_permissions');
  return $status;
}