From c6f56ddbd7da7bf179eb98b4e3221e2f5e5772f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 15 Dec 2025 17:56:32 +0100 Subject: [PATCH 1/6] fix(user_ldap): Remove usages of deprecated IServerContainer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/AppInfo/Application.php | 33 ++++------------------ apps/user_ldap/lib/LDAPProvider.php | 24 ++++++++-------- apps/user_ldap/lib/LDAPProviderFactory.php | 8 ++++-- 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/apps/user_ldap/lib/AppInfo/Application.php b/apps/user_ldap/lib/AppInfo/Application.php index 7646269828095..7976668287248 100644 --- a/apps/user_ldap/lib/AppInfo/Application.php +++ b/apps/user_ldap/lib/AppInfo/Application.php @@ -1,5 +1,7 @@ getContainer(); - - /** - * Controller - */ - $container->registerService('RenewPasswordController', function (ContainerInterface $appContainer) { - return new RenewPasswordController( - $appContainer->get('AppName'), - $appContainer->get(IRequest::class), - $appContainer->get(IUserManager::class), - $appContainer->get(IConfig::class), - $appContainer->get(IUserConfig::class), - $appContainer->get(IL10N::class), - $appContainer->get('Session'), - $appContainer->get(IURLGenerator::class), - $appContainer->get(IInitialState::class), - ); - }); + } - $container->registerService(ILDAPWrapper::class, function (ContainerInterface $appContainer) { + public function register(IRegistrationContext $context): void { + $context->registerService(ILDAPWrapper::class, function (ContainerInterface $c) { return new LDAP( - $appContainer->get(IConfig::class)->getSystemValueString('ldap_log_file') + $c->get(IConfig::class)->getSystemValueString('ldap_log_file') ); }); - } - public function register(IRegistrationContext $context): void { $context->registerNotifierService(Notifier::class); $context->registerService( diff --git a/apps/user_ldap/lib/LDAPProvider.php b/apps/user_ldap/lib/LDAPProvider.php index fad0da7f206d1..a15031b0e3c17 100644 --- a/apps/user_ldap/lib/LDAPProvider.php +++ b/apps/user_ldap/lib/LDAPProvider.php @@ -5,38 +5,38 @@ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ + namespace OCA\User_LDAP; use OCA\User_LDAP\User\DeletedUsersIndex; -use OCP\IServerContainer; +use OCP\GroupInterface; +use OCP\IGroupManager; +use OCP\IUserManager; use OCP\LDAP\IDeletionFlagSupport; use OCP\LDAP\ILDAPProvider; +use OCP\UserInterface; use Psr\Log\LoggerInterface; /** * LDAP provider for public access to the LDAP backend. */ class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport { - private $userBackend; - private $groupBackend; - private $logger; + private IUserLDAP&UserInterface $userBackend; + private IGroupLDAP&GroupInterface $groupBackend; /** - * Create new LDAPProvider - * @param IServerContainer $serverContainer - * @param Helper $helper - * @param DeletedUsersIndex $deletedUsersIndex * @throws \Exception if user_ldap app was not enabled */ public function __construct( - IServerContainer $serverContainer, + IUserManager $userManager, + IGroupManager $groupManager, private Helper $helper, private DeletedUsersIndex $deletedUsersIndex, + private LoggerInterface $logger, ) { - $this->logger = $serverContainer->get(LoggerInterface::class); $userBackendFound = false; $groupBackendFound = false; - foreach ($serverContainer->getUserManager()->getBackends() as $backend) { + foreach ($userManager->getBackends() as $backend) { $this->logger->debug('instance ' . get_class($backend) . ' user backend.', ['app' => 'user_ldap']); if ($backend instanceof IUserLDAP) { $this->userBackend = $backend; @@ -44,7 +44,7 @@ public function __construct( break; } } - foreach ($serverContainer->getGroupManager()->getBackends() as $backend) { + foreach ($groupManager->getBackends() as $backend) { $this->logger->debug('instance ' . get_class($backend) . ' group backend.', ['app' => 'user_ldap']); if ($backend instanceof IGroupLDAP) { $this->groupBackend = $backend; diff --git a/apps/user_ldap/lib/LDAPProviderFactory.php b/apps/user_ldap/lib/LDAPProviderFactory.php index 8fad9d52206ab..b91f92f2519dc 100644 --- a/apps/user_ldap/lib/LDAPProviderFactory.php +++ b/apps/user_ldap/lib/LDAPProviderFactory.php @@ -1,20 +1,22 @@ Date: Thu, 26 Feb 2026 13:00:48 +0100 Subject: [PATCH 2/6] fix: Adapt LDAPProvider tests to code changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/tests/LDAPProviderTest.php | 159 ++++++---------------- 1 file changed, 44 insertions(+), 115 deletions(-) diff --git a/apps/user_ldap/tests/LDAPProviderTest.php b/apps/user_ldap/tests/LDAPProviderTest.php index 116654929808f..aae29df74af24 100644 --- a/apps/user_ldap/tests/LDAPProviderTest.php +++ b/apps/user_ldap/tests/LDAPProviderTest.php @@ -7,7 +7,6 @@ */ namespace OCA\User_LDAP\Tests; -use OC\Config; use OC\User\Manager; use OCA\User_LDAP\Access; use OCA\User_LDAP\Connection; @@ -16,12 +15,12 @@ use OCA\User_LDAP\IGroupLDAP; use OCA\User_LDAP\ILDAPWrapper; use OCA\User_LDAP\IUserLDAP; -use OCA\User_LDAP\LDAPProviderFactory; +use OCA\User_LDAP\LDAPProvider; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_LDAP; use OCP\EventDispatcher\IEventDispatcher; use OCP\ICacheFactory; use OCP\IConfig; -use OCP\IServerContainer; use OCP\Server; use Psr\Log\LoggerInterface; @@ -33,21 +32,6 @@ */ #[\PHPUnit\Framework\Attributes\Group(name: 'DB')] class LDAPProviderTest extends \Test\TestCase { - private function getServerMock(IUserLDAP $userBackend, IGroupLDAP $groupBackend) { - $server = $this->getMockBuilder('OC\Server') - ->onlyMethods(['getUserManager', 'getGroupManager']) - ->setConstructorArgs(['', new Config(\OC::$configDir)]) - ->getMock(); - $server->expects($this->any()) - ->method('getUserManager') - ->willReturn($this->getUserManagerMock($userBackend)); - $server->expects($this->any()) - ->method('getGroupManager') - ->willReturn($this->getGroupManagerMock($groupBackend)); - - return $server; - } - private function getUserManagerMock(IUserLDAP $userBackend) { $userManager = $this->getMockBuilder(Manager::class) ->onlyMethods(['getBackends']) @@ -83,9 +67,14 @@ private function getDefaultGroupBackendMock() { return $groupBackend; } - private function getLDAPProvider(IServerContainer $serverContainer) { - $factory = new LDAPProviderFactory($serverContainer); - return $factory->getLDAPProvider(); + private function getLDAPProvider(IUserLDAP $userBackend, IGroupLDAP $groupBackend) { + return new LDAPProvider( + $this->getUserManagerMock($userBackend), + $this->getGroupManagerMock($groupBackend), + Server::get(Helper::class), + Server::get(DeletedUsersIndex::class), + $this->createMock(LoggerInterface::class), + ); } @@ -99,9 +88,7 @@ public function testGetUserDNUserIDNotFound(): void { ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->getUserDN('nonexisting_user'); } @@ -125,9 +112,7 @@ public function testGetUserDN(): void { ->method('getLDAPAccess') ->willReturn($userAccess); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org', $ldapProvider->getUserDN('existing_user')); } @@ -145,9 +130,7 @@ public function testGetGroupDNGroupIDNotFound(): void { $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->getGroupDN('nonexisting_group'); } @@ -170,9 +153,7 @@ public function testGetGroupDN(): void { ->method('getLDAPAccess') ->willReturn($groupAccess); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $this->assertEquals('cn=existing_group,ou=Are Sufficient To,ou=Test,dc=example,dc=org', $ldapProvider->getGroupDN('existing_group')); } @@ -186,9 +167,7 @@ public function testGetUserName(): void { ->method('dn2UserName') ->willReturn('existing_user'); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals('existing_user', $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } @@ -196,11 +175,9 @@ public function testGetUserName(): void { public function testDNasBaseParameter(): void { $userBackend = $this->createMock(User_LDAP::class); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - $helper = Server::get(Helper::class); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals( $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); @@ -209,11 +186,9 @@ public function testDNasBaseParameter(): void { public function testSanitizeDN(): void { $userBackend = $this->createMock(User_LDAP::class); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - $helper = Server::get(Helper::class); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals( $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); @@ -227,9 +202,7 @@ public function testGetLDAPConnectionUserIDNotFound(): void { $userBackend = $this->createMock(User_LDAP::class); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->getLDAPConnection('nonexisting_user'); } @@ -246,9 +219,7 @@ public function testGetLDAPConnection(): void { ->method('getNewLDAPConnection') ->willReturn($ldapConnection); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals($ldapConnection, $ldapProvider->getLDAPConnection('existing_user')); } @@ -265,9 +236,7 @@ public function testGetGroupLDAPConnectionGroupIDNotFound(): void { $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->getGroupLDAPConnection('nonexisting_group'); } @@ -287,9 +256,7 @@ public function testGetGroupLDAPConnection(): void { ->method('getNewLDAPConnection') ->willReturn($ldapConnection); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $this->assertEquals($ldapConnection, $ldapProvider->getGroupLDAPConnection('existing_group')); } @@ -304,9 +271,7 @@ public function testGetLDAPBaseUsersUserIDNotFound(): void { ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->getLDAPBaseUsers('nonexisting_user'); } @@ -352,9 +317,7 @@ public function testGetLDAPBaseUsers(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals($bases[1], $ldapProvider->getLDAPBaseUsers('existing_user')); } @@ -369,9 +332,7 @@ public function testGetLDAPBaseGroupsUserIDNotFound(): void { ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->getLDAPBaseGroups('nonexisting_user'); } @@ -410,9 +371,7 @@ public function testGetLDAPBaseGroups(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals($bases[0], $ldapProvider->getLDAPBaseGroups('existing_user')); } @@ -427,9 +386,7 @@ public function testClearCacheUserIDNotFound(): void { ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->clearCache('nonexisting_user'); } @@ -454,9 +411,7 @@ public function testClearCache(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->clearCache('existing_user'); $this->addToAssertionCount(1); } @@ -475,9 +430,7 @@ public function testClearGroupCacheGroupIDNotFound(): void { ->getMock(); $groupBackend->expects($this->any())->method('groupExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->clearGroupCache('nonexisting_group'); } @@ -503,9 +456,7 @@ public function testClearGroupCache(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->clearGroupCache('existing_group'); $this->addToAssertionCount(1); } @@ -519,26 +470,22 @@ public function testDnExists(): void { ->method('dn2UserName') ->willReturn('existing_user'); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); } public function testFlagRecord(): void { $userBackend = $this->createMock(User_LDAP::class); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->flagRecord('existing_user'); $this->addToAssertionCount(1); } public function testUnflagRecord(): void { $userBackend = $this->createMock(User_LDAP::class); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->unflagRecord('existing_user'); $this->addToAssertionCount(1); } @@ -554,9 +501,7 @@ public function testGetLDAPDisplayNameFieldUserIDNotFound(): void { ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->getLDAPDisplayNameField('nonexisting_user'); } @@ -581,9 +526,7 @@ public function testGetLDAPDisplayNameField(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals('displayName', $ldapProvider->getLDAPDisplayNameField('existing_user')); } @@ -598,9 +541,7 @@ public function testGetLDAPEmailFieldUserIDNotFound(): void { ->getMock(); $userBackend->expects($this->any())->method('userExists')->willReturn(false); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $ldapProvider->getLDAPEmailField('nonexisting_user'); } @@ -625,9 +566,7 @@ public function testGetLDAPEmailField(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $this->getDefaultGroupBackendMock()); $this->assertEquals('mail', $ldapProvider->getLDAPEmailField('existing_user')); } @@ -646,9 +585,7 @@ public function testGetLDAPGroupMemberAssocUserIDNotFound(): void { ->method('groupExists') ->willReturn(false); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->getLDAPGroupMemberAssoc('nonexisting_group'); } @@ -676,9 +613,7 @@ public function testgetLDAPGroupMemberAssoc(): void { ->method('getLDAPAccess') ->willReturn($access); - $server = $this->getServerMock($userBackend, $groupBackend); - - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $this->assertEquals('assoc_type', $ldapProvider->getLDAPGroupMemberAssoc('existing_group')); } @@ -692,9 +627,8 @@ public function testGetMultiValueUserAttributeUserNotFound(): void { ->with('admin') ->willReturn(false); $groupBackend = $this->createMock(Group_LDAP::class); - $server = $this->getServerMock($userBackend, $groupBackend); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias'); } @@ -719,9 +653,8 @@ public function testGetMultiValueUserAttributeCacheHit(): void { ->method('getLDAPAccess') ->willReturn($access); $groupBackend = $this->createMock(Group_LDAP::class); - $server = $this->getServerMock($userBackend, $groupBackend); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias'); } @@ -756,9 +689,8 @@ public function testGetMultiValueUserAttributeLdapError(): void { $groupBackend = $this->getMockBuilder(Group_LDAP::class) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($userBackend, $groupBackend); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $values = $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias'); self::assertCount(0, $values); @@ -795,9 +727,8 @@ public function testGetMultiValueUserAttribute(): void { $groupBackend = $this->getMockBuilder(Group_LDAP::class) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($userBackend, $groupBackend); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $values = $ldapProvider->getMultiValueUserAttribute('admin', 'mailAlias'); self::assertCount(2, $values); @@ -834,9 +765,8 @@ public function testGetUserAttributeLdapError(): void { $groupBackend = $this->getMockBuilder(Group_LDAP::class) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($userBackend, $groupBackend); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $value = $ldapProvider->getUserAttribute('admin', 'mailAlias'); self::assertNull($value); @@ -873,9 +803,8 @@ public function testGetUserAttribute(): void { $groupBackend = $this->getMockBuilder(Group_LDAP::class) ->disableOriginalConstructor() ->getMock(); - $server = $this->getServerMock($userBackend, $groupBackend); - $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider = $this->getLDAPProvider($userBackend, $groupBackend); $value = $ldapProvider->getUserAttribute('admin', 'mailAlias'); self::assertEquals('aliasA@test.local', $value); From cc365554c6e2ccfd76d6c7fb197cda4af92a3872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 26 Feb 2026 13:02:58 +0100 Subject: [PATCH 3/6] chore: update psalm baseline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- build/psalm-baseline.xml | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 9936d3543d934..d2be62c1b174a 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -2575,8 +2575,6 @@ 'loginName2UserName' )]]> - - @@ -2617,20 +2615,6 @@ - - - - - - - - - - - - - - From 60c86848e4debe9900083c8ee5eed83c014f0a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 26 Feb 2026 15:21:40 +0100 Subject: [PATCH 4/6] fix: Use proper DI for LDAP class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/AppInfo/Application.php | 6 +----- apps/user_ldap/lib/LDAP.php | 14 ++++++-------- apps/user_ldap/tests/LDAPTest.php | 8 ++++++++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/apps/user_ldap/lib/AppInfo/Application.php b/apps/user_ldap/lib/AppInfo/Application.php index 7976668287248..220a11715faea 100644 --- a/apps/user_ldap/lib/AppInfo/Application.php +++ b/apps/user_ldap/lib/AppInfo/Application.php @@ -53,11 +53,7 @@ public function __construct() { } public function register(IRegistrationContext $context): void { - $context->registerService(ILDAPWrapper::class, function (ContainerInterface $c) { - return new LDAP( - $c->get(IConfig::class)->getSystemValueString('ldap_log_file') - ); - }); + $context->registerServiceAlias(ILDAPWrapper::class, LDAP::class); $context->registerNotifierService(Notifier::class); diff --git a/apps/user_ldap/lib/LDAP.php b/apps/user_ldap/lib/LDAP.php index 1cf20c4b93902..21b5b519157b5 100644 --- a/apps/user_ldap/lib/LDAP.php +++ b/apps/user_ldap/lib/LDAP.php @@ -18,23 +18,21 @@ class LDAP implements ILDAPWrapper { protected array $curArgs = []; - protected LoggerInterface $logger; - protected IConfig $config; private ?LdapDataCollector $dataCollector = null; + protected string $logFile = ''; + public function __construct( - protected string $logFile = '', + IProfiler $profiler, + protected IConfig $config, + protected LoggerInterface $logger, ) { - /** @var IProfiler $profiler */ - $profiler = Server::get(IProfiler::class); if ($profiler->isEnabled()) { $this->dataCollector = new LdapDataCollector(); $profiler->add($this->dataCollector); } - - $this->logger = Server::get(LoggerInterface::class); - $this->config = Server::get(IConfig::class); + $this->logFile = $this->config->getSystemValueString('ldap_log_file'); } /** diff --git a/apps/user_ldap/tests/LDAPTest.php b/apps/user_ldap/tests/LDAPTest.php index 70a36cd4714fb..a453a174355dd 100644 --- a/apps/user_ldap/tests/LDAPTest.php +++ b/apps/user_ldap/tests/LDAPTest.php @@ -8,7 +8,10 @@ namespace OCA\User_LDAP\Tests; use OCA\User_LDAP\LDAP; +use OCP\IConfig; +use OCP\Profiler\IProfiler; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class LDAPTest extends TestCase { @@ -18,6 +21,11 @@ protected function setUp(): void { parent::setUp(); $this->ldap = $this->getMockBuilder(LDAP::class) ->onlyMethods(['invokeLDAPMethod']) + ->setConstructorArgs([ + $this->createMock(IProfiler::class), + $this->createMock(IConfig::class), + $this->createMock(LoggerInterface::class), + ]) ->getMock(); } From d8084559a9b04b68e21bd860387243e1aeb7d982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 26 Feb 2026 15:22:09 +0100 Subject: [PATCH 5/6] chore: Add missing return type in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/tests/LDAPProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/user_ldap/tests/LDAPProviderTest.php b/apps/user_ldap/tests/LDAPProviderTest.php index aae29df74af24..3055ff40dceda 100644 --- a/apps/user_ldap/tests/LDAPProviderTest.php +++ b/apps/user_ldap/tests/LDAPProviderTest.php @@ -67,7 +67,7 @@ private function getDefaultGroupBackendMock() { return $groupBackend; } - private function getLDAPProvider(IUserLDAP $userBackend, IGroupLDAP $groupBackend) { + private function getLDAPProvider(IUserLDAP $userBackend, IGroupLDAP $groupBackend): LDAPProvider { return new LDAPProvider( $this->getUserManagerMock($userBackend), $this->getGroupManagerMock($groupBackend), From 279d5c226ee09a0f39dfb298a01d21f2ff20c7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 2 Mar 2026 14:58:48 +0100 Subject: [PATCH 6/6] chore(user_ldap): Adapt code to LDAP constructor change, remove dead code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/Command/Search.php | 7 ------- apps/user_ldap/lib/Command/SetConfig.php | 15 +++++++++------ apps/user_ldap/lib/Jobs/Sync.php | 10 ---------- apps/user_ldap/tests/AccessTest.php | 7 ++++++- apps/user_ldap/tests/Jobs/SyncTest.php | 2 -- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/apps/user_ldap/lib/Command/Search.php b/apps/user_ldap/lib/Command/Search.php index 85906b20e9a7f..a2a8effb77039 100644 --- a/apps/user_ldap/lib/Command/Search.php +++ b/apps/user_ldap/lib/Command/Search.php @@ -8,11 +8,8 @@ namespace OCA\User_LDAP\Command; use OCA\User_LDAP\Group_Proxy; -use OCA\User_LDAP\Helper; -use OCA\User_LDAP\LDAP; use OCA\User_LDAP\User_Proxy; use OCP\IConfig; -use OCP\Server; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -82,10 +79,6 @@ protected function validateOffsetAndLimit(int $offset, int $limit): void { } protected function execute(InputInterface $input, OutputInterface $output): int { - $helper = Server::get(Helper::class); - $configPrefixes = $helper->getServerConfigurationPrefixes(true); - $ldapWrapper = new LDAP(); - $offset = (int)$input->getOption('offset'); $limit = (int)$input->getOption('limit'); $this->validateOffsetAndLimit($offset, $limit); diff --git a/apps/user_ldap/lib/Command/SetConfig.php b/apps/user_ldap/lib/Command/SetConfig.php index 7e9efcf34d031..f4aa2041b39b2 100644 --- a/apps/user_ldap/lib/Command/SetConfig.php +++ b/apps/user_ldap/lib/Command/SetConfig.php @@ -10,14 +10,19 @@ use OCA\User_LDAP\Configuration; use OCA\User_LDAP\ConnectionFactory; use OCA\User_LDAP\Helper; -use OCA\User_LDAP\LDAP; -use OCP\Server; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class SetConfig extends Command { + public function __construct( + private readonly Helper $helper, + private readonly ConnectionFactory $connectionFactory, + ) { + parent::__construct(); + } + protected function configure(): void { $this ->setName('ldap:set-config') @@ -41,8 +46,7 @@ protected function configure(): void { } protected function execute(InputInterface $input, OutputInterface $output): int { - $helper = Server::get(Helper::class); - $availableConfigs = $helper->getServerConfigurationPrefixes(); + $availableConfigs = $this->helper->getServerConfigurationPrefixes(); $configID = $input->getArgument('configID'); if (!in_array($configID, $availableConfigs)) { $output->writeln('Invalid configID'); @@ -65,7 +69,6 @@ protected function setValue(string $configID, string $key, string $value): void $configHolder->$key = $value; $configHolder->saveConfiguration(); - $connectionFactory = new ConnectionFactory(new LDAP()); - $connectionFactory->get($configID)->clearCache(); + $this->connectionFactory->get($configID)->clearCache(); } } diff --git a/apps/user_ldap/lib/Jobs/Sync.php b/apps/user_ldap/lib/Jobs/Sync.php index e49a2f0f59d6d..36738de20e7a7 100644 --- a/apps/user_ldap/lib/Jobs/Sync.php +++ b/apps/user_ldap/lib/Jobs/Sync.php @@ -23,8 +23,6 @@ class Sync extends TimedJob { public const MAX_INTERVAL = 12 * 60 * 60; // 12h public const MIN_INTERVAL = 30 * 60; // 30min - protected LDAP $ldap; - public function __construct( ITimeFactory $timeFactory, private IConfig $config, @@ -42,7 +40,6 @@ public function __construct( self::MIN_INTERVAL ) ); - $this->ldap = new LDAP($this->config->getSystemValueString('ldap_log_file')); } /** @@ -253,11 +250,4 @@ protected function getNextPrefix(?string $lastPrefix): ?string { } return $prefixes[$i]; } - - /** - * Only used in tests - */ - public function overwritePropertiesForTest(LDAP $ldapWrapper): void { - $this->ldap = $ldapWrapper; - } } diff --git a/apps/user_ldap/tests/AccessTest.php b/apps/user_ldap/tests/AccessTest.php index fc63e5cb73c50..16feadf81496e 100644 --- a/apps/user_ldap/tests/AccessTest.php +++ b/apps/user_ldap/tests/AccessTest.php @@ -29,6 +29,7 @@ use OCP\Image; use OCP\IUserManager; use OCP\Notification\IManager as INotificationManager; +use OCP\Profiler\IProfiler; use OCP\Server; use OCP\Share\IManager; use PHPUnit\Framework\MockObject\MockObject; @@ -242,7 +243,11 @@ public function testStringResemblesDN(string $input, array|bool $interResult, bo #[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dnInputDataProvider')] public function testStringResemblesDNLDAPmod(string $input, array|bool $interResult, bool $expectedResult): void { [, $con, $um, $helper] = $this->getConnectorAndLdapMock(); - $lw = new LDAP(); + $lw = new LDAP( + $this->createMock(IProfiler::class), + $this->createMock(IConfig::class), + $this->createMock(LoggerInterface::class), + ); $access = new Access($lw, $con, $um, $helper, $this->ncUserManager, $this->logger, $this->appConfig, $this->dispatcher); if (!function_exists('ldap_explode_dn')) { diff --git a/apps/user_ldap/tests/Jobs/SyncTest.php b/apps/user_ldap/tests/Jobs/SyncTest.php index f58a891d02f9c..3ff34172cfa54 100644 --- a/apps/user_ldap/tests/Jobs/SyncTest.php +++ b/apps/user_ldap/tests/Jobs/SyncTest.php @@ -76,8 +76,6 @@ protected function setUp(): void { $this->connectionFactory, $this->accessFactory, ); - - $this->sync->overwritePropertiesForTest($this->ldapWrapper); } public static function intervalDataProvider(): array {