Skip to content

Commit f314d93

Browse files
authored
Merge pull request nextcloud#52916 from nextcloud/fix/cache-ldap-configuration-prefixes
fix(user_ldap): Store the list of used configuration prefixed in appconfig
2 parents d156bb2 + b9949b2 commit f314d93

13 files changed

Lines changed: 134 additions & 87 deletions

File tree

apps/user_ldap/ajax/deleteConfiguration.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

33
use OCA\User_LDAP\Helper;
4-
use OCP\IConfig;
5-
use OCP\IDBConnection;
64
use OCP\Server;
75
use OCP\Util;
86

@@ -17,7 +15,7 @@
1715
\OC_JSON::callCheck();
1816

1917
$prefix = (string)$_POST['ldap_serverconfig_chooser'];
20-
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
18+
$helper = Server::get(Helper::class);
2119
if ($helper->deleteServerConfiguration($prefix)) {
2220
\OC_JSON::success();
2321
} else {

apps/user_ldap/ajax/getNewServerConfigPrefix.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
use OCA\User_LDAP\Configuration;
44
use OCA\User_LDAP\Helper;
5-
use OCP\IConfig;
6-
use OCP\IDBConnection;
75
use OCP\Server;
86

97
/**
@@ -16,7 +14,7 @@
1614
\OC_JSON::checkAppEnabled('user_ldap');
1715
\OC_JSON::callCheck();
1816

19-
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
17+
$helper = Server::get(Helper::class);
2018
$serverConnections = $helper->getServerConfigurationPrefixes();
2119
sort($serverConnections);
2220
$lk = array_pop($serverConnections);

apps/user_ldap/lib/Command/Search.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use OCA\User_LDAP\LDAP;
1313
use OCA\User_LDAP\User_Proxy;
1414
use OCP\IConfig;
15-
use OCP\IDBConnection;
1615
use OCP\Server;
1716

1817
use Symfony\Component\Console\Command\Command;
@@ -83,7 +82,7 @@ protected function validateOffsetAndLimit(int $offset, int $limit): void {
8382
}
8483

8584
protected function execute(InputInterface $input, OutputInterface $output): int {
86-
$helper = new Helper($this->ocConfig, Server::get(IDBConnection::class));
85+
$helper = Server::get(Helper::class);
8786
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
8887
$ldapWrapper = new LDAP();
8988

apps/user_ldap/lib/Command/SetConfig.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use OCA\User_LDAP\ConnectionFactory;
1212
use OCA\User_LDAP\Helper;
1313
use OCA\User_LDAP\LDAP;
14-
use OCP\IConfig;
15-
use OCP\IDBConnection;
1614
use OCP\Server;
1715
use Symfony\Component\Console\Command\Command;
1816
use Symfony\Component\Console\Input\InputArgument;
@@ -43,7 +41,7 @@ protected function configure(): void {
4341
}
4442

4543
protected function execute(InputInterface $input, OutputInterface $output): int {
46-
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
44+
$helper = Server::get(Helper::class);
4745
$availableConfigs = $helper->getServerConfigurationPrefixes();
4846
$configID = $input->getArgument('configID');
4947
if (!in_array($configID, $availableConfigs)) {

apps/user_ldap/lib/Connection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use OCA\User_LDAP\Exceptions\ConfigurationIssueException;
1212
use OCP\ICache;
1313
use OCP\ICacheFactory;
14-
use OCP\IConfig;
15-
use OCP\IDBConnection;
1614
use OCP\IL10N;
1715
use OCP\Server;
1816
use OCP\Util;
@@ -156,7 +154,7 @@ public function __construct(
156154
if ($memcache->isAvailable()) {
157155
$this->cache = $memcache->createDistributed();
158156
}
159-
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
157+
$helper = Server::get(Helper::class);
160158
$this->doNotValidate = !in_array($this->configPrefix,
161159
$helper->getServerConfigurationPrefixes());
162160
$this->logger = Server::get(LoggerInterface::class);

apps/user_ldap/lib/Helper.php

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use OCP\Cache\CappedMemoryCache;
1111
use OCP\DB\QueryBuilder\IQueryBuilder;
12-
use OCP\IConfig;
12+
use OCP\IAppConfig;
1313
use OCP\IDBConnection;
1414
use OCP\Server;
1515

@@ -18,7 +18,7 @@ class Helper {
1818
protected CappedMemoryCache $sanitizeDnCache;
1919

2020
public function __construct(
21-
private IConfig $config,
21+
private IAppConfig $appConfig,
2222
private IDBConnection $connection,
2323
) {
2424
$this->sanitizeDnCache = new CappedMemoryCache(10000);
@@ -45,21 +45,37 @@ public function __construct(
4545
* except the default (first) server shall be connected to.
4646
*
4747
*/
48-
public function getServerConfigurationPrefixes($activeConfigurations = false): array {
48+
public function getServerConfigurationPrefixes(bool $activeConfigurations = false): array {
49+
$all = $this->getAllServerConfigurationPrefixes();
50+
if (!$activeConfigurations) {
51+
return $all;
52+
}
53+
return array_values(array_filter(
54+
$all,
55+
fn (string $prefix): bool => ($this->appConfig->getValueString('user_ldap', $prefix . 'ldap_configuration_active') === '1')
56+
));
57+
}
58+
59+
protected function getAllServerConfigurationPrefixes(): array {
60+
$unfilled = ['UNFILLED'];
61+
$prefixes = $this->appConfig->getValueArray('user_ldap', 'configuration_prefixes', $unfilled);
62+
if ($prefixes !== $unfilled) {
63+
return $prefixes;
64+
}
65+
66+
/* Fallback to browsing key for migration from Nextcloud<32 */
4967
$referenceConfigkey = 'ldap_configuration_active';
5068

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

5371
$prefixes = [];
5472
foreach ($keys as $key) {
55-
if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') {
56-
continue;
57-
}
58-
5973
$len = strlen($key) - strlen($referenceConfigkey);
6074
$prefixes[] = substr($key, 0, $len);
6175
}
62-
asort($prefixes);
76+
sort($prefixes);
77+
78+
$this->appConfig->setValueArray('user_ldap', 'configuration_prefixes', $prefixes);
6379

6480
return $prefixes;
6581
}
@@ -68,46 +84,45 @@ public function getServerConfigurationPrefixes($activeConfigurations = false): a
6884
*
6985
* determines the host for every configured connection
7086
*
71-
* @return array an array with configprefix as keys
87+
* @return array<string,string> an array with configprefix as keys
7288
*
7389
*/
74-
public function getServerConfigurationHosts() {
75-
$referenceConfigkey = 'ldap_host';
76-
77-
$keys = $this->getServersConfig($referenceConfigkey);
90+
public function getServerConfigurationHosts(): array {
91+
$prefixes = $this->getServerConfigurationPrefixes();
7892

93+
$referenceConfigkey = 'ldap_host';
7994
$result = [];
80-
foreach ($keys as $key) {
81-
$len = strlen($key) - strlen($referenceConfigkey);
82-
$prefix = substr($key, 0, $len);
83-
$result[$prefix] = $this->config->getAppValue('user_ldap', $key);
95+
foreach ($prefixes as $prefix) {
96+
$result[$prefix] = $this->appConfig->getValueString('user_ldap', $prefix . $referenceConfigkey);
8497
}
8598

8699
return $result;
87100
}
88101

89102
/**
90-
* return the next available configuration prefix
91-
*
92-
* @return string
103+
* return the next available configuration prefix and register it as used
93104
*/
94-
public function getNextServerConfigurationPrefix() {
95-
$serverConnections = $this->getServerConfigurationPrefixes();
96-
97-
if (count($serverConnections) === 0) {
98-
return 's01';
105+
public function getNextServerConfigurationPrefix(): string {
106+
$prefixes = $this->getServerConfigurationPrefixes();
107+
108+
if (count($prefixes) === 0) {
109+
$prefix = 's01';
110+
} else {
111+
sort($prefixes);
112+
$lastKey = array_pop($prefixes);
113+
$lastNumber = (int)str_replace('s', '', $lastKey);
114+
$prefix = 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
99115
}
100116

101-
sort($serverConnections);
102-
$lastKey = array_pop($serverConnections);
103-
$lastNumber = (int)str_replace('s', '', $lastKey);
104-
return 's' . str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
117+
$prefixes[] = $prefix;
118+
$this->appConfig->setValueArray('user_ldap', 'configuration_prefixes', $prefixes);
119+
return $prefix;
105120
}
106121

107122
private function getServersConfig(string $value): array {
108123
$regex = '/' . $value . '$/S';
109124

110-
$keys = $this->config->getAppKeys('user_ldap');
125+
$keys = $this->appConfig->getKeys('user_ldap');
111126
$result = [];
112127
foreach ($keys as $key) {
113128
if (preg_match($regex, $key) === 1) {
@@ -125,7 +140,9 @@ private function getServersConfig(string $value): array {
125140
* @return bool true on success, false otherwise
126141
*/
127142
public function deleteServerConfiguration($prefix) {
128-
if (!in_array($prefix, self::getServerConfigurationPrefixes())) {
143+
$prefixes = $this->getServerConfigurationPrefixes();
144+
$index = array_search($prefix, $prefixes);
145+
if ($index === false) {
129146
return false;
130147
}
131148

@@ -144,18 +161,25 @@ public function deleteServerConfiguration($prefix) {
144161
$query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%')));
145162
}
146163

147-
$deletedRows = $query->execute();
164+
$deletedRows = $query->executeStatement();
165+
166+
unset($prefixes[$index]);
167+
$this->appConfig->setValueArray('user_ldap', 'configuration_prefixes', array_values($prefixes));
168+
148169
return $deletedRows !== 0;
149170
}
150171

151172
/**
152173
* checks whether there is one or more disabled LDAP configurations
153174
*/
154175
public function haveDisabledConfigurations(): bool {
155-
$all = $this->getServerConfigurationPrefixes(false);
156-
$active = $this->getServerConfigurationPrefixes(true);
157-
158-
return count($all) !== count($active) || count($all) === 0;
176+
$all = $this->getServerConfigurationPrefixes();
177+
foreach ($all as $prefix) {
178+
if ($this->appConfig->getValueString('user_ldap', $prefix . 'ldap_configuration_active') !== '1') {
179+
return true;
180+
}
181+
}
182+
return false;
159183
}
160184

161185
/**

apps/user_ldap/lib/Jobs/CleanUp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function setArguments($arguments): void {
6767
if (isset($arguments['helper'])) {
6868
$this->ldapHelper = $arguments['helper'];
6969
} else {
70-
$this->ldapHelper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
70+
$this->ldapHelper = Server::get(Helper::class);
7171
}
7272

7373
if (isset($arguments['ocConfig'])) {

apps/user_ldap/lib/Settings/Admin.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use OCA\User_LDAP\Configuration;
99
use OCA\User_LDAP\Helper;
1010
use OCP\AppFramework\Http\TemplateResponse;
11-
use OCP\IConfig;
12-
use OCP\IDBConnection;
1311
use OCP\IL10N;
1412
use OCP\Server;
1513
use OCP\Settings\IDelegatedSettings;
@@ -26,7 +24,7 @@ public function __construct(
2624
* @return TemplateResponse
2725
*/
2826
public function getForm() {
29-
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
27+
$helper = Server::get(Helper::class);
3028
$prefixes = $helper->getServerConfigurationPrefixes();
3129
if (count($prefixes) === 0) {
3230
$newPrefix = $helper->getNextServerConfigurationPrefix();

apps/user_ldap/tests/AccessTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use OCP\IAppConfig;
2626
use OCP\IAvatarManager;
2727
use OCP\IConfig;
28-
use OCP\IDBConnection;
2928
use OCP\Image;
3029
use OCP\IUserManager;
3130
use OCP\Notification\IManager as INotificationManager;
@@ -110,7 +109,7 @@ private function getConnectorAndLdapMock() {
110109
$this->createMock(INotificationManager::class),
111110
$this->shareManager])
112111
->getMock();
113-
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
112+
$helper = Server::get(Helper::class);
114113

115114
return [$lw, $connector, $um, $helper];
116115
}

0 commit comments

Comments
 (0)