‪TYPO3CMS  ‪main
SessionManager.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
25 
41 {
45  protected ‪$sessionBackends = [];
46 
53  {
54  if (!isset($this->sessionBackends[‪$identifier])) {
55  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['session'][‪$identifier] ?? false;
56  if (!$configuration) {
57  throw new \InvalidArgumentException('Session configuration for identifier ' . ‪$identifier . ' was not found', 1482234750);
58  }
59 
60  $sessionBackend = $this->‪createSessionBackendFromConfiguration($identifier, $configuration);
61 
62  // Validates the session backend configuration and throws an exception if something's wrong
63  $sessionBackend->validateConfiguration();
64  $this->sessionBackends[‪$identifier] = $sessionBackend;
65  }
66  return $this->sessionBackends[‪$identifier];
67  }
68 
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->getSession()->getIdentifier();
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 
106  protected function ‪createSessionBackendFromConfiguration(string ‪$identifier, array $configuration): ‪SessionBackendInterface
107  {
108  $className = $configuration['backend'];
109 
110  if (!is_subclass_of($className, SessionBackendInterface::class)) {
111  throw new \InvalidArgumentException('Configured session backend ' . $className . ' does not implement ' . SessionBackendInterface::class, 1482235035);
112  }
113 
114  $options = $configuration['options'] ?? [];
115 
117  $backend = GeneralUtility::makeInstance($className);
118  $backend->‪initialize(‪$identifier, $options);
119  return $backend;
120  }
121 }
‪TYPO3\CMS\Core\Session\SessionManager\getSessionBackend
‪getSessionBackend(string $identifier)
Definition: SessionManager.php:51
‪TYPO3\CMS\Core\Session\SessionManager
Definition: SessionManager.php:41
‪TYPO3\CMS\Core\Session\Backend\HashableSessionBackendInterface
Definition: HashableSessionBackendInterface.php:21
‪TYPO3\CMS\Core\Session\SessionManager\createSessionBackendFromConfiguration
‪createSessionBackendFromConfiguration(string $identifier, array $configuration)
Definition: SessionManager.php:105
‪TYPO3\CMS\Core\Session
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface
Definition: SessionBackendInterface.php:28
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\remove
‪bool remove(string $sessionId)
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\initialize
‪initialize(string $identifier, array $configuration)
‪TYPO3\CMS\Core\Session\SessionManager\$sessionBackends
‪SessionBackendInterface[] $sessionBackends
Definition: SessionManager.php:44
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\getAll
‪array getAll()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Session\SessionManager\invalidateAllSessionsByUserId
‪invalidateAllSessionsByUserId(SessionBackendInterface $backend, int $userId, AbstractUserAuthentication $userAuthentication=null)
Definition: SessionManager.php:73
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:65