You are here

class UserSessionState in RESTful 7.2

Class UserSessionState.

@package Drupal\restful\Authentication

Hierarchy

Expanded class hierarchy of UserSessionState

File

src/Authentication/UserSessionState.php, line 15
Contains \Drupal\restful\Authentication\UserSessionState.

Namespace

Drupal\restful\Authentication
View source
class UserSessionState implements UserSessionStateInterface {

  /**
   * Boolean holding if this is the first switch.
   *
   * @var bool
   */
  protected static $isSwitched = FALSE;

  /**
   * Boolean holding if the session needs to be saved.
   *
   * @var bool
   */
  protected $needsSaving = FALSE;

  /**
   * Object holding the original user.
   *
   * This is saved for switch back purposes.
   *
   * @var object
   */
  protected $originalUser;

  /**
   * {@inheritdoc}
   */
  public static function isSwitched() {
    return static::$isSwitched;
  }

  /**
   * {@inheritdoc}
   */
  public function switchUser($account) {
    global $user;
    if (!static::isSwitched() && !$this->originalUser && !$this->needsSaving) {

      // This is the first time a user switched, and there isn't an original
      // user session.
      $this->needsSaving = drupal_save_session();
      $this->originalUser = $user;

      // Don't allow a session to be saved. Provider that require a session to
      // be saved, like the cookie provider, need to explicitly set
      // drupal_save_session(TRUE).
      // @see LoginCookie__1_0::loginUser().
      drupal_save_session(FALSE);
    }

    // Set the global user.
    $user = $account;
  }

  /**
   * Switch the user to the authenticated user, and back.
   *
   * This should be called only for an API call. It should not be used for calls
   * via the menu system, as it might be a login request, so we avoid switching
   * back to the anonymous user.
   */
  public function switchUserBack() {
    global $user;
    if (!$this->originalUser) {
      return;
    }
    $user = $this->originalUser;
    drupal_save_session($this->needsSaving);
    $this
      ->reset();
  }

  /**
   * Reset the initial values.
   */
  protected function reset() {

    // Reset initial values.
    static::$isSwitched = FALSE;
    $this->originalUser = NULL;
    $this->needsSaving = FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserSessionState::$isSwitched protected static property Boolean holding if this is the first switch.
UserSessionState::$needsSaving protected property Boolean holding if the session needs to be saved.
UserSessionState::$originalUser protected property Object holding the original user.
UserSessionState::isSwitched public static function Check if the user has already been switched. Overrides UserSessionStateInterface::isSwitched
UserSessionState::reset protected function Reset the initial values.
UserSessionState::switchUser public function Make the passed in user to be the account for the Drupal thread. Overrides UserSessionStateInterface::switchUser
UserSessionState::switchUserBack public function Switch the user to the authenticated user, and back. Overrides UserSessionStateInterface::switchUserBack