From 22d5d29c015aa62d9a0a0351eb82886ac7374fc8 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Fri, 20 Sep 2024 11:26:22 +0200 Subject: 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 | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'lib/private') diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index cd3e268be48..92a0c735215 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -36,9 +36,12 @@ class LazyUser implements 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 e21759b1e98..3ee748a2775 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -68,6 +68,7 @@ class Manager extends PublicEmitter implements IUserManager { 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 +202,7 @@ class Manager extends PublicEmitter implements IUserManager { $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 +320,16 @@ class Manager extends PublicEmitter implements IUserManager { 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); -- cgit v1.2.3