You are here

public function LinkedInLinkController::linkAccountCallback in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  2. 8.2 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  3. 8.3 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  4. 8.4 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  5. 8.5 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  6. 8.6 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  7. 8.7 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::linkAccountCallback()
  8. 8.8 modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php \Drupal\social_auth_linkedin\Controller\LinkedInLinkController::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 'LinkedInLinkController::linkAccountCallback'
social_auth_linkedin.routing.yml in modules/custom/social_auth_linkedin/social_auth_linkedin.routing.yml
modules/custom/social_auth_linkedin/social_auth_linkedin.routing.yml

File

modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php, line 65

Class

LinkedInLinkController
Class LinkedInLinkController.

Namespace

Drupal\social_auth_linkedin\Controller

Code

public function linkAccountCallback() {
  $sdk = $this
    ->getSdk();
  if ($sdk instanceof RedirectResponse) {
    return $sdk;
  }
  $this->authManager
    ->setSdk($sdk);

  // Get the OAuth token from LinkedIn.
  if (!$this->authManager
    ->getAccessToken('link')) {
    drupal_set_message($this
      ->t('@network login failed. Token is not valid.', [
      '@network' => $this
        ->t('LinkedIn'),
    ]), 'error');
    return $this
      ->redirect('entity.user.edit_form', [
      'user' => $this
        ->currentUser()
        ->id(),
    ]);
  }

  // Get user's LinkedIn profile from LinkedIn API.
  if (!($profile = $this->authManager
    ->getProfile()) || !isset($profile['id'])) {
    drupal_set_message($this
      ->t('@network login failed, could not load @network profile. Contact site administrator.', [
      '@network' => $this
        ->t('LinkedIn'),
    ]), '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([
    'linkedin_id' => $profile['id'],
  ]);
  $account = current($account);

  // Check whether another account was connected to this LinkedIn 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('LinkedIn'),
    ]), 'warning');
    return $this
      ->redirect('entity.user.edit_form', [
      'user' => $this
        ->currentUser()
        ->id(),
    ]);
  }
  $account = User::load($this
    ->currentUser()
    ->id());
  $account
    ->get('linkedin_id')
    ->setValue($profile['id']);
  $account
    ->save();
  drupal_set_message($this
    ->t('You are now able to log in with @network', [
    '@network' => $this
      ->t('LinkedIn'),
  ]));
  return $this
    ->redirect('entity.user.edit_form', [
    'user' => $account
      ->id(),
  ]);
}