Skip to content

Commit 5fa368c

Browse files
authored
Merge pull request #57100 from nextcloud/fix/remove-iservercontainer-from-user-ldap
fix(user_ldap): Remove usages of deprecated IServerContainer
2 parents 0e6c8ec + 279d5c2 commit 5fa368c

12 files changed

Lines changed: 94 additions & 209 deletions

File tree

apps/user_ldap/lib/AppInfo/Application.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -8,7 +10,6 @@
810

911
use Closure;
1012
use OCA\Files_External\Service\BackendService;
11-
use OCA\User_LDAP\Controller\RenewPasswordController;
1213
use OCA\User_LDAP\Events\GroupBackendRegistered;
1314
use OCA\User_LDAP\Events\UserBackendRegistered;
1415
use OCA\User_LDAP\Group_Proxy;
@@ -30,16 +31,12 @@
3031
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3132
use OCP\AppFramework\IAppContainer;
3233
use OCP\AppFramework\Services\IAppConfig;
33-
use OCP\AppFramework\Services\IInitialState;
3434
use OCP\Config\IUserConfig;
3535
use OCP\EventDispatcher\IEventDispatcher;
3636
use OCP\IAvatarManager;
3737
use OCP\IConfig;
3838
use OCP\IGroupManager;
39-
use OCP\IL10N;
4039
use OCP\Image;
41-
use OCP\IRequest;
42-
use OCP\IURLGenerator;
4340
use OCP\IUserManager;
4441
use OCP\Notification\IManager as INotificationManager;
4542
use OCP\Share\IManager as IShareManager;
@@ -53,33 +50,11 @@ class Application extends App implements IBootstrap {
5350

5451
public function __construct() {
5552
parent::__construct(self::APP_ID);
56-
$container = $this->getContainer();
57-
58-
/**
59-
* Controller
60-
*/
61-
$container->registerService('RenewPasswordController', function (ContainerInterface $appContainer) {
62-
return new RenewPasswordController(
63-
$appContainer->get('AppName'),
64-
$appContainer->get(IRequest::class),
65-
$appContainer->get(IUserManager::class),
66-
$appContainer->get(IConfig::class),
67-
$appContainer->get(IUserConfig::class),
68-
$appContainer->get(IL10N::class),
69-
$appContainer->get('Session'),
70-
$appContainer->get(IURLGenerator::class),
71-
$appContainer->get(IInitialState::class),
72-
);
73-
});
74-
75-
$container->registerService(ILDAPWrapper::class, function (ContainerInterface $appContainer) {
76-
return new LDAP(
77-
$appContainer->get(IConfig::class)->getSystemValueString('ldap_log_file')
78-
);
79-
});
8053
}
8154

8255
public function register(IRegistrationContext $context): void {
56+
$context->registerServiceAlias(ILDAPWrapper::class, LDAP::class);
57+
8358
$context->registerNotifierService(Notifier::class);
8459

8560
$context->registerService(

apps/user_ldap/lib/Command/Search.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
namespace OCA\User_LDAP\Command;
99

1010
use OCA\User_LDAP\Group_Proxy;
11-
use OCA\User_LDAP\Helper;
12-
use OCA\User_LDAP\LDAP;
1311
use OCA\User_LDAP\User_Proxy;
1412
use OCP\IConfig;
15-
use OCP\Server;
1613

1714
use Symfony\Component\Console\Command\Command;
1815
use Symfony\Component\Console\Input\InputArgument;
@@ -82,10 +79,6 @@ protected function validateOffsetAndLimit(int $offset, int $limit): void {
8279
}
8380

8481
protected function execute(InputInterface $input, OutputInterface $output): int {
85-
$helper = Server::get(Helper::class);
86-
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
87-
$ldapWrapper = new LDAP();
88-
8982
$offset = (int)$input->getOption('offset');
9083
$limit = (int)$input->getOption('limit');
9184
$this->validateOffsetAndLimit($offset, $limit);

apps/user_ldap/lib/Command/SetConfig.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
use OCA\User_LDAP\Configuration;
1111
use OCA\User_LDAP\ConnectionFactory;
1212
use OCA\User_LDAP\Helper;
13-
use OCA\User_LDAP\LDAP;
14-
use OCP\Server;
1513
use Symfony\Component\Console\Command\Command;
1614
use Symfony\Component\Console\Input\InputArgument;
1715
use Symfony\Component\Console\Input\InputInterface;
1816
use Symfony\Component\Console\Output\OutputInterface;
1917

2018
class SetConfig extends Command {
19+
public function __construct(
20+
private readonly Helper $helper,
21+
private readonly ConnectionFactory $connectionFactory,
22+
) {
23+
parent::__construct();
24+
}
25+
2126
protected function configure(): void {
2227
$this
2328
->setName('ldap:set-config')
@@ -41,8 +46,7 @@ protected function configure(): void {
4146
}
4247

4348
protected function execute(InputInterface $input, OutputInterface $output): int {
44-
$helper = Server::get(Helper::class);
45-
$availableConfigs = $helper->getServerConfigurationPrefixes();
49+
$availableConfigs = $this->helper->getServerConfigurationPrefixes();
4650
$configID = $input->getArgument('configID');
4751
if (!in_array($configID, $availableConfigs)) {
4852
$output->writeln('Invalid configID');
@@ -65,7 +69,6 @@ protected function setValue(string $configID, string $key, string $value): void
6569
$configHolder->$key = $value;
6670
$configHolder->saveConfiguration();
6771

68-
$connectionFactory = new ConnectionFactory(new LDAP());
69-
$connectionFactory->get($configID)->clearCache();
72+
$this->connectionFactory->get($configID)->clearCache();
7073
}
7174
}

apps/user_ldap/lib/Jobs/Sync.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class Sync extends TimedJob {
2323
public const MAX_INTERVAL = 12 * 60 * 60; // 12h
2424
public const MIN_INTERVAL = 30 * 60; // 30min
2525

26-
protected LDAP $ldap;
27-
2826
public function __construct(
2927
ITimeFactory $timeFactory,
3028
private IConfig $config,
@@ -42,7 +40,6 @@ public function __construct(
4240
self::MIN_INTERVAL
4341
)
4442
);
45-
$this->ldap = new LDAP($this->config->getSystemValueString('ldap_log_file'));
4643
}
4744

4845
/**
@@ -253,11 +250,4 @@ protected function getNextPrefix(?string $lastPrefix): ?string {
253250
}
254251
return $prefixes[$i];
255252
}
256-
257-
/**
258-
* Only used in tests
259-
*/
260-
public function overwritePropertiesForTest(LDAP $ldapWrapper): void {
261-
$this->ldap = $ldapWrapper;
262-
}
263253
}

apps/user_ldap/lib/LDAP.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@
1818

1919
class LDAP implements ILDAPWrapper {
2020
protected array $curArgs = [];
21-
protected LoggerInterface $logger;
22-
protected IConfig $config;
2321

2422
private ?LdapDataCollector $dataCollector = null;
2523

24+
protected string $logFile = '';
25+
2626
public function __construct(
27-
protected string $logFile = '',
27+
IProfiler $profiler,
28+
protected IConfig $config,
29+
protected LoggerInterface $logger,
2830
) {
29-
/** @var IProfiler $profiler */
30-
$profiler = Server::get(IProfiler::class);
3131
if ($profiler->isEnabled()) {
3232
$this->dataCollector = new LdapDataCollector();
3333
$profiler->add($this->dataCollector);
3434
}
35-
36-
$this->logger = Server::get(LoggerInterface::class);
37-
$this->config = Server::get(IConfig::class);
35+
$this->logFile = $this->config->getSystemValueString('ldap_log_file');
3836
}
3937

4038
/**

apps/user_ldap/lib/LDAPProvider.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,46 @@
55
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
66
* SPDX-License-Identifier: AGPL-3.0-or-later
77
*/
8+
89
namespace OCA\User_LDAP;
910

1011
use OCA\User_LDAP\User\DeletedUsersIndex;
11-
use OCP\IServerContainer;
12+
use OCP\GroupInterface;
13+
use OCP\IGroupManager;
14+
use OCP\IUserManager;
1215
use OCP\LDAP\IDeletionFlagSupport;
1316
use OCP\LDAP\ILDAPProvider;
17+
use OCP\UserInterface;
1418
use Psr\Log\LoggerInterface;
1519

1620
/**
1721
* LDAP provider for public access to the LDAP backend.
1822
*/
1923
class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport {
20-
private $userBackend;
21-
private $groupBackend;
22-
private $logger;
24+
private IUserLDAP&UserInterface $userBackend;
25+
private IGroupLDAP&GroupInterface $groupBackend;
2326

2427
/**
25-
* Create new LDAPProvider
26-
* @param IServerContainer $serverContainer
27-
* @param Helper $helper
28-
* @param DeletedUsersIndex $deletedUsersIndex
2928
* @throws \Exception if user_ldap app was not enabled
3029
*/
3130
public function __construct(
32-
IServerContainer $serverContainer,
31+
IUserManager $userManager,
32+
IGroupManager $groupManager,
3333
private Helper $helper,
3434
private DeletedUsersIndex $deletedUsersIndex,
35+
private LoggerInterface $logger,
3536
) {
36-
$this->logger = $serverContainer->get(LoggerInterface::class);
3737
$userBackendFound = false;
3838
$groupBackendFound = false;
39-
foreach ($serverContainer->getUserManager()->getBackends() as $backend) {
39+
foreach ($userManager->getBackends() as $backend) {
4040
$this->logger->debug('instance ' . get_class($backend) . ' user backend.', ['app' => 'user_ldap']);
4141
if ($backend instanceof IUserLDAP) {
4242
$this->userBackend = $backend;
4343
$userBackendFound = true;
4444
break;
4545
}
4646
}
47-
foreach ($serverContainer->getGroupManager()->getBackends() as $backend) {
47+
foreach ($groupManager->getBackends() as $backend) {
4848
$this->logger->debug('instance ' . get_class($backend) . ' group backend.', ['app' => 'user_ldap']);
4949
if ($backend instanceof IGroupLDAP) {
5050
$this->groupBackend = $backend;

apps/user_ldap/lib/LDAPProviderFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
68
* SPDX-License-Identifier: AGPL-3.0-or-later
79
*/
10+
811
namespace OCA\User_LDAP;
912

10-
use OCP\IServerContainer;
1113
use OCP\LDAP\ILDAPProvider;
1214
use OCP\LDAP\ILDAPProviderFactory;
15+
use Psr\Container\ContainerInterface;
1316

1417
class LDAPProviderFactory implements ILDAPProviderFactory {
1518
public function __construct(
16-
/** * @var IServerContainer */
17-
private IServerContainer $serverContainer,
19+
private ContainerInterface $serverContainer,
1820
) {
1921
}
2022

apps/user_ldap/tests/AccessTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\Image;
3030
use OCP\IUserManager;
3131
use OCP\Notification\IManager as INotificationManager;
32+
use OCP\Profiler\IProfiler;
3233
use OCP\Server;
3334
use OCP\Share\IManager;
3435
use PHPUnit\Framework\MockObject\MockObject;
@@ -242,7 +243,11 @@ public function testStringResemblesDN(string $input, array|bool $interResult, bo
242243
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dnInputDataProvider')]
243244
public function testStringResemblesDNLDAPmod(string $input, array|bool $interResult, bool $expectedResult): void {
244245
[, $con, $um, $helper] = $this->getConnectorAndLdapMock();
245-
$lw = new LDAP();
246+
$lw = new LDAP(
247+
$this->createMock(IProfiler::class),
248+
$this->createMock(IConfig::class),
249+
$this->createMock(LoggerInterface::class),
250+
);
246251
$access = new Access($lw, $con, $um, $helper, $this->ncUserManager, $this->logger, $this->appConfig, $this->dispatcher);
247252

248253
if (!function_exists('ldap_explode_dn')) {

apps/user_ldap/tests/Jobs/SyncTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ protected function setUp(): void {
7676
$this->connectionFactory,
7777
$this->accessFactory,
7878
);
79-
80-
$this->sync->overwritePropertiesForTest($this->ldapWrapper);
8179
}
8280

8381
public static function intervalDataProvider(): array {

0 commit comments

Comments
 (0)