You are here

function hybridauth_user_delete_form_submit in HybridAuth Social Login 7

Handle identity deletion by removing {authmap} and {hybridauth_linked_account} entries.

File

./hybridauth.pages.inc, line 730

Code

function hybridauth_user_delete_form_submit($form, &$form_state) {
  $uid = $form_state['build_info']['args'][0]->uid;
  $aid = $form_state['build_info']['args'][1];
  $linked_account = db_query("SELECT am.authname, am.provider_id FROM {authmap} am INNER JOIN {hybridauth_account} ha ON am.aid = ha.aid WHERE am.uid=:uid AND am.aid=:aid AND module=:module", array(
    ':uid' => $uid,
    ':aid' => $aid,
    ':module' => 'hybridauth',
  ))
    ->fetchObject();

  // Linked account data has to be deleted from two tables, so use a transaction.
  $txn = db_transaction();
  try {
    $result1 = db_delete('authmap')
      ->condition('uid', $uid)
      ->condition('aid', $form_state['build_info']['args'][1])
      ->condition('module', 'hybridauth_core')
      ->execute();
    $result2 = db_delete('hybridauth_account')
      ->condition('aid', $form_state['build_info']['args'][1])
      ->execute();
  } catch (Exception $e) {
    $txn
      ->rollback();
    watchdog_exception('hybridauth', $e);
  }
  if (isset($result1) && isset($result2)) {
    $provider_id = $linked_account->provider_id;
    $provider_name = hybridauth_get_provider_name($provider_id);
    drupal_set_message(t('The linked %provider account has been successfully removed.', array(
      '%provider' => $provider_name,
    )));

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

    // Let other modules know that a linked account has been removed.
    $user = user_load($uid);
    $account = array(
      'user' => $user,
      'id' => _hybridauth_get_authmap_identfier($linked_account->authname),
      'provider_id' => $provider_id,
      'provider_name' => $provider_name,
    );
    module_invoke_all('hybridauth_account', 'delete', $account);
  }
  else {
    drupal_set_message(t('We were unable to delete the linked %provider account.', array(
      '%provider' => $provider_name,
    )), 'error');
  }

  // Let hybridauth_user_identities() know that the delete confirm_form has been
  // used (so that it can work around http://drupal.org/node/1029458#comment-4055748).
  $_SESSION['hybridauth_account_deleted'] = TRUE;
  $form_state['redirect'] = 'user/' . $uid . '/hybridauth';
}