Add class to represent a session
authorjoshuaruesweg <ruesweg@woltlab.com>
Sat, 24 Oct 2020 15:37:43 +0000 (17:37 +0200)
committerjoshuaruesweg <ruesweg@woltlab.com>
Mon, 2 Nov 2020 11:22:41 +0000 (12:22 +0100)
wcfsetup/install/files/lib/system/session/Session.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

diff --git a/wcfsetup/install/files/lib/system/session/Session.class.php b/wcfsetup/install/files/lib/system/session/Session.class.php
new file mode 100644 (file)
index 0000000..0185efe
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+namespace wcf\system\session;
+
+/**
+ * Represents a session.
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Session
+ * @since       5.4
+ */
+final class Session {
+       /**
+        * @var array 
+        */
+       private $data;
+       
+       /**
+        * @var bool 
+        */
+       private $isAcpSession;
+       
+       /**
+        * Session constructor.
+        */
+       public function __construct(array $data, bool $isAcpSession = false) {
+               $this->data = $data;
+               $this->isAcpSession = $isAcpSession;
+       }
+       
+       /**
+        * Returns the session id for the session.
+        */
+       public function getSessionID(): string {
+               return $this->data['sessionID'];
+       }
+       
+       /**
+        * Returns the user id for the session. If the session belongs to a guest, 
+        * `null` is returned.
+        */
+       public function getUserID(): ?int {
+               return $this->data['userID'];
+       }
+       
+       /**
+        * Returns the last activity time.
+        */
+       public function getLastActivityTime(): int {
+               return $this->data['lastActivityTime'];
+       }
+       
+       /**
+        * Returns `true`, if the session is an acp session.
+        */
+       public function isAcpSession(): bool {
+               return $this->isAcpSession;
+       }
+}
index 526415b3f951bf8b4f376bfb2021e7d562cd3adb..7356781cd99d063d982b6858dd6213262d65a8de 100644 (file)
@@ -987,6 +987,8 @@ final class SessionHandler extends SingletonFactory {
        
        /**
         * Returns all user sessions for a specific user.
+        *
+        * @return      \wcf\system\session\Session[]
         */
        public function getUserSessions(User $user): array {
                if ($user->userID === 0) {
@@ -999,11 +1001,19 @@ final class SessionHandler extends SingletonFactory {
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute([$user->userID]);
                
-               return $statement->fetchAll();
+               $sessions = [];
+               
+               while ($row = $statement->fetchArray()) {
+                       $sessions[] = new \wcf\system\session\Session($row);
+               } 
+               
+               return $sessions;
        }
        
        /**
         * Returns all acp sessions for a specific user.
+        * 
+        * @return      \wcf\system\session\Session[]
         */
        public function getAcpSessions(User $user): array {
                if ($user->userID === 0) {
@@ -1016,7 +1026,13 @@ final class SessionHandler extends SingletonFactory {
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute([$user->userID]);
                
-               return $statement->fetchAll();
+               $sessions = [];
+               
+               while ($row = $statement->fetchArray()) {
+                       $sessions[] = new \wcf\system\session\Session($row, true);
+               }
+               
+               return $sessions;
        }
        
        /**