From: joshuaruesweg Date: Sat, 24 Oct 2020 15:37:43 +0000 (+0200) Subject: Add class to represent a session X-Git-Tag: 5.4.0_Alpha_1~656^2~17 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=6687beba9a7202fb81e2e5265e05cbe79b6aaed0;p=GitHub%2FWoltLab%2FWCF.git Add class to represent a session --- 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; } /**