Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(user_ldap): Switch to OCP\IAppConfig and fix Helper constructor c…
…alls

Using OCP\AppFramework\Services\IAppConfig is not possible because the
 Helper is queried from places outside of the application DI container
(ajax pages, tests, other applications through ILDAPProviderFactory…)

Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Jun 10, 2025
commit f48e5aa1f30ba2d3f3a27c4193420be5433e8856
4 changes: 1 addition & 3 deletions apps/user_ldap/ajax/deleteConfiguration.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use OCA\User_LDAP\Helper;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Server;
use OCP\Util;

Expand All @@ -17,7 +15,7 @@
\OC_JSON::callCheck();

$prefix = (string)$_POST['ldap_serverconfig_chooser'];
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);
if ($helper->deleteServerConfiguration($prefix)) {
\OC_JSON::success();
} else {
Expand Down
4 changes: 1 addition & 3 deletions apps/user_ldap/ajax/getNewServerConfigPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\Helper;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Server;

/**
Expand All @@ -16,7 +14,7 @@
\OC_JSON::checkAppEnabled('user_ldap');
\OC_JSON::callCheck();

$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);
$serverConnections = $helper->getServerConfigurationPrefixes();
sort($serverConnections);
$lk = array_pop($serverConnections);
Expand Down
3 changes: 1 addition & 2 deletions apps/user_ldap/lib/Command/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\User_Proxy;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Server;

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

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

Expand Down
4 changes: 1 addition & 3 deletions apps/user_ldap/lib/Command/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use OCA\User_LDAP\ConnectionFactory;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Server;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -43,7 +41,7 @@ protected function configure(): void {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);
$availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) {
Expand Down
4 changes: 1 addition & 3 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use OCA\User_LDAP\Exceptions\ConfigurationIssueException;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\Server;
use OCP\Util;
Expand Down Expand Up @@ -156,7 +154,7 @@ public function __construct(
if ($memcache->isAvailable()) {
$this->cache = $memcache->createDistributed();
}
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);
$this->doNotValidate = !in_array($this->configPrefix,
$helper->getServerConfigurationPrefixes());
$this->logger = Server::get(LoggerInterface::class);
Expand Down
18 changes: 9 additions & 9 deletions apps/user_ldap/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/
namespace OCA\User_LDAP;

use OCP\AppFramework\Services\IAppConfig;
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\Server;

Expand Down Expand Up @@ -52,13 +52,13 @@ public function getServerConfigurationPrefixes(bool $activeConfigurations = fals
}
return array_values(array_filter(
$all,
fn (string $prefix): bool => ($this->appConfig->getAppValueString($prefix . 'ldap_configuration_active') === '1')
fn (string $prefix): bool => ($this->appConfig->getValueString('user_ldap', $prefix . 'ldap_configuration_active') === '1')
));
}

protected function getAllServerConfigurationPrefixes(): array {
$unfilled = ['UNFILLED'];
$prefixes = $this->appConfig->getAppValueArray('configuration_prefixes', $unfilled);
$prefixes = $this->appConfig->getValueArray('user_ldap', 'configuration_prefixes', $unfilled);
if ($prefixes !== $unfilled) {
return $prefixes;
}
Expand All @@ -75,7 +75,7 @@ protected function getAllServerConfigurationPrefixes(): array {
}
sort($prefixes);

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

return $prefixes;
}
Expand All @@ -93,7 +93,7 @@ public function getServerConfigurationHosts(): array {
$referenceConfigkey = 'ldap_host';
$result = [];
foreach ($prefixes as $prefix) {
$result[$prefix] = $this->appConfig->getAppValueString($prefix . $referenceConfigkey);
$result[$prefix] = $this->appConfig->getValueString('user_ldap', $prefix . $referenceConfigkey);
}

return $result;
Expand All @@ -115,14 +115,14 @@ public function getNextServerConfigurationPrefix(): string {
}

$prefixes[] = $prefix;
$this->appConfig->setAppValueArray('configuration_prefixes', $prefixes);
$this->appConfig->setValueArray('user_ldap', 'configuration_prefixes', $prefixes);
return $prefix;
}

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

$keys = $this->appConfig->getAppKeys();
$keys = $this->appConfig->getKeys('user_ldap');
$result = [];
foreach ($keys as $key) {
if (preg_match($regex, $key) === 1) {
Expand Down Expand Up @@ -164,7 +164,7 @@ public function deleteServerConfiguration($prefix) {
$deletedRows = $query->executeStatement();

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

return $deletedRows !== 0;
}
Expand All @@ -175,7 +175,7 @@ public function deleteServerConfiguration($prefix) {
public function haveDisabledConfigurations(): bool {
$all = $this->getServerConfigurationPrefixes();
foreach ($all as $prefix) {
if ($this->appConfig->getAppValueString($prefix . 'ldap_configuration_active') !== '1') {
if ($this->appConfig->getValueString('user_ldap', $prefix . 'ldap_configuration_active') !== '1') {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Jobs/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function setArguments($arguments): void {
if (isset($arguments['helper'])) {
$this->ldapHelper = $arguments['helper'];
} else {
$this->ldapHelper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$this->ldapHelper = Server::get(Helper::class);
}

if (isset($arguments['ocConfig'])) {
Expand Down
4 changes: 1 addition & 3 deletions apps/user_ldap/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\Helper;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\Server;
use OCP\Settings\IDelegatedSettings;
Expand All @@ -26,7 +24,7 @@ public function __construct(
* @return TemplateResponse
*/
public function getForm() {
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);
$prefixes = $helper->getServerConfigurationPrefixes();
if (count($prefixes) === 0) {
$newPrefix = $helper->getNextServerConfigurationPrefix();
Expand Down
3 changes: 1 addition & 2 deletions apps/user_ldap/tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use OCP\IAppConfig;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
Expand Down Expand Up @@ -110,7 +109,7 @@ private function getConnectorAndLdapMock() {
$this->createMock(INotificationManager::class),
$this->shareManager])
->getMock();
$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);

return [$lw, $connector, $um, $helper];
}
Expand Down
47 changes: 25 additions & 22 deletions apps/user_ldap/tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace OCA\User_LDAP\Tests;

use OCA\User_LDAP\Helper;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -32,36 +32,38 @@ protected function setUp(): void {
}

public function testGetServerConfigurationPrefixes(): void {
$this->appConfig->method('getAppKeys')
$this->appConfig->method('getKeys')
->with('user_ldap')
->willReturn([
'foo',
'ldap_configuration_active',
's1ldap_configuration_active',
]);

$this->appConfig->method('getAppValueArray')
->with('configuration_prefixes')
-> willReturnArgument(1);
$this->appConfig->method('getValueArray')
->with('user_ldap', 'configuration_prefixes')
-> willReturnArgument(2);

$result = $this->helper->getServerConfigurationPrefixes(false);

$this->assertEquals(['', 's1'], $result);
}

public function testGetServerConfigurationPrefixesActive(): void {
$this->appConfig->method('getAppKeys')
$this->appConfig->method('getKeys')
->with('user_ldap')
->willReturn([
'foo',
'ldap_configuration_active',
's1ldap_configuration_active',
]);

$this->appConfig->method('getAppValueArray')
->with('configuration_prefixes')
-> willReturnArgument(1);
$this->appConfig->method('getValueArray')
->with('user_ldap', 'configuration_prefixes')
-> willReturnArgument(2);

$this->appConfig->method('getAppValueString')
->willReturnCallback(function ($key, $default) {
$this->appConfig->method('getValueString')
->willReturnCallback(function ($app, $key, $default) {
if ($key === 's1ldap_configuration_active') {
return '1';
}
Expand All @@ -74,7 +76,8 @@ public function testGetServerConfigurationPrefixesActive(): void {
}

public function testGetServerConfigurationHostFromAppKeys(): void {
$this->appConfig->method('getAppKeys')
$this->appConfig->method('getKeys')
->with('user_ldap')
->willReturn([
'foo',
'ldap_host',
Expand All @@ -85,12 +88,12 @@ public function testGetServerConfigurationHostFromAppKeys(): void {
's02ldap_configuration_active',
]);

$this->appConfig->method('getAppValueArray')
->with('configuration_prefixes')
-> willReturnArgument(1);
$this->appConfig->method('getValueArray')
->with('user_ldap', 'configuration_prefixes')
-> willReturnArgument(2);

$this->appConfig->method('getAppValueString')
->willReturnCallback(function ($key, $default) {
$this->appConfig->method('getValueString')
->willReturnCallback(function ($app, $key, $default) {
if ($key === 'ldap_host') {
return 'example.com';
}
Expand All @@ -112,18 +115,18 @@ public function testGetServerConfigurationHostFromAppKeys(): void {
public function testGetServerConfigurationHost(): void {
$this->appConfig
->expects(self::never())
->method('getAppKeys');
->method('getKeys');

$this->appConfig->method('getAppValueArray')
->with('configuration_prefixes')
$this->appConfig->method('getValueArray')
->with('user_ldap', 'configuration_prefixes')
-> willReturn([
'',
's1',
's02',
]);

$this->appConfig->method('getAppValueString')
->willReturnCallback(function ($key, $default) {
$this->appConfig->method('getValueString')
->willReturnCallback(function ($app, $key, $default) {
if ($key === 'ldap_host') {
return 'example.com';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use OCA\User_LDAP\UserPluginManager;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager;
use OCP\Server;
Expand Down Expand Up @@ -125,7 +124,7 @@ protected function initUserManager() {
* initializes the test Helper
*/
protected function initHelper() {
$this->helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$this->helper = Server::get(Helper::class);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions apps/user_ldap/tests/LDAPProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IServerContainer;
use OCP\Server;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -199,7 +198,7 @@ public function testDNasBaseParameter(): void {

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals(
Expand All @@ -212,7 +211,7 @@ public function testSanitizeDN(): void {

$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());

$helper = new Helper(Server::get(IConfig::class), Server::get(IDBConnection::class));
$helper = Server::get(Helper::class);

$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals(
Expand Down