From 6687beba9a7202fb81e2e5265e05cbe79b6aaed0 Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Sat, 24 Oct 2020 17:37:43 +0200 Subject: [PATCH] Add class to represent a session --- .../lib/system/session/Session.class.php | 60 +++++++++++++++++++ .../system/session/SessionHandler.class.php | 20 ++++++- 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/session/Session.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 index 0000000000..0185efe3ba --- /dev/null +++ b/wcfsetup/install/files/lib/system/session/Session.class.php @@ -0,0 +1,60 @@ + + * @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; + } +} diff --git a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php index 526415b3f9..7356781cd9 100644 --- a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php @@ -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; } /** -- 2.20.1