You are here

function _hybridauth_popup_process_auth_addexisting 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_addexisting()
_hybridauth_popup_process_auth in ./hybridauth.pages.inc
A helper function that takes a successful HA authentication and handles the Drupal side of things.

File

./hybridauth.pages.inc, line 403

Code

function _hybridauth_popup_process_auth_addexisting($hybridauth, $adapter, $profile, $provider_id) {
  global $user;

  // The identifier given should not be already mapped to an existing account
  if (user_get_authmaps(_hybridauth_encode_authname($provider_id, $profile['identifier']))) {
    $message = array(
      'text' => t('We were unable to complete your request.  That account ID is already linked to a user on this site.'),
      'type' => 'error',
    );
  }
  else {
    $txn = db_transaction();
    try {

      // Can't use user_set_authmaps() here, since it doesn't support multiple authnames per user via same module
      $result1 = $aid = db_insert('authmap')
        ->fields(array(
        'uid' => $user->uid,
        'authname' => _hybridauth_encode_authname($provider_id, $profile['identifier']),
        'module' => 'hybridauth',
      ))
        ->execute();
      $result2 = db_insert('hybridauth_account')
        ->fields(array(
        'aid' => $aid,
        'provider_id' => $provider_id,
        'created' => REQUEST_TIME,
      ))
        ->execute();
    } catch (Exception $e) {
      $txn
        ->rollback();
      watchdog_exception('hybridauth', $e);
    }

    // Default (error) message.
    $message = array(
      'text' => t('We were unable to link your %provider account.', array(
        '%provider' => $provider_name,
      )),
      'type' => 'error',
    );
    if (isset($result1) && isset($result2)) {
      $message = array(
        'text' => t('We have successfully linked your %provider account.', array(
          '%provider' => $provider_name,
        )),
        'type' => 'status',
      );

      // 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_account', 'insert', $account);

      // TODO: support dynamic profile field mapping

      //_hybridauth_import_user_data();
    }
  }

  // Cleanly close popup and redirect
  drupal_set_message($message['text'], $message['type']);
  $GLOBALS['devel_shutdown'] = FALSE;

  // Prevent devel module from spewing.
  $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user/' . $user->uid . '/hybridauth';
  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();
}