Skip to content

Commit

Permalink
do not flip available state to unavailable, allow empty results
Browse files Browse the repository at this point in the history
- the detection relies that the first, requested result is not empty
- it might be empty though – groups without members
- protect switching from available to unavailable
  - switching the other way around was also not envisaged either

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Aug 11, 2020
1 parent 41eb1c0 commit eb1277e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/user_ldap/lib/Connection.php
Expand Up @@ -66,6 +66,7 @@
* @property string[] ldapBaseGroups
* @property string ldapGroupFilter
* @property string ldapGroupDisplayName
* @property string ldapMatchingRuleInChainState
*/
class Connection extends LDAPUtility {
private $ldapConnectionRes = null;
Expand Down
31 changes: 20 additions & 11 deletions apps/user_ldap/lib/Group_LDAP.php
Expand Up @@ -240,21 +240,27 @@ private function _groupMembers($dnGroup, &$seen = null) {
) {
$attemptedLdapMatchingRuleInChain = true;
// compatibility hack with servers supporting :1.2.840.113556.1.4.1941:, and others)
$filter = $this->access->combineFilterWithAnd([$this->access->connection->ldapUserFilter, 'memberof:1.2.840.113556.1.4.1941:=' . $dnGroup]);
$filter = $this->access->combineFilterWithAnd([
$this->access->connection->ldapUserFilter,
$this->access->connection->ldapUserDisplayName . '=*',
'memberof:1.2.840.113556.1.4.1941:=' . $dnGroup
]);
$memberRecords = $this->access->fetchListOfUsers(
$filter,
$this->access->userManager->getAttributes(true)
);
if (!empty($memberRecords)) {
if ($this->access->connection->ldapMatchingRuleInChainState === Configuration::LDAP_SERVER_FEATURE_UNKNOWN) {
$this->access->connection->ldapMatchingRuleInChainState = Configuration::LDAP_SERVER_FEATURE_AVAILABLE;
$this->access->connection->saveConfiguration();
}
return array_reduce($memberRecords, function ($carry, $record) {
$carry[] = $record['dn'][0];
return $carry;
}, []);
$result = array_reduce($memberRecords, function ($carry, $record) {
$carry[] = $record['dn'][0];
return $carry;
}, []);
if ($this->access->connection->ldapMatchingRuleInChainState === Configuration::LDAP_SERVER_FEATURE_AVAILABLE) {
return $result;
} elseif (!empty($memberRecords)) {
$this->access->connection->ldapMatchingRuleInChainState = Configuration::LDAP_SERVER_FEATURE_AVAILABLE;
$this->access->connection->saveConfiguration();
return $result;
}
// when feature availability is unknown, and the result is empty, continue and test with original approach
}

$seen[$dnGroup] = 1;
Expand All @@ -269,7 +275,10 @@ private function _groupMembers($dnGroup, &$seen = null) {
$allMembers += $this->getDynamicGroupMembers($dnGroup);

$this->access->connection->writeToCache($cacheKey, $allMembers);
if (isset($attemptedLdapMatchingRuleInChain) && !empty($allMembers)) {
if (isset($attemptedLdapMatchingRuleInChain)
&& $this->access->connection->ldapMatchingRuleInChainState === Configuration::LDAP_SERVER_FEATURE_UNKNOWN
&& !empty($allMembers)
) {
$this->access->connection->ldapMatchingRuleInChainState = Configuration::LDAP_SERVER_FEATURE_UNAVAILABLE;
$this->access->connection->saveConfiguration();
}
Expand Down
22 changes: 20 additions & 2 deletions apps/user_ldap/tests/Group_LDAPTest.php
Expand Up @@ -127,6 +127,10 @@ public function testCountEmptySearchString() {
->method('countUsers')
->will($this->returnValue(2));

$access->userManager->expects($this->any())
->method('getAttributes')
->willReturn(['displayName', 'mail']);

$groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->countUsersInGroup('group');

Expand Down Expand Up @@ -167,6 +171,10 @@ public function testCountWithSearchString() {
return 'foobar' . \OC::$server->getSecureRandom()->generate(7);
}));

$access->userManager->expects($this->any())
->method('getAttributes')
->willReturn(['displayName', 'mail']);

$groupBackend = new GroupLDAP($access,$pluginManager);
$users = $groupBackend->countUsersInGroup('group', '3');

Expand Down Expand Up @@ -535,7 +543,10 @@ public function testUsersInGroupPrimaryMembersOnly() {
$access->expects($this->exactly(2))
->method('nextcloudUserNames')
->willReturnOnConsecutiveCalls(['lisa', 'bart', 'kira', 'brad'], ['walle', 'dino', 'xenia']);
$access->userManager = $this->createMock(Manager::class);

$access->userManager->expects($this->any())
->method('getAttributes')
->willReturn(['displayName', 'mail']);

$groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->usersInGroup('foobar');
Expand Down Expand Up @@ -570,7 +581,10 @@ public function testUsersInGroupPrimaryAndUnixMembers() {
$access->expects($this->once())
->method('nextcloudUserNames')
->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad')));
$access->userManager = $this->createMock(Manager::class);

$access->userManager->expects($this->any())
->method('getAttributes')
->willReturn(['displayName', 'mail']);

$groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->usersInGroup('foobar');
Expand Down Expand Up @@ -609,6 +623,10 @@ public function testCountUsersInGroupPrimaryMembersOnly() {
->method('countUsers')
->will($this->returnValue(4));

$access->userManager->expects($this->any())
->method('getAttributes')
->willReturn(['displayName', 'mail']);

$groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->countUsersInGroup('foobar');

Expand Down

0 comments on commit eb1277e

Please sign in to comment.