Add new intern method to delete sessions except a specific one
authorjoshuaruesweg <ruesweg@woltlab.com>
Mon, 26 Oct 2020 13:42:04 +0000 (14:42 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Mon, 2 Nov 2020 11:22:41 +0000 (12:22 +0100)
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

index 374138084f2eac15721884f6f4d09dc893357922..e2b13a227bd9b97a27d99488a8313ce3ee2198ba 100644 (file)
@@ -1035,42 +1035,38 @@ final class SessionHandler extends SingletonFactory {
        
        /**
         * Deletes the user sessions for a specific user, except the session with the given session id.
-        * If the given session id is null or unknown, all sessions for the user will be deleted.
         * 
+        * If the given session id is `null` or unknown, all sessions of the user will be deleted.
+        * 
+        * @throws      \InvalidArgumentException if the given user is a guest.
         * @since       5.4
         */
        public function deleteUserSessionsExcept(User $user, ?string $sessionID = null): void {
-               if ($user->userID === 0) {
-                       throw new \InvalidArgumentException("The given user is a guest.");
-               }
-               
-               $conditionBuilder = new PreparedStatementConditionBuilder();
-               $conditionBuilder->add('userID = ?', [$user->userID]);
-               
-               if ($sessionID !== null) {
-                       $conditionBuilder->add('sessionID <> ?', [$sessionID]);
-               }
-               
-               $sql = "DELETE FROM     wcf".WCF_N."_user_session
-                       ". $conditionBuilder;
-               $statement = WCF::getDB()->prepareStatement($sql);
-               $statement->execute($conditionBuilder->getParameters());
-               
-               // Delete legacy session.
-               $sql = "DELETE FROM     wcf".WCF_N."_session
-                       ". $conditionBuilder;
-               $statement = WCF::getDB()->prepareStatement($sql);
-               $statement->execute($conditionBuilder->getParameters());
+               $this->deleteSessionsExcept($user, $sessionID);
        }
        
        /**
         * Deletes the acp sessions for a specific user, except the session with the given session id.
-        * If the given session id is null or unknown, all acp sessions for the user will be deleted.
         * 
+        * If the given session id is `null` or unknown, all acp sessions of the user will be deleted.
+        * 
+        * @throws      \InvalidArgumentException if the given user is a guest.
         * @since       5.4
         */
        public function deleteAcpSessionsExcept(User $user, ?string $sessionID = null): void {
-               if ($user->userID === 0) {
+               $this->deleteSessionsExcept($user, $sessionID, true);
+       }
+       
+       /**
+        * Deletes the sessions for a specific user, except the session with the given session id.
+        *
+        * If the given session id is `null` or unknown, all acp sessions of the user will be deleted.
+        *
+        * @throws      \InvalidArgumentException if the given user is a guest.
+        * @since       5.4
+        */
+       private function deleteSessionsExcept(User $user, ?string $sessionID = null, bool $isAcp = false): void {
+               if (!$user->userID) {
                        throw new \InvalidArgumentException("The given user is a guest.");
                }
                
@@ -1081,10 +1077,18 @@ final class SessionHandler extends SingletonFactory {
                        $conditionBuilder->add('sessionID <> ?', [$sessionID]);
                }
                
-               $sql = "DELETE FROM     wcf".WCF_N."_acp_session
+               $sql = "DELETE FROM     wcf".WCF_N."_". ($isAcp ? 'acp' : 'user') ."_session
                        ". $conditionBuilder;
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute($conditionBuilder->getParameters());
+               
+               if (!$isAcp) {
+                       // Delete legacy session.
+                       $sql = "DELETE FROM     wcf".WCF_N."_session
+                       ". $conditionBuilder;
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditionBuilder->getParameters());
+               }
        }
        
        /**