You are here

function bakery_request_account in Bakery Single Sign-On System 6.2

Same name and namespace in other branches
  1. 7.2 bakery.module \bakery_request_account()

Request account information from master to create account locally.

Parameters

string $name the username or e-mail to request information for to create.:

boolean $or_email load account by name or email. Useful for getting: account data from a password request where you get name or email.

Return value

The newly created local UID or FALSE.

3 calls to bakery_request_account()
bakery_pull_form_submit in ./bakery.module
If the request succeeds, go to the user page. Otherwise, show an error.
_bakery_pass_validate in ./bakery.module
Validate handler for the password reset login.
_bakery_taste_chocolatechip_cookie in ./bakery.module
Test identification cookie.

File

./bakery.module, line 1352

Code

function bakery_request_account($name, $or_email = FALSE) {
  global $base_url;
  $existing_account = user_load(array(
    'name' => $name,
  ));
  if (!$existing_account && $or_email) {
    $existing_account = user_load(array(
      'mail' => $name,
    ));
  }

  // We return FALSE in cases that the account already exists locally or if
  // there was an error along the way of requesting and creating it.
  if ($existing_account) {
    return FALSE;
  }
  $master = variable_get('bakery_master', 'http://drupal.org/');
  $key = variable_get('bakery_key', '');

  // Save a stub account so we have a slave UID to send.
  $new_account = array(
    'name' => $name,
    'pass' => user_password(),
    'status' => 1,
    'init' => 'bakery_temp/' . mt_rand(),
  );
  $account = user_save(NULL, $new_account);
  if (!$account) {
    watchdog('bakery', 'Unable to create stub account for @name', array(
      '@name' => $name,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  $stub_uid = $account->uid;
  $type = 'gingerbread';
  $payload = array();
  $payload['name'] = $name;
  $payload['or_email'] = $or_email;
  $payload['slave'] = rtrim($base_url, '/') . '/';

  // Match how slaves are set on the master.
  $payload['uid'] = $account->uid;
  $payload['timestamp'] = $_SERVER['REQUEST_TIME'];
  $payload['type'] = $type;
  $data = bakery_bake_data($payload);
  $payload = drupal_query_string_encode(array(
    $type => $data,
  ));

  // Make request to master for account information.
  $result = drupal_http_request($master . 'bakery/create', array(
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
  ), 'POST', $payload);

  // Parse result and create account.
  if ($result->code != 200) {
    $message = $result->data;
    watchdog('bakery', 'Received response !code from master with message @message', array(
      '!code' => $result->code,
      '@message' => $message,
    ), WATCHDOG_ERROR);
    user_delete(array(), $stub_uid);
    return FALSE;
  }

  // Validate response.
  if (($cookie = bakery_validate_data($result->data)) === FALSE) {

    // Invalid response.
    watchdog('bakery', 'Invalid response from master when attempting to create local account for @name', array(
      '@name' => $name,
    ), WATCHDOG_ERROR);
    user_delete(array(), $stub_uid);
    return FALSE;
  }

  // Fill in details from master and create account.
  $account = user_save($account, array_merge(bakery_prepare_fields($cookie, $account), array(
    'pass' => user_password(),
    'init' => _bakery_init_field($cookie['uid']),
  )));
  if ($account) {
    watchdog('bakery', 'Created account for @name', array(
      '@name' => $name,
    ));

    // Invoke hook_bakery_receive().
    module_invoke_all('bakery_receive', $account, $cookie);
    return $account->uid;
  }
  watchdog('bakery', 'Unable to create account for @name', array(
    '@name' => $name,
  ), WATCHDOG_ERROR);
  user_delete(array(), $stub_uid);
  return FALSE;
}