Same name and namespace in other branches
  1. 4.7.x modules/user.module \user_save()
  2. 5.x modules/user/user.module \user_save()
  3. 6.x modules/user/user.module \user_save()
  4. 7.x modules/user/user.module \user_save()

Save changes to a user account.

Parameters

$account: The $user object for the user to modify.

$array: An array of fields and values to save. For example array('name' => 'My name'); Setting a field to null deletes it from the data column.

$category: (optional) The category for storing profile information in.

5 calls to user_save()
update_108 in database/updates.inc
update_80 in database/updates.inc
user_admin_create in modules/user.module
user_pass in modules/user.module
user_register in modules/user.module

File

modules/user.module, line 96
Enables the user registration and login system.

Code

function user_save($account, $array = array(), $category = 'account') {

  // Dynamically compose a SQL query:
  $user_fields = user_fields();
  if ($account->uid) {
    user_module_invoke('update', $array, $account, $category);
    $data = unserialize(db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $account->uid)));
    foreach ($array as $key => $value) {
      if ($key == 'pass') {
        $query .= "{$key} = '%s', ";
        $v[] = md5($value);
      }
      else {
        if (substr($key, 0, 4) !== 'auth') {
          if (in_array($key, $user_fields)) {

            // Save standard fields
            $query .= "{$key} = '%s', ";
            $v[] = $value;
          }
          else {
            if ($key != 'roles') {

              // Roles is a special case: it used below.
              if ($value === null) {
                unset($data[$key]);
              }
              else {
                $data[$key] = $value;
              }
            }
          }
        }
      }
    }
    $query .= "data = '%s', ";
    $v[] = serialize($data);
    db_query("UPDATE {users} SET {$query} changed = %d WHERE uid = %d", array_merge($v, array(
      time(),
      $account->uid,
    )));

    // Reload user roles if provided
    if (is_array($array['roles'])) {
      db_query('DELETE FROM {users_roles} WHERE uid = %d', $account->uid);
      foreach (array_keys($array['roles']) as $rid) {
        db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
      }
    }

    // Delete a blocked user's sessions to kick them if they are online.
    if (isset($array['status']) && $array['status'] == 0) {
      db_query('DELETE FROM {sessions} WHERE uid = %d', $account->uid);
    }

    // Refresh user object
    $user = user_load(array(
      'uid' => $account->uid,
    ));
  }
  else {
    $array['created'] = time();
    $array['changed'] = time();
    $array['uid'] = db_next_id('{users}_uid');

    // Note, we wait with saving the data column to prevent module-handled
    // fields from being saved there. We cannot invoke hook_user('insert') here
    // because we don't have a fully initialized user object yet.
    foreach ($array as $key => $value) {
      if ($key == 'pass') {
        $fields[] = db_escape_string($key);
        $values[] = md5($value);
        $s[] = "'%s'";
      }
      else {
        if (substr($key, 0, 4) !== 'auth') {
          if (in_array($key, $user_fields)) {
            $fields[] = db_escape_string($key);
            $values[] = $value;
            $s[] = "'%s'";
          }
        }
      }
    }
    db_query('INSERT INTO {users} (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $s) . ')', $values);

    // Reload user roles (delete just to be safe).
    db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']);
    foreach ($array['roles'] as $rid) {
      db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid);
    }

    // Build the initial user object.
    $user = user_load(array(
      'uid' => $array['uid'],
    ));
    user_module_invoke('insert', $array, $user, $category);

    // Build and save the serialized data field now
    $data = array();
    foreach ($array as $key => $value) {
      if (substr($key, 0, 4) !== 'auth' && !in_array($key, $user_fields) && $value !== null) {
        $data[$key] = $value;
      }
    }
    db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);

    // Build the finished user object.
    $user = user_load(array(
      'uid' => $array['uid'],
    ));
  }

  // Save distributed authentication mappings
  foreach ($array as $key => $value) {
    if (substr($key, 0, 4) == 'auth') {
      $authmaps[$key] = $value;
    }
  }
  if ($authmaps) {
    user_set_authmaps($user, $authmaps);
  }
  return $user;
}