Skip to content

Commit

Permalink
fix: Implement option to temporarily set the user session
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Mar 19, 2024
1 parent 20dd80d commit 4131f98
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
9 changes: 5 additions & 4 deletions apps/files_external/lib/Migration/DummyUserSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@

class DummyUserSession implements IUserSession {

/**
* @var IUser
*/
private $user;
private ?IUser $user;

public function login($uid, $password) {
}
Expand All @@ -44,6 +41,10 @@ public function setUser($user) {
$this->user = $user;
}

public function setActiveUser(?IUser $user): void {
$this->user = $user;
}

public function getUser() {
return $this->user;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,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 setActiveUser(?IUser $user): void {
$this->activeUser = $user;
}

/**
* get the current active user
*
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -338,7 +339,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 {
Expand Down
8 changes: 8 additions & 0 deletions lib/public/IUserSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 setActiveUser(?IUser $user): void;

/**
* get the current active user
*
Expand Down

0 comments on commit 4131f98

Please sign in to comment.