Skip to content

Commit 7d65d93

Browse files
committed
fix: Implement option to temporarily set the user session
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 20dd80d commit 7d65d93

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

apps/files_external/lib/Migration/DummyUserSession.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
class DummyUserSession implements IUserSession {
3131

3232
/**
33-
* @var IUser
33+
* @var ?IUser
3434
*/
3535
private $user;
3636

@@ -44,6 +44,10 @@ public function setUser($user) {
4444
$this->user = $user;
4545
}
4646

47+
public function setActiveUser(?IUser $user): void {
48+
$this->user = $user;
49+
}
50+
4751
public function getUser() {
4852
return $this->user;
4953
}

lib/private/User/Session.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ public function setUser($user) {
211211
$this->activeUser = $user;
212212
}
213213

214+
/**
215+
* Temporarily set the currently active user without persisting in the session
216+
*
217+
* @param IUser|null $user
218+
*/
219+
public function setActiveUser(?IUser $user): void {
220+
$this->activeUser = $user;
221+
}
222+
214223
/**
215224
* get the current active user
216225
*

lib/private/legacy/OC_User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCP\IGroupManager;
4242
use OCP\IUser;
4343
use OCP\IUserManager;
44+
use OCP\IUserSession;
4445
use OCP\Server;
4546
use OCP\User\Events\BeforeUserLoggedInEvent;
4647
use OCP\User\Events\UserLoggedInEvent;
@@ -338,7 +339,7 @@ public static function isAdminUser($uid) {
338339
* @return string|false uid or false
339340
*/
340341
public static function getUser() {
341-
$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
342+
$uid = Server::get(IUserSession::class)->getUser()?->getUID();
342343
if (!is_null($uid) && self::$incognitoMode === false) {
343344
return $uid;
344345
} else {

lib/public/IUserSession.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public function logout();
6363
*/
6464
public function setUser($user);
6565

66+
/**
67+
* Temporarily set the currently active user without persisting in the session
68+
*
69+
* @param IUser|null $user
70+
* @since 29.0.0
71+
*/
72+
public function setActiveUser(?IUser $user): void;
73+
6674
/**
6775
* get the current active user
6876
*

0 commit comments

Comments
 (0)