Passed
Push — master ( da9c6d...70f855 )
by
unknown
17:46
created

BackendUserSessionRepository::switchBackToOriginalUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A BackendUserSessionRepository::terminateSessionByIdentifier() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Beuser\Domain\Repository;
17
18
use TYPO3\CMS\Beuser\Domain\Model\BackendUser;
19
use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
20
use TYPO3\CMS\Core\Session\Backend\HashableSessionBackendInterface;
21
use TYPO3\CMS\Core\Session\Backend\SessionBackendInterface;
22
use TYPO3\CMS\Core\Session\SessionManager;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
27
 */
28
class BackendUserSessionRepository
29
{
30
    protected SessionBackendInterface $sessionBackend;
31
32
    public function __construct()
33
    {
34
        $this->sessionBackend = GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend('BE');
35
    }
36
37
    /**
38
     * Find all active sessions for all backend users
39
     *
40
     * @return array
41
     */
42
    public function findAllActive(): array
43
    {
44
        $allSessions = $this->sessionBackend->getAll();
45
46
        // Map array to correct keys
47
        $allSessions = array_map(
48
            function ($session) {
49
                return [
50
                    'id' => $session['ses_id'], // this is the hashed sessionId
51
                    'ip' => $session['ses_iplock'],
52
                    'timestamp' => $session['ses_tstamp'],
53
                    'ses_userid' => $session['ses_userid']
54
                ];
55
            },
56
            $allSessions
57
        );
58
59
        // Sort by timestamp
60
        usort($allSessions, function ($session1, $session2) {
61
            return $session1['timestamp'] <=> $session2['timestamp'];
62
        });
63
64
        return $allSessions;
65
    }
66
67
    /**
68
     * Find Sessions for specific BackendUser
69
     *
70
     * @param BackendUser $backendUser
71
     * @return array
72
     */
73
    public function findByBackendUser(BackendUser $backendUser)
74
    {
75
        $allActive = $this->findAllActive();
76
77
        return array_filter(
78
            $allActive,
79
            function ($session) use ($backendUser) {
80
                return (int)$session['ses_userid'] === $backendUser->getUid();
81
            }
82
        );
83
    }
84
85
    public function getPersistedSessionIdentifier(AbstractUserAuthentication $userObject): string
86
    {
87
        $currentSessionId = $userObject->getSession()->getIdentifier();
88
        if ($this->sessionBackend instanceof HashableSessionBackendInterface) {
89
            $currentSessionId = $this->sessionBackend->hash($currentSessionId);
0 ignored issues
show
Bug introduced by
The method hash() does not exist on TYPO3\CMS\Core\Session\B...SessionBackendInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to TYPO3\CMS\Core\Session\B...SessionBackendInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            /** @scrutinizer ignore-call */ 
90
            $currentSessionId = $this->sessionBackend->hash($currentSessionId);
Loading history...
90
        }
91
        return $currentSessionId;
92
    }
93
94
    public function terminateSessionByIdentifier(string $sessionIdentifier): string
95
    {
96
        return $this->sessionBackend->remove($sessionIdentifier);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sessionBac...ove($sessionIdentifier) returns the type boolean which is incompatible with the type-hinted return string.
Loading history...
97
    }
98
}
99