You are here

function _hybridauth_popup_process_auth in HybridAuth Social Login 7

A helper function that takes a successful HA authentication and handles the Drupal side of things.

1 call to _hybridauth_popup_process_auth()
_hybridauth_popup_auth in ./hybridauth.pages.inc

File

./hybridauth.pages.inc, line 221

Code

function _hybridauth_popup_process_auth($hybridauth, $adapter, $profile, $provider_id) {
  global $user;
  $provider_name = hybridauth_get_provider_name($provider_id);
  $_SESSION['hybridauth']['profile'] = $profile;

  // Save provider info (for token replacement and account linking).
  $_SESSION['hybridauth_provider_info'] = array(
    'id' => $provider_id,
    'name' => $provider_name,
  );

  // Are we adding a new identity to an existing account?
  if (isset($_GET['add']) && $_GET['add']) {
    if (user_is_logged_in()) {
      return _hybridauth_popup_process_auth_addexisting($hybridauth, $adapter, $profile, $provider_id);
    }
  }

  // This wasn't a request to add to an existing logged-in account
  // Let's see if it matches an existing account that is not logged in.
  $account = user_external_load(_hybridauth_encode_authname($provider_id, $profile['identifier']));

  // Is this a registered user?
  if (isset($account->uid)) {
    if (!variable_get('user_email_verification', TRUE) || $account->login || !empty($account->data['hybridauth_data']['profile']['emailVerified']) && strtolower($account->data['hybridauth_data']['profile']['emailVerified']) == strtolower($account->mail)) {

      // IF settings do not require email verification
      // OR
      // it's not the first login for the user (which means the email has
      // already been verified)
      // OR
      // they are using an email the ID provider has already verified
      //
      // then we can skip the email verification process
      // Check that the user has not been 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);
      }
    }
    else {
      drupal_set_message(t('You must validate your email address for this account before logging in with it.'), 'error');
    }

    // Cleanly close popup and redirect
    $GLOBALS['devel_shutdown'] = FALSE;

    // Prevent devel module from spewing.
    $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user';
    drupal_add_js('
      if (window.opener){
        try { window.opener.parent.$.colorbox.close(); } catch(err) {}
        window.opener.parent.location.href = "' . url($destination, array(
      'absolute' => TRUE,
    )) . '";
      }
      window.self.close();
    ', 'inline');
    $page = array(
      'page_top' => '',
      '#children' => 'Closing...',
      'page_bottom' => '',
    );
    print theme('html', array(
      'page' => $page,
    ));
    drupal_exit();
  }
  else {

    // Check that users are allowed to register on their own.
    if (variable_get('user_register', 1)) {
      if (!variable_get('hybridauth_force_registration_form', FALSE)) {
        $form_state['values'] = array();
        $form_state['values']['op'] = t('Create new account');
        drupal_form_submit('user_register_form', $form_state);

        // See if the user was successfully registered.
        if (!empty($form_state['user'])) {

          // Let other modules know that a linked account has been added.
          $account = array(
            'user' => $user,
            'id' => _hybridauth_encode_authname($provider_id, $profile['identifier']),
            'provider_id' => $provider_id,
            'provider_name' => $provider_name,
          );
          module_invoke_all('hybridauth_linked_account', 'insert', $account);

          // Cleanly close popup and redirect
          $GLOBALS['devel_shutdown'] = FALSE;

          // Prevent devel module from spewing.
          $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user';
          drupal_add_js('
            if (window.opener){
              try { window.opener.parent.$.colorbox.close(); } catch(err) {}
              window.opener.parent.location.href = "' . url($destination, array(
            'absolute' => TRUE,
          )) . '";
            }
            window.self.close();
          ', 'inline');
          $page = array(
            'page_top' => '',
            '#children' => 'Closing...',
            'page_bottom' => '',
          );
          print theme('html', array(
            'page' => $page,
          ));
          drupal_exit();
        }

        // get the error messages and clear the messages queue
        $messages = drupal_get_messages('error');
        if (empty($form_state['values']['mail'])) {

          // If the idenitity provider did not provide an email address, ask
          // the user to complete (and submit) the form manually instead of
          // showing the error messages about the missing values generated by
          // FAPI.
          drupal_set_message(t('Although we have verified your account, @provider did not provide us with your e-mail address.  Please enter one below to complete your registration.  (If you\'ve previously registered with us, please <a href="@login">log in</a> and add your @provider account under "Linked accounts.")', array(
            '@provider' => $provider_name,
            '@login' => url('user/login'),
          )), 'warning');
        }
        else {
          drupal_set_message(t('Although we have verified your account, registration using the information provided by @provider failed due to the reasons listed below. Please complete the registration by filling out the form below. (If you\'ve previously registered with us, please <a href="@login">log in</a> and add your @provider account under "Linked accounts.")', array(
            '@provider' => $provider_name,
            '@login' => url('user/login'),
          )), 'warning');

          // Append form validation errors below the above warning.
          foreach ($messages['error'] as $message) {
            drupal_set_message($message, 'error');
          }
        }
      }
      else {
        drupal_set_message(t('Please complete the registration by filling out the form below.  (If you\'ve previously registered with us, please <a href="@login">log in</a> and add your @provider account under "Linked accounts.")', array(
          '@provider' => $provider_name,
          '@login' => url('user/login'),
        )), 'warning');
      }

      // Redirect to the normal registration page and prefill with the values
      // we received from HybridAuth.
      $destination = drupal_get_destination();
      unset($_GET['destination']);

      // Cleanly close popup and redirect
      $GLOBALS['devel_shutdown'] = FALSE;

      // Prevent devel module from spewing.
      drupal_add_js('
        if (window.opener){
          try { window.opener.parent.$.colorbox.close(); } catch(err) {}
          window.opener.parent.location.href = "' . url('user/register', array(
        'query' => $destination,
      )) . '";
        }
        window.self.close();
      ', 'inline');
      $page = array(
        'page_top' => '',
        '#children' => 'Closing...',
        'page_bottom' => '',
      );
      print theme('html', array(
        'page' => $page,
      ));
      drupal_exit();
    }
    else {
      drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');

      // Cleanly close popup and redirect
      $GLOBALS['devel_shutdown'] = FALSE;

      // Prevent devel module from spewing.
      $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user';
      drupal_add_js('
        if (window.opener){
          try { window.opener.parent.$.colorbox.close(); } catch(err) {}
          window.opener.parent.location.href = "' . url($destination, array(
        'absolute' => TRUE,
      )) . '";
        }
        window.self.close();
      ', 'inline');
      $page = array(
        'page_top' => '',
        '#children' => 'Closing...',
        'page_bottom' => '',
      );
      print theme('html', array(
        'page' => $page,
      ));
      drupal_exit();
    }
  }

  // We shouldn't get here, but just in case...
  return MENU_ACCESS_DENIED;
}