You are here

public function TwitterLinkController::linkAccountCallback in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  2. 8.2 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  3. 8.3 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  4. 8.4 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  5. 8.5 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  6. 8.6 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  7. 8.7 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()
  8. 8.8 modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php \Drupal\social_auth_twitter\Controller\TwitterLinkController::linkAccountCallback()

Makes joining between account on this site and account on social network.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A RedirectResponse pointing to the user edit form.

1 string reference to 'TwitterLinkController::linkAccountCallback'
social_auth_twitter.routing.yml in modules/custom/social_auth_twitter/social_auth_twitter.routing.yml
modules/custom/social_auth_twitter/social_auth_twitter.routing.yml

File

modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php, line 69

Class

TwitterLinkController
Class TwitterLinkController.

Namespace

Drupal\social_auth_twitter\Controller

Code

public function linkAccountCallback() {
  $sdk = $this
    ->getSdk();
  if ($sdk instanceof RedirectResponse) {
    return $sdk;
  }
  $network_manager = $this->networkManager
    ->createInstance('social_auth_twitter');
  $data_handler = $network_manager
    ->getDataHandler();

  // Get the Twitter OAuth token from storage.
  if (!($oauth_token = $data_handler
    ->get('oauth_token')) || !($oauth_token_secret = $data_handler
    ->get('oauth_token_secret'))) {
    drupal_set_message($this
      ->t('Twitter login failed. Token or Token Secret is not valid.'), 'error');
    return $this
      ->redirect('entity.user.edit_form', [
      'user' => $this
        ->currentUser()
        ->id(),
    ]);
  }
  $this->authManager
    ->setSdk($sdk);
  $this->authManager
    ->setAccessToken([
    'oauth_token' => $oauth_token,
    'oauth_token_secret' => $oauth_token_secret,
  ]);

  // Get the permanent access token.
  $access_token = $sdk
    ->oauth('oauth/access_token', [
    'oauth_verifier' => $this->request
      ->get('oauth_verifier'),
  ]);
  $this->authManager
    ->setAccessToken([
    'oauth_token' => $access_token['oauth_token'],
    'oauth_token_secret' => $access_token['oauth_token_secret'],
  ]);

  // Get user's profile from Twitter API.
  if (!$this->authManager
    ->getProfile() || !($account_id = $this->authManager
    ->getAccountId())) {
    drupal_set_message($this
      ->t('@network login failed, could not load @network profile. Contact site administrator.', [
      '@network' => $this
        ->t('Twitter'),
    ]), 'error');
    return $this
      ->redirect('entity.user.edit_form', [
      'user' => $this
        ->currentUser()
        ->id(),
    ]);
  }

  // Check whether any another user account already connected.
  $account = $this
    ->entityTypeManager()
    ->getStorage('user')
    ->loadByProperties([
    'twitter_id' => $account_id,
  ]);
  $account = current($account);

  // Check whether another account was connected to this Twitter account.
  if ($account && (int) $account
    ->id() !== (int) $this
    ->currentUser()
    ->id()) {
    drupal_set_message($this
      ->t('Your @network account has already connected to another account on this site.', [
      '@network' => $this
        ->t('Twitter'),
    ]), 'warning');
    return $this
      ->redirect('entity.user.edit_form', [
      'user' => $this
        ->currentUser()
        ->id(),
    ]);
  }
  $account = User::load($this
    ->currentUser()
    ->id());
  $account
    ->get('twitter_id')
    ->setValue($account_id);
  $account
    ->save();
  drupal_set_message($this
    ->t('You are now able to log in with @network', [
    '@network' => $this
      ->t('Twitter'),
  ]));
  return $this
    ->redirect('entity.user.edit_form', [
    'user' => $account
      ->id(),
  ]);
}