Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(user_ldap): Store the list of used configuration prefixed in appconfig #52916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from
Draft
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
fix(user_ldap): Store the list of used configuration prefixed in appc…
…onfig

This avoids getting all keys from appconfig, which was triggering
 loading of lazy configuration on all requests.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed May 16, 2025
commit 1bf0217a4fc166efecc1913a1697c5ed31b94fb2
90 changes: 58 additions & 32 deletions 90 apps/user_ldap/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\User_LDAP;

use OCP\AppFramework\Services\IAppConfig;
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
Expand All @@ -19,6 +20,7 @@ class Helper {

public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private IDBConnection $connection,
) {
$this->sanitizeDnCache = new CappedMemoryCache(10000);
Expand All @@ -45,21 +47,37 @@ public function __construct(
* except the default (first) server shall be connected to.
*
*/
public function getServerConfigurationPrefixes($activeConfigurations = false): array {
public function getServerConfigurationPrefixes(bool $activeConfigurations): array {
$all = $this->getAllServerConfigurationPrefixes();
if (!$activeConfigurations) {
return $all;
}
return array_values(array_filter(
$all,
fn (string $prefix) => ($this->config->getAppValue('user_ldap', $prefix . 'ldap_configuration_active') === '1')
));
}

protected function getAllServerConfigurationPrefixes(): array {
$unfilled = ['UNFILLED'];
$prefixes = $this->appconfig->getAppValueArray('configuration_prefixes', $unfilled);
if ($prefixes !== $unfilled) {
return $prefixes;
}

/* Fallback to browsing key for migration from Nextcloud<32 */
$referenceConfigkey = 'ldap_configuration_active';

$keys = $this->getServersConfig($referenceConfigkey);

$prefixes = [];
foreach ($keys as $key) {
if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') {
continue;
}

$len = strlen($key) - strlen($referenceConfigkey);
$prefixes[] = substr($key, 0, $len);
}
asort($prefixes);
sort($prefixes);

$this->appConfig->setAppValueArray('configuration_prefixes', $prefixes);

return $prefixes;
}
Expand All @@ -71,37 +89,36 @@ public function getServerConfigurationPrefixes($activeConfigurations = false): a
* @return array an array with configprefix as keys
*
*/
public function getServerConfigurationHosts() {
$referenceConfigkey = 'ldap_host';

$keys = $this->getServersConfig($referenceConfigkey);
public function getServerConfigurationHosts(): array {
$prefixes = $this->getServerConfigurationPrefixes();

$referenceConfigkey = 'ldap_host';
$result = [];
foreach ($keys as $key) {
$len = strlen($key) - strlen($referenceConfigkey);
$prefix = substr($key, 0, $len);
$result[$prefix] = $this->config->getAppValue('user_ldap', $key);
foreach ($prefixes as $prefix) {
$result[$prefix] = $this->config->getAppValue('user_ldap', $prefix . $referenceConfigkey);
}

return $result;
}

/**
* return the next available configuration prefix
*
* @return string
* return the next available configuration prefix and register it as used
*/
public function getNextServerConfigurationPrefix() {
$serverConnections = $this->getServerConfigurationPrefixes();

if (count($serverConnections) === 0) {
return 's01';
public function getNextServerConfigurationPrefix(): string {
$prefixes = $this->getServerConfigurationPrefixes();

if (count($prefixes) === 0) {
$prefix = 's01';
} else {
sort($prefixes);
$lastKey = array_pop($prefixes);
$lastNumber = (int)str_replace('s', '', $lastKey);
$prefix = 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
}

sort($serverConnections);
$lastKey = array_pop($serverConnections);
$lastNumber = (int)str_replace('s', '', $lastKey);
return 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
$prefixes[] = $prefix;
$this->appConfig->setAppValueArray('configuration_prefixes', $prefixes);
return $prefix;
}

private function getServersConfig(string $value): array {
Expand All @@ -125,7 +142,9 @@ private function getServersConfig(string $value): array {
* @return bool true on success, false otherwise
*/
public function deleteServerConfiguration($prefix) {
if (!in_array($prefix, self::getServerConfigurationPrefixes())) {
$prefixes = $this->getServerConfigurationPrefixes();
$index = array_search($prefix, $prefixes);
if ($index === false) {
return false;
}

Expand All @@ -144,18 +163,25 @@ public function deleteServerConfiguration($prefix) {
$query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%')));
}

$deletedRows = $query->execute();
$deletedRows = $query->executeStatement();

unset($prefixes[$index]);
$this->appConfig->setAppValueArray('configuration_prefixes', array_values($prefixes));

return $deletedRows !== 0;
}

/**
* checks whether there is one or more disabled LDAP configurations
*/
public function haveDisabledConfigurations(): bool {
$all = $this->getServerConfigurationPrefixes(false);
$active = $this->getServerConfigurationPrefixes(true);

return count($all) !== count($active) || count($all) === 0;
$all = $this->getServerConfigurationPrefixes();
foreach ($all as $prefix) {
if ($this->config->getAppValue('user_ldap', $prefix . 'ldap_configuration_active') !== '1') {
return true;
}
}
return false;
}

/**
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.