You are here

function _hybridauth_window_process_auth in HybridAuth Social Login 6.2

Same name and namespace in other branches
  1. 7.2 hybridauth.pages.inc \_hybridauth_window_process_auth()

Handle the Drupal authentication.

2 calls to _hybridauth_window_process_auth()
hybridauth_additional_info_form_submit in ./hybridauth.pages.inc
_hybridauth_window_auth in ./hybridauth.pages.inc

File

./hybridauth.pages.inc, line 128

Code

function _hybridauth_window_process_auth($data) {
  global $user;

  // User is already logged in, tries to add new identity.
  if (user_is_logged_in()) {

    // Identity is already registered.
    if ($identity = _hybridauth_identity_load($data)) {

      // Registered to this user.
      if ($user->uid == $identity['uid']) {
        drupal_set_message(t('You have already registered this identity.'));
        _hybridauth_window_close();
      }
      else {
        drupal_set_message(t('This identity is registered to another user.'), 'error');
        _hybridauth_window_close();
      }
    }
    else {
      _hybridauth_identity_save($data);
      drupal_set_message(t('New identity added.'));

      // Invoke hybridauth_identity_added rules event.
      if (module_exists('rules')) {
        rules_invoke_event('hybridauth_identity_added', $user, $data);
      }
      _hybridauth_window_close();
    }
  }
  if ($identity = _hybridauth_identity_load($data)) {

    // Check if user is blocked.
    if ($account = _hybridauth_user_is_blocked_by_uid($identity['uid'])) {
      drupal_set_message(t('The username %name has not been activated or is blocked.', array(
        '%name' => $account->name,
      )), 'error');
    }
    elseif (!_hybridauth_user_login_access_by_uid($identity['uid'])) {
      $data = unserialize($identity['data']);
      drupal_set_message(t('You need to verify your e-mail address - !email.', array(
        '!email' => $data['email'],
      )), 'error');
      drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
      _user_mail_notify('register_no_approval_required', user_load($identity['uid']));
    }
    else {
      _hybridauth_user_login(user_load($identity['uid']), $data);
    }
  }
  elseif (variable_get('hybridauth_duplicate_emails', 1) && !empty($data['email']) && ($account = user_load(array(
    'mail' => $data['email'],
  )))) {

    // Add identity to existing account, only if emailVerified.
    if (variable_get('hybridauth_duplicate_emails', 1) == 2 && $data['email'] == $data['emailVerified']) {
      _hybridauth_identity_save($data, $account->uid);
      drupal_set_message(t('New identity added.'));

      // Invoke hybridauth_identity_added rules event.
      if (module_exists('rules')) {
        rules_invoke_event('hybridauth_identity_added', $account, $data);
      }
      _hybridauth_user_login($account, $data);
    }
    else {
      drupal_set_message(t('You are trying to login with email address of another user.'), 'error');
      if (!empty($account->hybridauth)) {
        $providers = hybridauth_providers_list();
        drupal_set_message(t('If you are completely sure it is your email address, try to login through %provider.', array(
          '%provider' => $providers[$account->hybridauth['provider']],
        )), 'status');
      }
      else {
        drupal_set_message(t('If you are completely sure it is your email address, try to login using your username and password on this site. If you don\'t remember your password - <a href="@password">request new password</a>.', array(
          '@password' => url('user/password'),
        )));
      }
    }
  }
  else {

    // Visitors can create accounts.
    if (!variable_get('hybridauth_register', 0) && variable_get('user_register', 1) || variable_get('hybridauth_register', 0)) {

      // Check profile information for required fields.
      _hybridauth_check_additional_info($data);

      //TODO: remove this global if possible
      global $hybridauth_data;
      $hybridauth_data = $data;

      // Register this new user.
      $name = _hybridauth_make_username($data);
      $userinfo = array(
        'name' => $name,
        'pass' => user_password(),
        'init' => $name,
        'status' => 1,
        'access' => time(),
        'mail' => $data['email'],
        //'data' => array('hybridauth' => $data),
        'hybridauth' => $data,
      );
      $admin_approval_required = FALSE;

      // Admin approval is required.
      if (!variable_get('hybridauth_register', 0) && variable_get('user_register', 1) == 2 || variable_get('hybridauth_register', 0) == 2) {
        $userinfo['status'] = 0;
        $admin_approval_required = TRUE;
      }
      $account = user_save(drupal_anonymous_user(), $userinfo);

      // Terminate if an error occurred during user_save().
      if (!$account) {
        drupal_set_message(t("Error saving user account."), 'error');
        _hybridauth_window_close();
      }

      // Invoke hybridauth_user_insert rules event.
      if (module_exists('rules')) {
        rules_invoke_event('hybridauth_user_insert', $account, $data);
      }
      _hybridauth_identity_save($data, $account->uid);

      // Invoke hybridauth_identity_added rules event.
      if (module_exists('rules')) {
        rules_invoke_event('hybridauth_identity_added', $account, $data);
      }
      $user_save_trigger = FALSE;
      $user_email_verify_trigger = FALSE;
      $user_login_trigger = TRUE;

      // Save user picture.
      if (variable_get('user_pictures', 0) && variable_get('hybridauth_pictures', 1)) {
        $photo_url = $data['photoURL'];
        if ($photo_url) {
          $photo = drupal_http_request($photo_url);
          $file = file_save_data($photo->data, file_directory_temp() . '/' . md5($photo_url), FILE_EXISTS_REPLACE);
          $info = image_get_info($file);
          $destination = variable_get('user_picture_path', 'pictures') . '/picture-' . $account->uid . '.' . $info['extension'];
          if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
            $edit['picture'] = $file;
            $user_save_trigger = TRUE;
          }
        }
      }

      // Admin approval is required.
      if ($admin_approval_required) {
        $user_login_trigger = FALSE;
        _user_mail_notify('register_pending_approval', $account);
        drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
      }
      elseif (!empty($data['email']) && $data['email'] != $data['emailVerified'] && (!variable_get('hybridauth_email_verification', 0) && variable_get('user_email_verification', TRUE) || variable_get('hybridauth_email_verification', 0) == 1)) {
        $user_login_trigger = FALSE;
        $edit['login'] = 280281600;

        //Dries birthday timestamp, Nov 19, 1978 :)
        $user_save_trigger = TRUE;
        $user_email_verify_trigger = TRUE;
      }
      if ($user_save_trigger) {

        // Hack to remove one notice from Legal module.

        /*if (module_exists('legal')) {
            $edit['legal_accept'] = NULL;
          }*/
        $account = user_save($account, $edit);
      }
      if ($user_email_verify_trigger) {
        _user_mail_notify('register_no_approval_required', $account);
        drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
      }

      // Log user in.
      if ($user_login_trigger) {
        _hybridauth_user_login($account, $data);
      }
    }
    else {
      drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
    }
  }
  _hybridauth_window_close();
}