You are here

function _services_raw_user_create in Services Client 7

Same name and namespace in other branches
  1. 7.2 services_raw/services_raw.inc \_services_raw_user_create()

Create new user account using user_save

Parameters

$account: Account that should be created

1 string reference to '_services_raw_user_create'
_services_raw_services_resources in services_raw/services_raw.inc
Provides API definition of provided services objects and operations.

File

services_raw/services_raw.inc, line 684
Custom services definition and implementation of all callbacks.

Code

function _services_raw_user_create($account) {

  // Load original node resource
  module_load_include('inc', 'services', 'resources/user_resource');

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

  // Prevent re-writing user
  unset($account['uid']);

  // Check if user email already exists
  $exists = db_select('users', 'u')
    ->fields('u', array(
    'mail',
  ))
    ->condition('u.mail', $account['mail'])
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($exists) {
    return services_error(t("Error when saving user account."), 406, array(
      'error' => t('Account with specified email @mail already exists.', array(
        '@mail' => $account['mail'],
      )),
    ));
  }
  try {
    $account_raw = $account;
    if (!($account = user_save(NULL, $account))) {
      return services_error(t("Error when saving user account."), 406);
    }

    // @TODO: REMOVE
    watchdog('cci_services_raw', 'Create user: Retrieved: <pre>@retrieved</pre> Saved: <pre>@saved</pre>', array(
      '@retrieved' => print_r($account_raw, TRUE),
      '@saved' => print_r($account, TRUE),
    ), WATCHDOG_NOTICE);

    // @TODO: /REMOVE
    // Password is automatically rehashed by user module, manual sql update is required
    if (!empty($account_raw['pass'])) {
      db_update('users')
        ->fields(array(
        'pass' => $account_raw['pass'],
      ))
        ->condition('uid', $account->uid, '=')
        ->execute();
    }
  } catch (Exception $e) {
    return services_error(t("Error when saving user account."), 406, array(
      'error' => $e
        ->getMessage(),
    ));
  }
  $result = array(
    'uid' => $account->uid,
  );
  if ($uri = services_resource_uri(array(
    'user',
    $account->uid,
  ))) {
    $result['uri'] = $uri;
  }
  return $result;
}