You are here

public function GoogleLinkController::linkAccountCallback in Open Social 8.8

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

File

modules/custom/social_auth_google/src/Controller/GoogleLinkController.php, line 65

Class

GoogleLinkController
Class GoogleLinkController.

Namespace

Drupal\social_auth_google\Controller

Code

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

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

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

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