Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/User/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use OCP\ServerVersion;
use OCP\User\Backend\ABackend;
Expand Down Expand Up @@ -344,10 +345,12 @@ public function getCurrentUserId(): string {
}

$this->session->set('last-password-confirm', strtotime('+4 year', time()));
$this->setSessionUser($userId);
return $userId;
} elseif ($this->userExists($tokenUserId)) {
$this->checkFirstLogin($tokenUserId);
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
$this->setSessionUser($tokenUserId);
return $tokenUserId;
} else {
// check if the user exists locally
Expand All @@ -369,6 +372,7 @@ public function getCurrentUserId(): string {
}
$this->checkFirstLogin($tokenUserId);
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
$this->setSessionUser($tokenUserId);
return $tokenUserId;
}
}
Expand All @@ -380,6 +384,31 @@ public function getCurrentUserId(): string {
return '';
}

/**
* Set the user in IUserSession after bearer token validation.
* Without this, DI-injected $userId is null in OCS controllers
* and CalDAV plugins, causing 500 errors in Deck, Talk, and Tasks.
*
* Note: IUserSession is resolved via Server::get() rather than constructor
* injection to avoid a circular dependency (IUserSession depends on this Backend).
*/
private function setSessionUser(string $userId): void {
try {
$userSession = Server::get(IUserSession::class);
$currentUser = $userSession->getUser();

// Only fetch and set if the session doesn't already have this user
if ($currentUser === null || $currentUser->getUID() !== $userId) {
$user = $this->userManager->get($userId);
if ($user !== null) {
$userSession->setUser($user);
}
}
} catch (\Throwable $e) {
$this->logger->debug('Failed to set session user after bearer validation: ' . $e->getMessage());
}
}
Comment thread
hangerrits marked this conversation as resolved.

/**
* Inspired by lib/private/User/Session.php::prepareUserLogin()
*
Expand Down
Loading