| 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $currentSessionId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function terminateSessionByIdentifier(string $sessionIdentifier): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $this->sessionBackend->remove($sessionIdentifier); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |