fix: Implement option to temporarily set the user session

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2024-03-19 11:12:03 +01:00
parent 20dd80d2fd
commit e330efe5a0
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
4 changed files with 24 additions and 5 deletions

View File

@ -29,10 +29,7 @@ use OCP\IUserSession;
class DummyUserSession implements IUserSession {
/**
* @var IUser
*/
private $user;
private ?IUser $user = null;
public function login($uid, $password) {
}
@ -44,6 +41,10 @@ class DummyUserSession implements IUserSession {
$this->user = $user;
}
public function setVolatileActiveUser(?IUser $user): void {
$this->user = $user;
}
public function getUser() {
return $this->user;
}

View File

@ -211,6 +211,15 @@ class Session implements IUserSession, Emitter {
$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
*

View File

@ -41,6 +41,7 @@ use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use OCP\User\Events\BeforeUserLoggedInEvent;
use OCP\User\Events\UserLoggedInEvent;
@ -338,7 +339,7 @@ class OC_User {
* @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 {

View File

@ -63,6 +63,14 @@ interface IUserSession {
*/
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
*