Skip to content

Commit 0d6e9a4

Browse files
authored
Merge pull request #16011 from nextcloud/fix/noid/ldapprovider-return-one-base
(LDAP) API: return one base properly when multiple are configured
2 parents b25838e + 0b34085 commit 0d6e9a4

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

apps/user_ldap/lib/ILDAPUserPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function respondToActions();
4040
*
4141
* @param string $uid The UID of the user to create
4242
* @param string $password The password of the new user
43-
* @return bool
43+
* @return bool|string
4444
*/
4545
public function createUser($uid, $password);
4646

apps/user_ldap/lib/LDAPProvider.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,25 @@ public function getGroupLDAPConnection($gid) {
182182
public function getLDAPBaseUsers($uid) {
183183
if(!$this->userBackend->userExists($uid)){
184184
throw new \Exception('User id not found in LDAP');
185-
}
186-
return $this->userBackend->getLDAPAccess($uid)->getConnection()->getConfiguration()['ldap_base_users'];
185+
}
186+
$access = $this->userBackend->getLDAPAccess($uid);
187+
$bases = $access->getConnection()->ldapBaseUsers;
188+
$dn = $this->getUserDN($uid);
189+
foreach ($bases as $base) {
190+
if($access->isDNPartOfBase($dn, [$base])) {
191+
return $base;
192+
}
193+
}
194+
// should not occur, because the user does not qualify to use NC in this case
195+
$this->logger->info(
196+
'No matching user base found for user {dn}, available: {bases}.',
197+
[
198+
'app' => 'user_ldap',
199+
'dn' => $dn,
200+
'bases' => $bases,
201+
]
202+
);
203+
return array_shift($bases);
187204
}
188205

189206
/**
@@ -196,7 +213,8 @@ public function getLDAPBaseGroups($uid) {
196213
if(!$this->userBackend->userExists($uid)){
197214
throw new \Exception('User id not found in LDAP');
198215
}
199-
return $this->userBackend->getLDAPAccess($uid)->getConnection()->getConfiguration()['ldap_base_groups'];
216+
$bases = $this->userBackend->getLDAPAccess($uid)->getConnection()->ldapBaseGroups;
217+
return array_shift($bases);
200218
}
201219

202220
/**

apps/user_ldap/tests/LDAPProviderTest.php

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
namespace OCA\User_LDAP\Tests;
2727

2828
use OC\User\Manager;
29+
use OCA\User_LDAP\Access;
30+
use OCA\User_LDAP\Connection;
2931
use OCA\User_LDAP\IGroupLDAP;
3032
use OCP\IConfig;
3133
use OCP\IServerContainer;
@@ -337,24 +339,49 @@ public function testGetLDAPBaseUsersUserIDNotFound() {
337339
}
338340

339341
public function testGetLDAPBaseUsers() {
342+
$bases = [
343+
'ou=users,ou=foobar,dc=example,dc=org',
344+
'ou=users,ou=barfoo,dc=example,dc=org',
345+
];
346+
$dn = 'uid=malik,' . $bases[1];
347+
348+
$connection = $this->createMock(Connection::class);
349+
$connection->expects($this->any())
350+
->method('__get')
351+
->willReturnCallback(function ($key) use ($bases) {
352+
switch($key) {
353+
case 'ldapBaseUsers':
354+
return $bases;
355+
}
356+
return null;
357+
});
358+
359+
$access = $this->createMock(Access::class);
360+
$access->expects($this->any())
361+
->method('getConnection')
362+
->willReturn($connection);
363+
$access->expects($this->exactly(2))
364+
->method('isDNPartOfBase')
365+
->willReturnOnConsecutiveCalls(false, true);
366+
$access->expects($this->atLeastOnce())
367+
->method('username2dn')
368+
->willReturn($dn);
369+
340370
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
341371
->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
342372
->disableOriginalConstructor()
343373
->getMock();
344-
$userBackend->expects($this->at(0))
374+
$userBackend->expects($this->atLeastOnce())
345375
->method('userExists')
346376
->willReturn(true);
347-
$userBackend->expects($this->at(3))
348-
->method('getConfiguration')
349-
->willReturn(array('ldap_base_users'=>'ou=users,dc=example,dc=org'));
350377
$userBackend->expects($this->any())
351-
->method($this->anything())
352-
->willReturnSelf();
353-
378+
->method('getLDAPAccess')
379+
->willReturn($access);
380+
354381
$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
355382

356383
$ldapProvider = $this->getLDAPProvider($server);
357-
$this->assertEquals('ou=users,dc=example,dc=org', $ldapProvider->getLDAPBaseUsers('existing_user'));
384+
$this->assertEquals($bases[1], $ldapProvider->getLDAPBaseUsers('existing_user'));
358385
}
359386

360387
/**
@@ -375,24 +402,42 @@ public function testGetLDAPBaseGroupsUserIDNotFound() {
375402
}
376403

377404
public function testGetLDAPBaseGroups() {
405+
$bases = [
406+
'ou=groupd,ou=foobar,dc=example,dc=org',
407+
'ou=groups,ou=barfoo,dc=example,dc=org',
408+
];
409+
410+
$connection = $this->createMock(Connection::class);
411+
$connection->expects($this->any())
412+
->method('__get')
413+
->willReturnCallback(function ($key) use ($bases) {
414+
switch($key) {
415+
case 'ldapBaseGroups':
416+
return $bases;
417+
}
418+
return null;
419+
});
420+
421+
$access = $this->createMock(Access::class);
422+
$access->expects($this->any())
423+
->method('getConnection')
424+
->willReturn($connection);
425+
378426
$userBackend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP')
379427
->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration'])
380428
->disableOriginalConstructor()
381429
->getMock();
382-
$userBackend->expects($this->at(0))
430+
$userBackend->expects($this->any())
383431
->method('userExists')
384432
->willReturn(true);
385-
$userBackend->expects($this->at(3))
386-
->method('getConfiguration')
387-
->willReturn(array('ldap_base_groups'=>'ou=groups,dc=example,dc=org'));
388433
$userBackend->expects($this->any())
389-
->method($this->anything())
390-
->willReturnSelf();
391-
434+
->method('getLDAPAccess')
435+
->willReturn($access);
436+
392437
$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
393438

394439
$ldapProvider = $this->getLDAPProvider($server);
395-
$this->assertEquals('ou=groups,dc=example,dc=org', $ldapProvider->getLDAPBaseGroups('existing_user'));
440+
$this->assertEquals($bases[0], $ldapProvider->getLDAPBaseGroups('existing_user'));
396441
}
397442

398443
/**

0 commit comments

Comments
 (0)