You are here

public function FacebookLinkController::linkAccountCallback in Open Social 8

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

File

modules/custom/social_auth_facebook/src/Controller/FacebookLinkController.php, line 65

Class

FacebookLinkController
Class FacebookLinkController.

Namespace

Drupal\social_auth_facebook\Controller

Code

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

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

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

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