From 1fa384242377025a702be4088ea0c76de4d7d083 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Thu, 19 Sep 2024 15:54:47 +0200 Subject: [PATCH] fix(users): Don't crash if disabled user is missing in the database Signed-off-by: Louis Chemineau --- lib/private/User/LazyUser.php | 9 ++++++--- lib/private/User/Manager.php | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index cd3e268be48bc..92a0c73521514 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -36,9 +36,12 @@ private function getUser(): IUser { $this->user = $this->userManager->get($this->uid); } } - /** @var IUser */ - $user = $this->user; - return $user; + + if ($this->user === null) { + throw new NoUserException('User not found in backend'); + } + + return $this->user; } public function getUID() { diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index e21759b1e9865..773f806750c23 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -60,14 +60,13 @@ class Manager extends PublicEmitter implements IUserManager { */ private array $cachedUsers = []; - private ICache $cache; - private DisplayNameCache $displayNameCache; public function __construct( private IConfig $config, ICacheFactory $cacheFactory, private IEventDispatcher $eventDispatcher, + private LoggerInterface $logger, ) { $this->cache = new WithLocalCache($cacheFactory->createDistributed('user_backend_map')); $this->listen('\OC\User', 'postDelete', function (IUser $user): void { @@ -201,7 +200,7 @@ public function checkPassword($loginName, $password) { $result = $this->checkPasswordNoLogging($loginName, $password); if ($result === false) { - \OCP\Server::get(LoggerInterface::class)->warning('Login failed: \'' . $loginName . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']); + $this->logger->warning('Login failed: \'' . $loginName . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']); } return $result; @@ -319,11 +318,16 @@ public function getDisabledUsers(?int $limit = null, int $offset = 0, string $se if ($search !== '') { $users = array_filter( $users, - fn (IUser $user): bool => - mb_stripos($user->getUID(), $search) !== false || - mb_stripos($user->getDisplayName(), $search) !== false || - mb_stripos($user->getEMailAddress() ?? '', $search) !== false, - ); + function (IUser $user) use ($search): bool { + try { + return mb_stripos($user->getUID(), $search) !== false || + mb_stripos($user->getDisplayName(), $search) !== false || + mb_stripos($user->getEMailAddress() ?? '', $search) !== false; + } catch (NoUserException $ex) { + $this->logger->error('Error while filtering disabled users', ['exception' => $ex, 'userUID' => $user->getUID()]); + return false; + } + }); } $tempLimit = ($limit === null ? null : $limit + $offset);