‪TYPO3CMS  9.5
SessionManager.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
23 
37 {
41  protected ‪$sessionBackends = [];
42 
50  public function ‪getSessionBackend(string $identifier): ‪SessionBackendInterface
51  {
52  if (!isset($this->sessionBackends[$identifier])) {
53  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['session'][$identifier] ?? false;
54  if (!$configuration) {
55  throw new \InvalidArgumentException('Session configuration for identifier ' . $identifier . ' was not found', 1482234750);
56  }
57 
58  $sessionBackend = $this->‪createSessionBackendFromConfiguration($identifier, $configuration);
59 
60  // Validates the session backend configuration and throws an exception if something's wrong
61  $sessionBackend->validateConfiguration();
62  $this->sessionBackends[$identifier] = $sessionBackend;
63  }
64  return $this->sessionBackends[$identifier];
65  }
66 
74  public function ‪invalidateAllSessionsByUserId(‪SessionBackendInterface $backend, int $userId, ‪AbstractUserAuthentication $userAuthentication = null)
75  {
76  $sessionToRenew = '';
77  $hashedSessionToRenew = '';
78  // Prevent destroying the session of the current user session, but renew session id
79  if ($userAuthentication !== null && (int)$userAuthentication->user['uid'] === $userId) {
80  $sessionToRenew = $userAuthentication->getSessionId();
81  }
82  if ($sessionToRenew !== '' && $backend instanceof ‪HashableSessionBackendInterface) {
83  $hashedSessionToRenew = $backend->hash($sessionToRenew);
84  }
85 
86  foreach ($backend->‪getAll() as $session) {
87  if ($userAuthentication !== null) {
88  if ($session['ses_id'] === $sessionToRenew || $session['ses_id'] === $hashedSessionToRenew) {
89  $userAuthentication->enforceNewSessionId();
90  continue;
91  }
92  }
93  if ((int)$session['ses_userid'] === $userId) {
94  $backend->‪remove($session['ses_id']);
95  }
96  }
97  }
98 
107  protected function ‪createSessionBackendFromConfiguration(string $identifier, array $configuration): ‪SessionBackendInterface
108  {
109  $className = $configuration['backend'];
110 
111  if (!is_subclass_of($className, SessionBackendInterface::class)) {
112  throw new \InvalidArgumentException('Configured session backend ' . $className . ' does not implement ' . SessionBackendInterface::class, 1482235035);
113  }
114 
115  $options = $configuration['options'] ?? [];
116 
118  $backend = GeneralUtility::makeInstance($className);
119  $backend->‪initialize($identifier, $options);
120  return $backend;
121  }
122 }
‪TYPO3\CMS\Core\Session\SessionManager
Definition: SessionManager.php:37
‪TYPO3\CMS\Core\Session\Backend\HashableSessionBackendInterface
Definition: HashableSessionBackendInterface.php:21
‪TYPO3\CMS\Core\Session
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface
Definition: SessionBackendInterface.php:26
‪TYPO3\CMS\Core\Session\SessionManager\getSessionBackend
‪SessionBackendInterface getSessionBackend(string $identifier)
Definition: SessionManager.php:49
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\remove
‪bool remove(string $sessionId)
‪TYPO3\CMS\Core\Session\SessionManager\createSessionBackendFromConfiguration
‪SessionBackendInterface createSessionBackendFromConfiguration(string $identifier, array $configuration)
Definition: SessionManager.php:106
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\initialize
‪initialize(string $identifier, array $configuration)
‪TYPO3\CMS\Core\Session\SessionManager\$sessionBackends
‪SessionBackendInterface[] $sessionBackends
Definition: SessionManager.php:40
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\getAll
‪array getAll()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Session\SessionManager\invalidateAllSessionsByUserId
‪invalidateAllSessionsByUserId(SessionBackendInterface $backend, int $userId, AbstractUserAuthentication $userAuthentication=null)
Definition: SessionManager.php:73
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51