Skip to content

Commit dedc877

Browse files
committed
Add helper functions
Signed-off-by: Jean-Yves <[email protected]>
1 parent e235c71 commit dedc877

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

apps/user_ldap/lib/Command/CreateEmptyConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151
$output->writeln('The prefix may only contain alphanumeric characters, dashes and underscores');
5252
return self::FAILURE;
5353
}
54-
$availableConfigs = $this->helper->getServerConfigurationPrefixes();
55-
if (in_array($configPrefix, $availableConfigs)) {
54+
$configPrefix = $this->helper->registerNewServerConfigurationPrefix($configPrefix);
55+
if ($configPrefix === false) {
5656
$output->writeln('The prefix is already in use');
5757
return self::FAILURE;
5858
}

apps/user_ldap/lib/Helper.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ public function getServerConfigurationHosts(): array {
105105
public function getNextServerConfigurationPrefix(): string {
106106
$prefixes = $this->getServerConfigurationPrefixes();
107107

108-
if (count($prefixes) === 0) {
108+
$filteredPrefixes = array_filter($prefixes, fn($p) => preg_match('/^s\d{2}$/', $p));
109+
110+
if (count($filteredPrefixes) === 0) {
109111
$prefix = 's01';
110112
} else {
111-
sort($prefixes);
112-
$lastKey = array_pop($prefixes);
113+
sort($filteredPrefixes);
114+
$lastKey = array_pop($filteredPrefixes);
113115
$lastNumber = (int)str_replace('s', '', $lastKey);
114116
$prefix = 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
115117
}
@@ -119,6 +121,22 @@ public function getNextServerConfigurationPrefix(): string {
119121
return $prefix;
120122
}
121123

124+
/**
125+
* registers the given prefix as used, if it is not already in use
126+
*
127+
* @param string $prefix the prefix to register
128+
* @return string|false the given prefix on success, false if it is already in use
129+
*/
130+
public function registerNewServerConfigurationPrefix(string $prefix): string|false {
131+
$prefixes = $this->getServerConfigurationPrefixes();
132+
if (in_array($prefix, $prefixes)) {
133+
return false;
134+
}
135+
$prefixes[] = $prefix;
136+
$this->appConfig->setValueArray('user_ldap', 'configuration_prefixes', $prefixes);
137+
return $prefix;
138+
}
139+
122140
private function getServersConfig(string $value): array {
123141
$regex = '/' . $value . '$/S';
124142

0 commit comments

Comments
 (0)