]> source.dussan.org Git - nextcloud-server.git/commitdiff
dav search to honour sharing.maxAutocompleteResults setting
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Fri, 11 Dec 2020 13:04:40 +0000 (14:04 +0100)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Tue, 15 Dec 2020 10:53:39 +0000 (11:53 +0100)
- it is being used on frontend by users
- user and big instances benefit from quicker results and less load

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
apps/dav/lib/Connector/Sabre/Principal.php
apps/dav/lib/DAV/GroupPrincipalBackend.php
apps/dav/lib/RootCollection.php
apps/dav/tests/unit/DAV/GroupPrincipalTest.php

index 5dedb0a7d7b94d9db40fe08086a83c587ddf0f19..12e1d18faa268ce5d41a7d84f3575627c6c8576c 100644 (file)
@@ -270,6 +270,7 @@ class Principal implements BackendInterface {
                        }
                }
 
+               $searchLimit = $this->config->getSystemValue('sharing.maxAutocompleteResults', null);
                foreach ($searchProperties as $prop => $value) {
                        switch ($prop) {
                                case '{http://sabredav.org/ns}email-address':
@@ -305,7 +306,7 @@ class Principal implements BackendInterface {
                                        break;
 
                                case '{DAV:}displayname':
-                                       $users = $this->userManager->searchDisplayName($value);
+                                       $users = $this->userManager->searchDisplayName($value, $searchLimit);
 
                                        if (!$allowEnumeration) {
                                                $users = \array_filter($users, static function (IUser $user) use ($value) {
index 389443174241f08a96b0b9e4dce1c146b9fe3000..747976b6ad393474df9ba89db3bf353815c97745 100644 (file)
@@ -27,6 +27,7 @@
 
 namespace OCA\DAV\DAV;
 
+use OCP\IConfig;
 use OCP\IGroup;
 use OCP\IGroupManager;
 use OCP\IUser;
@@ -47,18 +48,24 @@ class GroupPrincipalBackend implements BackendInterface {
 
        /** @var IShareManager */
        private $shareManager;
+       /** @var IConfig */
+       private $config;
 
        /**
         * @param IGroupManager $IGroupManager
         * @param IUserSession $userSession
         * @param IShareManager $shareManager
         */
-       public function __construct(IGroupManager $IGroupManager,
-                                                               IUserSession $userSession,
-                                                               IShareManager $shareManager) {
+       public function __construct(
+               IGroupManager $IGroupManager,
+               IUserSession $userSession,
+               IShareManager $shareManager,
+               IConfig $config
+       ) {
                $this->groupManager = $IGroupManager;
                $this->userSession = $userSession;
                $this->shareManager = $shareManager;
+               $this->config = $config;
        }
 
        /**
@@ -205,10 +212,11 @@ class GroupPrincipalBackend implements BackendInterface {
                        $restrictGroups = $this->groupManager->getUserGroupIds($user);
                }
 
+               $searchLimit = $this->config->getSystemValue('sharing.maxAutocompleteResults', null);
                foreach ($searchProperties as $prop => $value) {
                        switch ($prop) {
                                case '{DAV:}displayname':
-                                       $groups = $this->groupManager->search($value);
+                                       $groups = $this->groupManager->search($value, $searchLimit);
 
                                        $results[] = array_reduce($groups, function (array $carry, IGroup $group) use ($restrictGroups) {
                                                $gid = $group->getGID();
index 83f3691959ba78b319ce5c382c12e7aa9468e8f0..b08775d80f6068b62ec7437c950b6a1752caf53c 100644 (file)
@@ -72,7 +72,7 @@ class RootCollection extends SimpleCollection {
                        $proxyMapper,
                        \OC::$server->getConfig()
                );
-               $groupPrincipalBackend = new GroupPrincipalBackend($groupManager, $userSession, $shareManager);
+               $groupPrincipalBackend = new GroupPrincipalBackend($groupManager, $userSession, $shareManager, $config);
                $calendarResourcePrincipalBackend = new ResourcePrincipalBackend($db, $userSession, $groupManager, $logger, $proxyMapper);
                $calendarRoomPrincipalBackend = new RoomPrincipalBackend($db, $userSession, $groupManager, $logger, $proxyMapper);
                // as soon as debug mode is enabled we allow listing of principals
index aaa45e5d7154506a4e42ce4c84910808e823343f..51b73b3b0a744d9c4114ba3605833997788e6dfb 100644 (file)
@@ -31,6 +31,7 @@ namespace OCA\DAV\Tests\unit\DAV;
 
 use OC\Group\Group;
 use OCA\DAV\DAV\GroupPrincipalBackend;
+use OCP\IConfig;
 use OCP\IGroup;
 use OCP\IGroupManager;
 use OCP\IUser;
@@ -39,6 +40,8 @@ use OCP\Share\IManager;
 use Sabre\DAV\PropPatch;
 
 class GroupPrincipalTest extends \Test\TestCase {
+       /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
+       private $config;
 
        /** @var IGroupManager | \PHPUnit\Framework\MockObject\MockObject */
        private $groupManager;
@@ -56,11 +59,14 @@ class GroupPrincipalTest extends \Test\TestCase {
                $this->groupManager = $this->createMock(IGroupManager::class);
                $this->userSession = $this->createMock(IUserSession::class);
                $this->shareManager = $this->createMock(IManager::class);
+               $this->config = $this->createMock(IConfig::class);
 
                $this->connector = new GroupPrincipalBackend(
                        $this->groupManager,
                        $this->userSession,
-                       $this->shareManager);
+                       $this->shareManager,
+                       $this->config
+               );
                parent::setUp();
        }
 
@@ -167,6 +173,23 @@ class GroupPrincipalTest extends \Test\TestCase {
                $this->assertSame($expectedResponse, $response);
        }
 
+       public function testGetPrincipalsByPathGroupWithHash() {
+               $group1 = $this->mockGroup('foo#bar');
+               $this->groupManager
+                       ->expects($this->once())
+                       ->method('get')
+                       ->with('foo#bar')
+                       ->willReturn($group1);
+
+               $expectedResponse = [
+                       'uri' => 'principals/groups/foo%23bar',
+                       '{DAV:}displayname' => 'Group foo#bar',
+                       '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
+               ];
+               $response = $this->connector->getPrincipalByPath('principals/groups/foo#bar');
+               $this->assertSame($expectedResponse, $response);
+       }
+
        public function testGetGroupMemberSet() {
                $response = $this->connector->getGroupMemberSet('principals/groups/foo');
                $this->assertSame([], $response);