You are here

function user_service_save in Services 6.2

Same name and namespace in other branches
  1. 6 services/user_service/user_service.inc \user_service_save()
  2. 7 services/user_service/user_service.inc \user_service_save()

Save user data.

This can create a new user or modify an existing user, depending on the information passed to the function. This function uses drupal_execute() and as such exepects all input to match the submitting form in question.

Parameters

$account: An array of account information.

Return value

The UID of the user whose information was saved.

1 string reference to 'user_service_save'
user_service_service in services/user_service/user_service.module
Implementation of hook_service().

File

services/user_service/user_service.inc, line 147
Link general user functionalities to services module.

Code

function user_service_save($account) {

  // Load the required includes for saving profile information
  // with drupal_execute().
  module_load_include('inc', 'user', 'user.pages');

  // if uid is present then update, otherwise insert
  $update = user_load($account['uid']);
  $form_state = array();

  // Any logged in user is by default authenticated,
  // and leaving this role set in the user's roles array
  // causes big problems because of a FAPI hack that controls
  // this checkbox on the user create and edit form (and thus
  // causes problems with drupal_execute()). Therefore we just
  // force it to 0 here.
  if (isset($account['roles'][2])) {
    $account['roles'][2] = 0;
  }
  if (!isset($update->uid)) {

    // register a new user
    $form_state['values'] = $account;
    $form_state['values']['pass'] = array(
      'pass1' => $account['pass'],
      'pass2' => $account['pass'],
    );
    $form_state['values']['op'] = t('Create new account');

    // reset to anonymous user
    global $user;
    $orig_user = $user;
    session_save_session(FALSE);
    $user = user_load(array(
      'uid' => 0,
    ));

    // execute the register form
    drupal_execute('user_register', $form_state);

    // find and store the new user into the form_state
    $form_state['user'] = user_load(array(
      'name' => $form_state['values']['name'],
    ));

    // revert back to the original user.
    $user = $orig_user;
    session_save_session(TRUE);
  }
  else {

    // If a profile category was passed in, use it. Otherwise default
    // to 'account' (for saving core user data.)
    $category = 'account';
    if (isset($account['category'])) {
      $category = $account['category'];
      unset($account['category']);
    }

    // Drop any passed in values into the $account var. Anything
    // unused by the form just gets ignored.
    foreach ($account as $key => $value) {
      $form_state['values'][$key] = $value;
    }
    $form_state['values']['op'] = 'Save';
    $form_state['values']['_category'] = $category;
    $form_state['values']['_account'] = $account;
    $ret = drupal_execute('user_profile_form', $form_state, (object) $account, $category);
  }

  // Error if needed.
  if ($errors = form_get_errors()) {
    return services_error(implode("\n", $errors), 401);
  }
  else {
    return $form_state['user']->uid;
  }
}