You are here

function openid_authentication in Drupal 7

Same name and namespace in other branches
  1. 6 modules/openid/openid.module \openid_authentication()

Authenticate a user or attempt registration.

Parameters

$response Response values from the OpenID Provider.:

1 call to openid_authentication()
openid_authentication_page in modules/openid/openid.pages.inc
Menu callback; Process an OpenID authentication.

File

modules/openid/openid.module, line 658
Implement OpenID Relying Party support for Drupal

Code

function openid_authentication($response) {
  $identity = $response['openid.claimed_id'];
  $account = user_external_load($identity);

  // Tries to load user account if user_external_load fails due to possibly
  // incompletely stored OpenID identifier in the authmap.
  if (!isset($account->uid) && variable_get('openid_less_obtrusive_transition', FALSE)) {
    module_load_include('inc', 'openid');
    $account = _openid_invalid_openid_transition($identity, $response);
  }
  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', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {

    // Register new user.
    // Save response for use in openid_form_user_register_form_alter().
    $_SESSION['openid']['response'] = $response;
    $form_state['values'] = array();
    $form_state['values']['op'] = t('Create new account');
    drupal_form_submit('user_register_form', $form_state);
    if (!empty($form_state['user'])) {
      module_invoke_all('openid_response', $response, $form_state['user']);
      drupal_goto();
    }
    $messages = drupal_get_messages('error');
    if (empty($form_state['values']['name']) || empty($form_state['values']['mail'])) {

      // If the OpenID provider did not provide both a user name and an email
      // address, ask the user to complete the registration manually instead of
      // showing the error messages about the missing values generated by FAPI.
      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');
    }
    else {
      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.
      foreach ($messages['error'] as $message) {
        drupal_set_message($message, 'error');
      }
    }

    // We were unable to register a valid new user. Redirect to the normal
    // registration page and prefill with the values we received.
    $destination = drupal_get_destination();
    unset($_GET['destination']);
    drupal_goto('user/register', array(
      'query' => $destination,
    ));
  }
  else {
    drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
  }
  drupal_goto();
}