523733d216254d5dbc41c3d121ff6b3bd3e61996
[yaffs-website] / web / core / modules / workspaces / src / Negotiator / SessionWorkspaceNegotiator.php
1 <?php
2
3 namespace Drupal\workspaces\Negotiator;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\workspaces\WorkspaceInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\Session\Session;
10
11 /**
12  * Defines the session workspace negotiator.
13  */
14 class SessionWorkspaceNegotiator implements WorkspaceNegotiatorInterface {
15
16   /**
17    * The current user.
18    *
19    * @var \Drupal\Core\Session\AccountInterface
20    */
21   protected $currentUser;
22
23   /**
24    * The session.
25    *
26    * @var \Symfony\Component\HttpFoundation\Session\Session
27    */
28   protected $session;
29
30   /**
31    * The workspace storage handler.
32    *
33    * @var \Drupal\Core\Entity\EntityStorageInterface
34    */
35   protected $workspaceStorage;
36
37   /**
38    * Constructor.
39    *
40    * @param \Drupal\Core\Session\AccountInterface $current_user
41    *   The current user.
42    * @param \Symfony\Component\HttpFoundation\Session\Session $session
43    *   The session.
44    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
45    *   The entity type manager.
46    */
47   public function __construct(AccountInterface $current_user, Session $session, EntityTypeManagerInterface $entity_type_manager) {
48     $this->currentUser = $current_user;
49     $this->session = $session;
50     $this->workspaceStorage = $entity_type_manager->getStorage('workspace');
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function applies(Request $request) {
57     // This negotiator only applies if the current user is authenticated.
58     return $this->currentUser->isAuthenticated();
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getActiveWorkspace(Request $request) {
65     $workspace_id = $this->session->get('active_workspace_id');
66
67     if ($workspace_id && ($workspace = $this->workspaceStorage->load($workspace_id))) {
68       return $workspace;
69     }
70
71     return NULL;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function setActiveWorkspace(WorkspaceInterface $workspace) {
78     $this->session->set('active_workspace_id', $workspace->id());
79   }
80
81 }