You are here

class SessionWorkspaceNegotiator in Workspace 8.2

Defines the session workspace negotiator.

Hierarchy

Expanded class hierarchy of SessionWorkspaceNegotiator

1 string reference to 'SessionWorkspaceNegotiator'
workspace.services.yml in ./workspace.services.yml
workspace.services.yml
1 service uses SessionWorkspaceNegotiator
workspace.negotiator.session in ./workspace.services.yml
Drupal\workspace\Negotiator\SessionWorkspaceNegotiator

File

src/Negotiator/SessionWorkspaceNegotiator.php, line 14

Namespace

Drupal\workspace\Negotiator
View source
class SessionWorkspaceNegotiator implements WorkspaceNegotiatorInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;

  /**
   * The workspace storage handler.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $workspaceStorage;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Symfony\Component\HttpFoundation\Session\Session $session
   *   The session.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(AccountInterface $current_user, Session $session, EntityTypeManagerInterface $entity_type_manager) {
    $this->currentUser = $current_user;
    $this->session = $session;
    $this->workspaceStorage = $entity_type_manager
      ->getStorage('workspace');
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Request $request) {

    // This negotiator only applies if the current user is authenticated.
    return $this->currentUser
      ->isAuthenticated();
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveWorkspace(Request $request) {
    $workspace_id = $this->session
      ->get('active_workspace_id');
    if ($workspace_id && ($workspace = $this->workspaceStorage
      ->load($workspace_id))) {
      return $workspace;
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setActiveWorkspace(WorkspaceInterface $workspace) {
    $this->session
      ->set('active_workspace_id', $workspace
      ->id());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SessionWorkspaceNegotiator::$currentUser protected property The current user.
SessionWorkspaceNegotiator::$session protected property The session.
SessionWorkspaceNegotiator::$workspaceStorage protected property The workspace storage handler.
SessionWorkspaceNegotiator::applies public function Checks whether the negotiator applies to the current request or not. Overrides WorkspaceNegotiatorInterface::applies
SessionWorkspaceNegotiator::getActiveWorkspace public function Gets the negotiated workspace, if any. Overrides WorkspaceNegotiatorInterface::getActiveWorkspace
SessionWorkspaceNegotiator::setActiveWorkspace public function Sets the negotiated workspace. Overrides WorkspaceNegotiatorInterface::setActiveWorkspace
SessionWorkspaceNegotiator::__construct public function Constructor.