From b15e4d518a6b1d41398386866ce8b45148bf7b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 12 Feb 2024 14:17:27 +0100 Subject: [PATCH] fix(user_ldap): Do not block access to configuration page upon bad configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It should be possible to access configuration page with the local admin user even when LDAP configuration is not valid. This prevent ldap backend from throwing in countUsers to allow this. Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/Connection.php | 1 + apps/user_ldap/lib/User_LDAP.php | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/user_ldap/lib/Connection.php b/apps/user_ldap/lib/Connection.php index 37f7dcaea5c..4b890d8a311 100644 --- a/apps/user_ldap/lib/Connection.php +++ b/apps/user_ldap/lib/Connection.php @@ -241,6 +241,7 @@ class Connection extends LDAPUtility { } /** + * @throws ServerNotAvailableException When connection failed or configuration is invalid * @return resource|\LDAP\Connection The LDAP resource */ public function getConnectionResource() { diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index d787bfea4d4..e014832b216 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -300,7 +300,7 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user * name or an instance of that user * @throws \Exception - * @throws \OC\ServerNotAvailableException + * @throws ServerNotAvailableException */ public function userExistsOnLDAP($user, bool $ignoreCache = false): bool { if (is_string($user)) { @@ -586,14 +586,18 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I return $this->userPluginManager->countUsers(); } - $filter = $this->access->getFilterForUserCount(); - $cacheKey = 'countUsers-'.$filter; - if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { + try { + $filter = $this->access->getFilterForUserCount(); + $cacheKey = 'countUsers-'.$filter; + if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { + return $entries; + } + $entries = $this->access->countUsers($filter); + $this->access->connection->writeToCache($cacheKey, $entries); return $entries; + } catch (ServerNotAvailableException $e) { + return false; } - $entries = $this->access->countUsers($filter); - $this->access->connection->writeToCache($cacheKey, $entries); - return $entries; } public function countMappedUsers(): int {