You are here

class SimpleFbConnectPostLoginManager in Simple FB Connect 8.2

Same name and namespace in other branches
  1. 8.3 src/SimpleFbConnectPostLoginManager.php \Drupal\simple_fb_connect\SimpleFbConnectPostLoginManager

Contains all logic that is related to post login redirects.

Hierarchy

Expanded class hierarchy of SimpleFbConnectPostLoginManager

2 files declare their use of SimpleFbConnectPostLoginManager
SimpleFbConnectController.php in src/Controller/SimpleFbConnectController.php
Contains \Drupal\simple_fb_connect\Controller\SimpleFbConnectController.
SimpleFbConnectPostLoginManagerTest.php in tests/src/Unit/SimpleFbConnectPostLoginManagerTest.php
Contains Drupal\Tests\simple_fb_connect\Unit\SimpleFbConnectPostLoginManagerTest.
1 string reference to 'SimpleFbConnectPostLoginManager'
simple_fb_connect.services.yml in ./simple_fb_connect.services.yml
simple_fb_connect.services.yml
1 service uses SimpleFbConnectPostLoginManager
simple_fb_connect.post_login_manager in ./simple_fb_connect.services.yml
Drupal\simple_fb_connect\SimpleFbConnectPostLoginManager

File

src/SimpleFbConnectPostLoginManager.php, line 19
Contains \Drupal\simple_fb_connect\SimpleFbConnectPostLoginManager.

Namespace

Drupal\simple_fb_connect
View source
class SimpleFbConnectPostLoginManager {
  protected $configFactory;
  protected $requestContext;
  protected $session;
  protected $pathValidator;

  /**
   * Constructor.
   */
  public function __construct(ConfigFactoryInterface $config_factory, SessionInterface $session, RequestContext $request_context, PathValidatorInterface $path_validator) {
    $this->configFactory = $config_factory;
    $this->requestContext = $request_context;
    $this->session = $session;
    $this->pathValidator = $path_validator;
  }

  /**
   * Returns the value of postLoginPath query parameter if set.
   *
   * @returns string|bool
   *   Raw query parameter value if set.
   *   False otherwise.
   */
  public function getPostLoginPathFromRequest() {
    if ($query_string = $this->requestContext
      ->getQueryString()) {
      parse_str($query_string, $query_params);
      if (isset($query_params['postLoginPath'])) {
        return $query_params['postLoginPath'];
      }
    }
    return FALSE;
  }

  /**
   * Saves given path to session.
   *
   * @param string $post_login_path
   *   Path to save to session.
   */
  public function savePostLoginPathToSession($post_login_path) {
    if (!is_null($post_login_path)) {
      $this->session
        ->set('simple_fb_connect_post_login_path', $post_login_path);
    }
  }

  /**
   * Returns the path the user should be redirected after a successful login.
   *
   * Return order:
   * 1. Path from query string, if set, valid and not external.
   * 2. Path from module settings, if valid and not external.
   * 3. User page.
   *
   * @return string
   *   URL where the user should be redirected after FB login.
   */
  public function getPostLoginPath() {

    // 1. Read the post login path from session.
    $post_login_path = $this->session
      ->get('simple_fb_connect_post_login_path');
    if ($post_login_path) {
      if ($valid_path = $this
        ->validateInternalPath($post_login_path)) {
        return $valid_path;
      }
    }

    // 2. Use post login path from module settings.
    $post_login_path = $this
      ->getPostLoginPathSetting();
    if ($valid_path = $this
      ->validateInternalPath($post_login_path)) {
      return $valid_path;
    }

    // 3. Use user page.
    $post_login_path = 'user';
    return $this
      ->validateInternalPath($post_login_path);
  }

  /**
   * Checks if new users should be redirected to Drupal user form.
   *
   * @return bool
   *   True if new users should be redirected to user form.
   *   False otherwise.
   */
  public function getRedirectNewUsersToUserFormSetting() {
    return $this->configFactory
      ->get('simple_fb_connect.settings')
      ->get('redirect_user_form');
  }

  /**
   * Returns the path to user's user form.
   *
   * @param \Drupal\user\Entity\User $drupal_user
   *   User object.
   *
   * @return string
   *   Path to user edit form.
   */
  public function getPathToUserForm(User $drupal_user) {
    $uid = $drupal_user
      ->id();
    $post_login_path = 'user/' . $uid . '/edit';
    $url = $this->pathValidator
      ->getUrlIfValid($post_login_path);
    return $url
      ->toString();
  }

  /**
   * Returns the post login path defined in module settings.
   *
   * @return string
   *   Path defined in module settings.
   */
  protected function getPostLoginPathSetting() {
    return $this->configFactory
      ->get('simple_fb_connect.settings')
      ->get('post_login_path');
  }

  /**
   * Checks that given path is valid internal Drupal path.
   *
   * Returned path includes subfolder as path prefix if Drupal is installed to a
   * subfolder.
   *
   * @param string $path
   *   Path to validate.
   *
   * @return string|bool
   *   Path to redirect the user if $path was valid.
   *   False otherwise
   */
  protected function validateInternalPath($path) {
    $url = $this->pathValidator
      ->getUrlIfValid($path);
    if ($url !== FALSE && $url
      ->isExternal() === FALSE) {
      return $url
        ->toString();
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleFbConnectPostLoginManager::$configFactory protected property
SimpleFbConnectPostLoginManager::$pathValidator protected property
SimpleFbConnectPostLoginManager::$requestContext protected property
SimpleFbConnectPostLoginManager::$session protected property
SimpleFbConnectPostLoginManager::getPathToUserForm public function Returns the path to user's user form.
SimpleFbConnectPostLoginManager::getPostLoginPath public function Returns the path the user should be redirected after a successful login.
SimpleFbConnectPostLoginManager::getPostLoginPathFromRequest public function Returns the value of postLoginPath query parameter if set.
SimpleFbConnectPostLoginManager::getPostLoginPathSetting protected function Returns the post login path defined in module settings.
SimpleFbConnectPostLoginManager::getRedirectNewUsersToUserFormSetting public function Checks if new users should be redirected to Drupal user form.
SimpleFbConnectPostLoginManager::savePostLoginPathToSession public function Saves given path to session.
SimpleFbConnectPostLoginManager::validateInternalPath protected function Checks that given path is valid internal Drupal path.
SimpleFbConnectPostLoginManager::__construct public function Constructor.