You are here

function twitter_signin_oauth_callback_submit in Twitter 7.4

Same name and namespace in other branches
  1. 6.5 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  2. 6.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  3. 6.4 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  4. 7.6 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  5. 7.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  6. 7.5 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()

Form submit for the OAuth callback. Here we add in sign-in specific handling.

1 string reference to 'twitter_signin_oauth_callback_submit'
twitter_signin_form_alter in twitter_signin/twitter_signin.module
Implements hook_form_alter().

File

twitter_signin/twitter_signin.module, line 122
Hook implementations for Twitter Signin module.

Code

function twitter_signin_oauth_callback_submit(&$form, &$form_state) {
  global $user;
  $success = FALSE;
  $key = variable_get('twitter_consumer_key', '');
  $secret = variable_get('twitter_consumer_secret', '');
  $response = $form_state['twitter_oauth']['response'];
  $account = user_external_load($response['user_id']);
  if (isset($account->uid)) {
    user_external_login($account, $response);
    $success = TRUE;
  }
  elseif ($uid = db_query("SELECT uid FROM {twitter_account} WHERE twitter_uid = :twitter_uid", array(
    ':twitter_uid' => $response['user_id'],
  ))
    ->fetchField()) {

    // We have an existing Twitter account - set it up for login.
    $account = user_load($uid);
    $edit["authname_twitter"] = $response['user_id'];
    user_save($account, $edit);
    $user = $account;
    $form_state['twitter_oauth']['account'] = $account;
    $success = TRUE;
  }
  else {

    // No existing user account, let's see if we can register.
    if (variable_get('twitter_signin_register', 0)) {

      // Check for a nickname collision.
      $account = array_shift(user_load_multiple(array(), array(
        'name' => $response['screen_name'],
      )));
      if (empty($account->uid)) {
        $password = user_password();
        $account->name = $response['screen_name'];
        $account->pass = $password;
        $account->init = $response['screen_name'];
        $account->status = 1;
        $account->authname_twitter = $response['user_id'];
        $account->access = REQUEST_TIME;
        $account->is_new = TRUE;
        $account = user_save($account);
        $user = $account;
        $form_state['twitter_oauth']['account'] = $account;
        drupal_set_message(t('You have been automatically registered with the password !password. ' . 'Copy it to !link.', array(
          '!password' => $password,
          '!link' => l('set your account settings', 'user/' . $account->uid . '/edit'),
        )));
        $success = TRUE;
      }
      else {
        drupal_set_message(t('The nickname %name is already in use on this site, please register below with a new nick name, or @login to continue.', array(
          '%name' => $response['screen_name'],
          '@login' => url('user/login'),
        )), 'warning');
      }
    }
    else {
      drupal_set_message(t('Please log in or register to relate your Twitter account with a user.'));
    }
  }
  if (!$success) {
    $_SESSION['twitter']['values'] = $response;
    drupal_goto('user/register');
  }
}