openid_authentication($response)Authenticate a user or attempt registration.
$response Response values from the OpenID Provider.
modules/openid/openid.module, line 530
<?php
function openid_authentication($response) {
module_load_include('inc', 'openid');
$identity = $response['openid.claimed_id'];
$account = user_external_load($identity);
if (isset($account->uid)) {
if (!variable_get('user_email_verification', TRUE) || $account->login) {
// Check if user is blocked.
$state['values']['name'] = $account->name;
user_login_name_validate(array(), $state);
if (!form_get_errors()) {
// Load global $user and perform final login tasks.
$form_state['uid'] = $account->uid;
user_login_submit(array(), $form_state);
// Let other modules act on OpenID login
module_invoke_all('openid_response', $response, $account);
}
}
else {
drupal_set_message(t('You must validate your email address for this account before logging in via OpenID'));
}
}
elseif (variable_get('user_register', 1)) {
// Register new user.
// Extract Simple Registration keys from the response.
$sreg_values = openid_extract_namespace($response, OPENID_NS_SREG, 'sreg');
// Extract Attribute Exchanges keys from the response.
$ax_values = openid_extract_namespace($response, OPENID_NS_AX, 'ax');
$form_state['build_info']['args'] = array();
$form_state['redirect'] = NULL;
if (!empty($sreg_values['nickname'])) {
// Use the nickname returned by Simple Registration if available.
$form_state['values']['name'] = $sreg_values['nickname'];
}
else if (!empty($ax_values['value.email'])) {
// Else, extract the name part of the email address returned by AX if available.
list ($name, $domain) = explode('@', $ax_values['value.email'], 2);
$form_state['values']['name'] = $name;
}
else {
$form_state['values']['name'] = '';
}
if (!empty($sreg_values['email'])) {
// Use the email returned by Simple Registration if available.
$form_state['values']['mail'] = $sreg_values['email'];
}
else if (!empty($ax_values['value.email'])) {
// Else, use the email returned by AX if available.
$form_state['values']['mail'] = $ax_values['value.email'];
}
else {
$form_state['values']['mail'] = '';
}
$form_state['values']['pass'] = user_password();
$form_state['values']['status'] = variable_get('user_register', 1) == 1;
$form_state['values']['response'] = $response;
if (empty($form_state['values']['name']) || empty($form_state['values']['mail'])) {
drupal_set_message(t('Complete the registration by filling out the form below. If you already have an account, you can <a href="@login">log in</a> now and add your OpenID under "My account".', array('@login' => url('user/login'))), 'warning');
$success = FALSE;
}
else {
$form = drupal_retrieve_form('user_register_form', $form_state);
drupal_prepare_form('user_register_form', $form, $form_state);
drupal_validate_form('user_register_form', $form, $form_state);
$success = !form_get_errors();
if (!$success) {
drupal_set_message(t('Account registration using the information provided by your OpenID provider failed due to the reasons listed below. Complete the registration by filling out the form below. If you already have an account, you can <a href="@login">log in</a> now and add your OpenID under "My account".', array('@login' => url('user/login'))), 'warning');
// Append form validation errors below the above warning.
$messages = drupal_get_messages('error');
foreach ($messages['error'] as $message) {
drupal_set_message( $message, 'error');
}
}
}
if (!$success) {
// We were unable to register a valid new user, redirect to standard
// user/register and prefill with the values we received.
$_SESSION['openid']['values'] = $form_state['values'];
// We'll want to redirect back to the same place.
$destination = drupal_get_destination();
unset($_GET['destination']);
drupal_goto('user/register', array('query' => $destination));
}
else {
unset($form_state['values']['response']);
$account = user_save(drupal_anonymous_user(), $form_state['values']);
// Terminate if an error occurred during user_save().
if (!$account) {
drupal_set_message(t("Error saving user account."), 'error');
drupal_goto();
}
user_set_authmaps($account, array("authname_openid" => $identity));
// Load global $user and perform final login tasks.
$form_state['uid'] = $account->uid;
user_login_submit(array(), $form_state);
// Let other modules act on OpenID login
module_invoke_all('openid_response', $response, $account);
}
drupal_redirect_form($form_state);
}
else {
drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
}
drupal_goto();
}
?>