From e1241f5f71179aa048a7b36a838ff26edfac02c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 19 Mar 2024 11:12:03 +0100 Subject: [PATCH] fix: Implement option to temporarily set the user session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- apps/files_external/lib/Migration/DummyUserSession.php | 9 +++++---- lib/private/User/Session.php | 9 +++++++++ lib/private/legacy/OC_User.php | 4 +++- lib/public/IUserSession.php | 8 ++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/apps/files_external/lib/Migration/DummyUserSession.php b/apps/files_external/lib/Migration/DummyUserSession.php index e1b2b500188c5..ce987b3c575f8 100644 --- a/apps/files_external/lib/Migration/DummyUserSession.php +++ b/apps/files_external/lib/Migration/DummyUserSession.php @@ -29,10 +29,7 @@ class DummyUserSession implements IUserSession { - /** - * @var IUser - */ - private $user; + private ?IUser $user = null; public function login($uid, $password) { } @@ -44,6 +41,10 @@ public function setUser($user) { $this->user = $user; } + public function setVolatileActiveUser(?IUser $user): void { + $this->user = $user; + } + public function getUser() { return $this->user; } diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index e7a9980262e71..32391e35adfc4 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -181,6 +181,15 @@ public function setUser($user) { $this->activeUser = $user; } + /** + * Temporarily set the currently active user without persisting in the session + * + * @param IUser|null $user + */ + public function setVolatileActiveUser(?IUser $user): void { + $this->activeUser = $user; + } + /** * get the current active user * diff --git a/lib/private/legacy/OC_User.php b/lib/private/legacy/OC_User.php index 7943be9d1c57d..238de2a888fa9 100644 --- a/lib/private/legacy/OC_User.php +++ b/lib/private/legacy/OC_User.php @@ -40,6 +40,8 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\ILogger; use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Server; use OCP\User\Events\BeforeUserLoggedInEvent; use OCP\User\Events\UserLoggedInEvent; @@ -349,7 +351,7 @@ public static function isAdminUser($uid) { * @return string|false uid or false */ public static function getUser() { - $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; + $uid = Server::get(IUserSession::class)->getUser()?->getUID(); if (!is_null($uid) && self::$incognitoMode === false) { return $uid; } else { diff --git a/lib/public/IUserSession.php b/lib/public/IUserSession.php index 7bc37cc67c6a1..dc6094550bc63 100644 --- a/lib/public/IUserSession.php +++ b/lib/public/IUserSession.php @@ -63,6 +63,14 @@ public function logout(); */ public function setUser($user); + /** + * Temporarily set the currently active user without persisting in the session + * + * @param IUser|null $user + * @since 29.0.0 + */ + public function setVolatileActiveUser(?IUser $user): void; + /** * get the current active user *