You are here

SimpleFbConnectController.php in Simple FB Connect 8.2

File

src/Controller/SimpleFbConnectController.php
View source
<?php

/**
 * @file
 * Contains \Drupal\simple_fb_connect\Controller\SimpleFbConnectController.
 */
namespace Drupal\simple_fb_connect\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Facebook\FacebookRedirectLoginHelper;
use Drupal\simple_fb_connect\SimpleFbConnectFbManager;
use Drupal\simple_fb_connect\SimpleFbConnectUserManager;
use Drupal\simple_fb_connect\SimpleFbConnectPostLoginManager;

/**
 * Returns responses for Simple FB Connect module routes.
 */
class SimpleFbConnectController extends ControllerBase {
  protected $fbManager;
  protected $userManager;
  protected $postLoginManager;

  /**
   * Constructor.
   *
   * The constructor parameters are passed from the create() method.
   */
  public function __construct(SimpleFbConnectFbManager $fb_manager, SimpleFbConnectUserManager $user_manager, SimpleFbConnectPostLoginManager $post_login_manager) {
    $this->fbManager = $fb_manager;
    $this->userManager = $user_manager;
    $this->postLoginManager = $post_login_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('simple_fb_connect.fb_manager'), $container
      ->get('simple_fb_connect.user_manager'), $container
      ->get('simple_fb_connect.post_login_manager'));
  }

  /**
   * Response for path 'user/simple-fb-connect'.
   *
   * Redirects the user to FB for authentication.
   */
  public function redirectToFb() {

    // Validate configuration.
    if (!$this->fbManager
      ->validateConfig()) {
      drupal_set_message(t('Simple FB Connect not configured properly. Contact site administrator.'), 'error');
      return $this
        ->redirect('user.login');
    }

    // Save post login path to session if it was set as query parameter.
    if ($post_login_path = $this->postLoginManager
      ->getPostLoginPathFromRequest()) {
      $this->postLoginManager
        ->savePostLoginPathToSession($post_login_path);
    }

    // Redirect the user to FB for authentication.
    $fb_login_url = $this->fbManager
      ->getFbLoginUrl();
    return new TrustedRedirectResponse($fb_login_url);
  }

  /**
   * Response for path 'user/simple-fb-connect/return'.
   *
   * Facebook returns the user here after user has authenticated in FB.
   */
  public function returnFromFb() {

    // Validate configuration.
    if (!$this->fbManager
      ->validateConfig()) {
      drupal_set_message(t('Simple FB Connect not configured properly.'), 'error');
      return $this
        ->redirect('user.login');
    }

    // SDK can start FacebookSession from the page where FB returned the user.
    $login_helper = new FacebookRedirectLoginHelper($this->fbManager
      ->getReturnUrl());
    if (!$this->fbManager
      ->startFbSession($login_helper)) {
      drupal_set_message(t("Facebook login failed."), 'error');
      return $this
        ->redirect('user.login');
    }

    // Get a validated FacebookSession object.
    if (!($fb_session = $this->fbManager
      ->getFbSession())) {
      drupal_set_message(t("Facebook login failed."), 'error');
      return $this
        ->redirect('user.login');
    }

    // Get user's FB profile from Facebook API.
    if (!($fb_profile = $this->fbManager
      ->getFbProfile($fb_session))) {
      drupal_set_message(t("Facebook login failed, could not load Facebook profile. Contact site administrator."), 'error');
      return $this
        ->redirect('user.login');
    }

    // Get user's email from the FB profile.
    if (!($email = $this->fbManager
      ->getEmail($fb_profile))) {
      drupal_set_message(t('Facebook login failed. This site requires permission to get your email address.'), 'error');
      return $this
        ->redirect('user.login');
    }

    // If we have an existing user with the same email address, try to log in.
    if ($drupal_user = $this->userManager
      ->loadUserByProperty('mail', $email)) {
      if ($this->userManager
        ->loginUser($drupal_user)) {
        return new RedirectResponse($this->postLoginManager
          ->getPostLoginPath());
      }
      else {
        return $this
          ->redirect('user.login');
      }
    }

    // If there was no existing user, try to create a new user.
    $drupal_user = $this->userManager
      ->createUser($fb_profile
      ->getProperty('name'), $email);
    if ($drupal_user) {

      // Download profile picture for the newly created user.
      if ($picture_url = $this->fbManager
        ->getFbProfilePicUrl($fb_session)) {
        $this->userManager
          ->setProfilePic($drupal_user, $picture_url, $fb_profile
          ->getProperty('id'));
      }

      // Log the newly created user in.
      if ($this->userManager
        ->loginUser($drupal_user)) {

        // Check if new users should be redirected to Drupal user form.
        if ($this->postLoginManager
          ->getRedirectNewUsersToUserFormSetting()) {
          drupal_set_message(t("Please check your account details. Since you logged in with Facebook, you don't need to update your password."));
          return new RedirectResponse($this->postLoginManager
            ->getPathToUserForm($drupal_user));
        }

        // Use normal post login path if user wasn't redirected to user form.
        return new RedirectResponse($this->postLoginManager
          ->getPostLoginPath());
      }
      else {

        // New user was succesfully created but the account is pending approval.
        drupal_set_message(t('You will receive an email when site administrator activates your account.'), 'warning');
        return $this
          ->redirect('user.login');
      }
    }
    else {

      // User could not be created.
      return $this
        ->redirect('user.login');
    }

    // This should never be reached, user should have been redirected already.
    throw new AccessDeniedHttpException();
  }

}

Classes

Namesort descending Description
SimpleFbConnectController Returns responses for Simple FB Connect module routes.