From e235c7195e549b9ce2d20bfe0c85cc967b1f80c7 Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 30 Sep 2025 11:37:33 +0200 Subject: [PATCH 1/2] Implement manual prefix setting for configuration Add option to manually set configuration prefix with validation. Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- .../lib/Command/CreateEmptyConfig.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/user_ldap/lib/Command/CreateEmptyConfig.php b/apps/user_ldap/lib/Command/CreateEmptyConfig.php index 7c381cf431fb4..a3fc83a1b6ae9 100644 --- a/apps/user_ldap/lib/Command/CreateEmptyConfig.php +++ b/apps/user_ldap/lib/Command/CreateEmptyConfig.php @@ -31,11 +31,34 @@ protected function configure(): void { InputOption::VALUE_NONE, 'outputs only the prefix' ) + ->addOption( + 'set-prefix', + 's', + InputOption::VALUE_OPTIONAL, + 'manually set the prefix (must be unique)', + ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { - $configPrefix = $this->helper->getNextServerConfigurationPrefix(); + $configPrefix = $input->getOption('set-prefix'); + if (is_string($configPrefix)) { + if ($configPrefix === '') { + $output->writeln('The prefix cannot be empty'); + return self::FAILURE; + } + if (preg_match('/^[a-zA-Z0-9-_]+$/', $configPrefix) !== 1) { + $output->writeln('The prefix may only contain alphanumeric characters, dashes and underscores'); + return self::FAILURE; + } + $availableConfigs = $this->helper->getServerConfigurationPrefixes(); + if (in_array($configPrefix, $availableConfigs)) { + $output->writeln('The prefix is already in use'); + return self::FAILURE; + } + } else { + $configPrefix = $this->helper->getNextServerConfigurationPrefix(); + } $configHolder = new Configuration($configPrefix); $configHolder->ldapConfigurationActive = false; $configHolder->saveConfiguration(); From dedc87776fecff8b34e1d1ed75980c8a83604dfa Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 30 Sep 2025 16:02:48 +0200 Subject: [PATCH 2/2] Add helper functions Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- .../lib/Command/CreateEmptyConfig.php | 4 ++-- apps/user_ldap/lib/Helper.php | 24 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/user_ldap/lib/Command/CreateEmptyConfig.php b/apps/user_ldap/lib/Command/CreateEmptyConfig.php index a3fc83a1b6ae9..5c9ebddd1713f 100644 --- a/apps/user_ldap/lib/Command/CreateEmptyConfig.php +++ b/apps/user_ldap/lib/Command/CreateEmptyConfig.php @@ -51,8 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('The prefix may only contain alphanumeric characters, dashes and underscores'); return self::FAILURE; } - $availableConfigs = $this->helper->getServerConfigurationPrefixes(); - if (in_array($configPrefix, $availableConfigs)) { + $configPrefix = $this->helper->registerNewServerConfigurationPrefix($configPrefix); + if ($configPrefix === false) { $output->writeln('The prefix is already in use'); return self::FAILURE; } diff --git a/apps/user_ldap/lib/Helper.php b/apps/user_ldap/lib/Helper.php index d3abf04fd1e9c..f4cd654f8a9f2 100644 --- a/apps/user_ldap/lib/Helper.php +++ b/apps/user_ldap/lib/Helper.php @@ -105,11 +105,13 @@ public function getServerConfigurationHosts(): array { public function getNextServerConfigurationPrefix(): string { $prefixes = $this->getServerConfigurationPrefixes(); - if (count($prefixes) === 0) { + $filteredPrefixes = array_filter($prefixes, fn($p) => preg_match('/^s\d{2}$/', $p)); + + if (count($filteredPrefixes) === 0) { $prefix = 's01'; } else { - sort($prefixes); - $lastKey = array_pop($prefixes); + sort($filteredPrefixes); + $lastKey = array_pop($filteredPrefixes); $lastNumber = (int)str_replace('s', '', $lastKey); $prefix = 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT); } @@ -119,6 +121,22 @@ public function getNextServerConfigurationPrefix(): string { return $prefix; } + /** + * registers the given prefix as used, if it is not already in use + * + * @param string $prefix the prefix to register + * @return string|false the given prefix on success, false if it is already in use + */ + public function registerNewServerConfigurationPrefix(string $prefix): string|false { + $prefixes = $this->getServerConfigurationPrefixes(); + if (in_array($prefix, $prefixes)) { + return false; + } + $prefixes[] = $prefix; + $this->appConfig->setValueArray('user_ldap', 'configuration_prefixes', $prefixes); + return $prefix; + } + private function getServersConfig(string $value): array { $regex = '/' . $value . '$/S';