You are here

function _user_resource_create in Services 7.3

Same name and namespace in other branches
  1. 6.3 resources/user_resource.inc \_user_resource_create()

Create a new user.

This function uses drupal_form_submit() and as such expects all input to match the submitting form in question.

Parameters

$account: A object containing account information. The $account object should contain, at minimum, the following properties:

  • name (user name)
  • mail (email address)
  • pass (plain text unencrypted password)

These properties can be passed but are optional

  • status (0 for blocked, otherwise will be active by default)
  • notify (1 to notify user of new account, will not notify by default)

Roles can be passed in a roles property which is an associative array formatted with '<role id>' => '<role id>', not including the authenticated user role, which is given by default.

Return value

The user object of the newly created user.

1 string reference to '_user_resource_create'
_user_resource_definition in resources/user_resource.inc

File

resources/user_resource.inc, line 328

Code

function _user_resource_create($account) {

  // Adds backwards compatability with regression fixed in #1083242
  $account = _services_arg_value($account, 'account');

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

  // Register a new user.
  $form_state['values'] = $account;

  // Determine the password(s). Passwords may not be available as this callback
  // is used for registration as well.
  $pass1 = '';
  $pass2 = '';
  if (isset($account['pass'])) {

    // For legacy usage, passwords come in as a single string. To match the
    // actual form state value keys used by Drupal, we also can collect two
    // passwords via an array.
    if (is_array($account['pass'])) {
      $pass1 = $account['pass']['pass1'];
      $pass2 = $account['pass']['pass2'];
    }
    else {
      $pass1 = $account['pass'];
      $pass2 = $account['pass'];
    }
  }
  $form_state['values']['pass'] = array(
    'pass1' => $pass1,
    'pass2' => $pass2,
  );

  // Set the form state op.
  $form_state['values']['op'] = variable_get('services_user_create_button_resource_create', t('Create new account'));

  // Execute the register form.
  $form_state['programmed_bypass_access_check'] = FALSE;
  drupal_form_submit('user_register_form', $form_state);

  // find and store the new user into the form_state
  if (isset($form_state['values']['uid'])) {
    $form_state['user'] = user_load($form_state['values']['uid']);
  }

  // Error if needed.
  if ($errors = form_get_errors()) {
    return services_error(implode(" ", $errors), 406, array(
      'form_errors' => $errors,
    ));
  }
  else {
    $user = array(
      'uid' => $form_state['user']->uid,
    );
    if ($uri = services_resource_uri(array(
      'user',
      $user['uid'],
    ))) {
      $user['uri'] = $uri;
    }
    _user_resource_update_services_user($user['uid'], time());
    return $user;
  }
}