Skip to content
Closed
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion apps/user_ldap/lib/Command/CreateEmptyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
$configPrefix = $this->helper->registerNewServerConfigurationPrefix($configPrefix);
if ($configPrefix === false) {
$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();
Expand Down
24 changes: 21 additions & 3 deletions apps/user_ldap/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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';

Expand Down
Loading