]> source.dussan.org Git - nextcloud-server.git/commitdiff
do not flip available state to unavailable, allow empty results
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Tue, 11 Aug 2020 16:53:50 +0000 (18:53 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Tue, 11 Aug 2020 23:19:46 +0000 (01:19 +0200)
- 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>
apps/user_ldap/lib/Connection.php
apps/user_ldap/lib/Group_LDAP.php
apps/user_ldap/tests/Group_LDAPTest.php

index 8d499a4ee1cc53e978c37d7db6ff3201adb69048..47d7a4fdbb7affcb4a4f335dcf274dbe3ee0bfe2 100644 (file)
@@ -66,6 +66,7 @@ use OCP\ILogger;
  * @property string[] ldapBaseGroups
  * @property string ldapGroupFilter
  * @property string ldapGroupDisplayName
+ * @property string ldapMatchingRuleInChainState
  */
 class Connection extends LDAPUtility {
        private $ldapConnectionRes = null;
index 46a0467d15dc0ad89be097779ac93189349f3b8b..e9382206f7279c2fa0308847d0c1e336a2ae0e84 100644 (file)
@@ -240,21 +240,27 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                ) {
                        $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;
@@ -269,7 +275,10 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                $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();
                }
index 3e681233186506aeaba7aea690e7a1dd7e2c295d..f71b2472886a0be068612ec6960a350a846dc508 100644 (file)
@@ -127,6 +127,10 @@ class Group_LDAPTest extends TestCase {
                        ->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');
 
@@ -167,6 +171,10 @@ class Group_LDAPTest extends TestCase {
                                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');
 
@@ -535,7 +543,10 @@ class Group_LDAPTest extends TestCase {
                $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');
@@ -570,7 +581,10 @@ class Group_LDAPTest extends TestCase {
                $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');
@@ -609,6 +623,10 @@ class Group_LDAPTest extends TestCase {
                        ->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');