Skip to content

Commit

Permalink
fix(users): Don't crash if disabled user is missing in the database
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Sep 19, 2024
1 parent 74c38b8 commit 5a951fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
9 changes: 6 additions & 3 deletions lib/private/User/LazyUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 12 additions & 8 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Check failure on line 71 in lib/private/User/Manager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedThisPropertyAssignment

lib/private/User/Manager.php:71:3: UndefinedThisPropertyAssignment: Instance property OC\User\Manager::$cache is not defined (see https://psalm.dev/040)
$this->listen('\OC\User', 'postDelete', function (IUser $user): void {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5a951fa

Please sign in to comment.