You are here

class SimpleFbConnectPostLoginManager in Simple FB Connect 8.3

Same name and namespace in other branches
  1. 8.2 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
SimpleFbConnectPostLoginManagerTest.php in tests/src/Unit/SimpleFbConnectPostLoginManagerTest.php
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 13

Namespace

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

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Used for accessing Drupal configuration.
   * @param \Drupal\Core\Routing\RequestContext $request_context
   *   Used for reading the query string.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   Used for validating user provided paths.
   * @param SimpleFbConnectPersistentDataHandler $persistent_data_handler
   *   Used for reading data from and writing data to session.
   */
  public function __construct(ConfigFactoryInterface $config_factory, RequestContext $request_context, PathValidatorInterface $path_validator, SimpleFbConnectPersistentDataHandler $persistent_data_handler) {
    $this->configFactory = $config_factory;
    $this->requestContext = $request_context;
    $this->pathValidator = $path_validator;
    $this->persistentDataHandler = $persistent_data_handler;
  }

  /**
   * 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 savePostLoginPath($post_login_path) {
    if (!is_null($post_login_path)) {
      $this->persistentDataHandler
        ->set('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->persistentDataHandler
      ->get('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::$persistentDataHandler protected property
SimpleFbConnectPostLoginManager::$requestContext 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::savePostLoginPath 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.