function bakery_request_account in Bakery Single Sign-On System 7.2
Same name and namespace in other branches
- 6.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.
bool $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
mixed 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 1447 - Module file for the Bakery.
Code
function bakery_request_account($name, $or_email = FALSE) {
global $base_url;
$existing_account = user_load_by_name($name);
if (!$existing_account && $or_email) {
$account = user_load_by_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;
// Match how slaves are set on the master.
$payload['slave'] = rtrim($base_url, '/') . '/';
$payload['uid'] = $account->uid;
$payload['timestamp'] = $_SERVER['REQUEST_TIME'];
$payload['type'] = $type;
$data = bakery_bake_data($payload);
$payload = drupal_http_build_query(array(
$type => $data,
));
// Make request to master for account information.
$http_options = array(
'method' => 'POST',
'data' => $payload,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
),
);
$result = drupal_http_request($master . 'bakery/create', $http_options);
// 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($stub_uid);
return FALSE;
}
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($stub_uid);
return FALSE;
}
// Valid response. Fill in details from master.
$new_account = array(
'name' => $cookie['name'],
'pass' => user_password(),
'mail' => $cookie['mail'],
'init' => _bakery_init_field($cookie['uid']),
);
// Add any supported sync fields.
foreach (variable_get('bakery_supported_fields', array(
'mail' => 'mail',
'name' => 'name',
)) as $type => $enabled) {
if ($enabled && isset($cookie[$type])) {
$new_account[$type] = $cookie[$type];
}
}
// Create account.
$account = user_save($account, $new_account);
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($stub_uid);
return FALSE;
}