From 9bb13fb05fffe9af94f1e57866a025382b71f5a4 Mon Sep 17 00:00:00 2001 From: Vinicius Cubas Brand Date: Fri, 29 Sep 2017 18:43:04 -0300 Subject: [PATCH 1/3] Created infrastructure to show circles' shared files There is a proposal to allow users to filter files shared to circles. This commit is needed to provide the infrastucture for it. Issue: https://github.com/nextcloud/circles/issues/137 Changes to comply to https://github.com/coletivoEITA/circles/pull/2 Polishing: get files shared to circles in caldav Signed-off-by: Vinicius Cubas Brand Signed-off-by: Maxence Lange Signed-off-by: Morris Jobke --- .../lib/Connector/Sabre/FilesReportPlugin.php | 28 +++++++++++++++++++ apps/dav/lib/Connector/Sabre/Principal.php | 15 ++++++---- .../unit/CalDAV/PublicCalendarRootTest.php | 9 ++++++ core/js/files/client.js | 8 ++++-- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php index 3e85615638..f4e5de0ea4 100644 --- a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php @@ -46,6 +46,7 @@ class FilesReportPlugin extends ServerPlugin { const NS_OWNCLOUD = 'http://owncloud.org/ns'; const REPORT_NAME = '{http://owncloud.org/ns}filter-files'; const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag'; + const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle'; /** * Reference to main server object @@ -256,14 +257,19 @@ class FilesReportPlugin extends ServerPlugin { $ns = '{' . $this::NS_OWNCLOUD . '}'; $resultFileIds = null; $systemTagIds = []; + $circlesIds = []; $favoriteFilter = null; foreach ($filterRules as $filterRule) { if ($filterRule['name'] === $ns . 'systemtag') { $systemTagIds[] = $filterRule['value']; } + if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) { + $circlesIds[] = $filterRule['value']; + } if ($filterRule['name'] === $ns . 'favorite') { $favoriteFilter = true; } + } if ($favoriteFilter !== null) { @@ -282,6 +288,15 @@ class FilesReportPlugin extends ServerPlugin { } } + if (!empty($circlesIds)) { + $fileIds = $this->getCirclesFileIds($circlesIds); + if (empty($resultFileIds)) { + $resultFileIds = $fileIds; + } else { + $resultFileIds = array_intersect($fileIds, $resultFileIds); + } + } + return $resultFileIds; } @@ -328,6 +343,19 @@ class FilesReportPlugin extends ServerPlugin { return $resultFileIds; } + /** + * @suppress PhanUndeclaredClassMethod + * @param array $circlesIds + * @return array + */ + private function getCirclesFileIds(array $circlesIds) { + if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { + return array(); + } + return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds); + } + + /** * Prepare propfind response for the given nodes * diff --git a/apps/dav/lib/Connector/Sabre/Principal.php b/apps/dav/lib/Connector/Sabre/Principal.php index a441e1c812..e781618539 100644 --- a/apps/dav/lib/Connector/Sabre/Principal.php +++ b/apps/dav/lib/Connector/Sabre/Principal.php @@ -45,7 +45,7 @@ use OCP\IUserManager; use OCP\IUserSession; use OCP\Share\IManager as IShareManager; use Sabre\DAV\Exception; -use \Sabre\DAV\PropPatch; +use Sabre\DAV\PropPatch; use Sabre\DAVACL\PrincipalBackend\BackendInterface; class Principal implements BackendInterface { @@ -145,7 +145,11 @@ class Principal implements BackendInterface { return $this->userToPrincipal($user); } } else if ($prefix === 'principals/circles') { - return $this->circleToPrincipal($name); + try { + return $this->circleToPrincipal($name); + } catch (QueryException $e) { + return null; + } } return null; } @@ -406,6 +410,7 @@ class Principal implements BackendInterface { /** * @param string $circleUniqueId * @return array|null + * @throws \OCP\AppFramework\QueryException * @suppress PhanUndeclaredClassMethod * @suppress PhanUndeclaredClassCatch */ @@ -438,9 +443,9 @@ class Principal implements BackendInterface { * Returns the list of circles a principal is a member of * * @param string $principal - * @param bool $needGroups * @return array * @throws Exception + * @throws \OCP\AppFramework\QueryException * @suppress PhanUndeclaredClassMethod */ public function getCircleMembership($principal):array { @@ -458,13 +463,13 @@ class Principal implements BackendInterface { $circles = \OCA\Circles\Api\v1\Circles::joinedCircles($name, true); $circles = array_map(function($circle) { - /** @var \OCA\Circles\Model\Circle $group */ + /** @var \OCA\Circles\Model\Circle $circle */ return 'principals/circles/' . urlencode($circle->getUniqueId()); }, $circles); return $circles; - } + return []; } diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php index c10b333e28..874d4d84ff 100644 --- a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php @@ -86,6 +86,10 @@ class PublicCalendarRootTest extends TestCase { ->withAnyParameters() ->willReturn([]); + $this->principal->expects($this->any())->method('getCircleMembership') + ->withAnyParameters() + ->willReturn([]); + $this->backend = new CalDavBackend( $db, $this->principal, @@ -112,6 +116,11 @@ class PublicCalendarRootTest extends TestCase { $this->principal->expects($this->any())->method('getGroupMembership') ->withAnyParameters() ->willReturn([]); + + $this->principal->expects($this->any())->method('getCircleMembership') + ->withAnyParameters() + ->willReturn([]); + $books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER); foreach ($books as $book) { $this->backend->deleteCalendar($book['id']); diff --git a/core/js/files/client.js b/core/js/files/client.js index 2017becf87..98874d165b 100644 --- a/core/js/files/client.js +++ b/core/js/files/client.js @@ -503,7 +503,7 @@ /** * Fetches a flat list of files filtered by a given filter criteria. - * (currently only system tags is supported) + * (currently system tags and circles are supported) * * @param {Object} filter filter criteria * @param {Object} [filter.systemTagIds] list of system tag ids to filter by @@ -525,7 +525,8 @@ properties = options.properties; } - if (!filter || (!filter.systemTagIds && _.isUndefined(filter.favorite))) { + if (!filter || + (!filter.systemTagIds && _.isUndefined(filter.favorite) && !filter.circlesIds) ) { throw 'Missing filter argument'; } @@ -551,6 +552,9 @@ _.each(filter.systemTagIds, function(systemTagIds) { body += ' ' + escapeHTML(systemTagIds) + '\n'; }); + _.each(filter.circlesIds, function(circlesIds) { + body += ' ' + escapeHTML(circlesIds) + '\n'; + }); if (filter.favorite) { body += ' ' + (filter.favorite ? '1': '0') + '\n'; } From 389c2095a6130f16f02ca72e09712596e0e48551 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 13 Mar 2019 00:42:39 +0100 Subject: [PATCH 2/3] Use dependency injection for app manager Signed-off-by: Morris Jobke --- apps/dav/lib/Connector/Sabre/FilesReportPlugin.php | 13 +++++++++++-- apps/dav/lib/Connector/Sabre/ServerFactory.php | 3 ++- apps/dav/lib/Server.php | 3 ++- .../unit/Connector/Sabre/FilesReportPluginTest.php | 11 ++++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php index f4e5de0ea4..6451030c32 100644 --- a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php @@ -25,6 +25,7 @@ namespace OCA\DAV\Connector\Sabre; use OC\Files\View; +use OCP\App\IAppManager; use Sabre\DAV\Exception\PreconditionFailed; use Sabre\DAV\Exception\BadRequest; use Sabre\DAV\ServerPlugin; @@ -97,6 +98,11 @@ class FilesReportPlugin extends ServerPlugin { */ private $userFolder; + /** + * @var IAppManager + */ + private $appManager; + /** * @param Tree $tree * @param View $view @@ -106,6 +112,7 @@ class FilesReportPlugin extends ServerPlugin { * @param IUserSession $userSession * @param IGroupManager $groupManager * @param Folder $userFolder + * @param IAppManager $appManager */ public function __construct(Tree $tree, View $view, @@ -114,7 +121,8 @@ class FilesReportPlugin extends ServerPlugin { ITagManager $fileTagger, IUserSession $userSession, IGroupManager $groupManager, - Folder $userFolder + Folder $userFolder, + IAppManager $appManager ) { $this->tree = $tree; $this->fileView = $view; @@ -124,6 +132,7 @@ class FilesReportPlugin extends ServerPlugin { $this->userSession = $userSession; $this->groupManager = $groupManager; $this->userFolder = $userFolder; + $this->appManager = $appManager; } /** @@ -349,7 +358,7 @@ class FilesReportPlugin extends ServerPlugin { * @return array */ private function getCirclesFileIds(array $circlesIds) { - if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { + if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { return array(); } return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds); diff --git a/apps/dav/lib/Connector/Sabre/ServerFactory.php b/apps/dav/lib/Connector/Sabre/ServerFactory.php index 12b00be43f..1821638189 100644 --- a/apps/dav/lib/Connector/Sabre/ServerFactory.php +++ b/apps/dav/lib/Connector/Sabre/ServerFactory.php @@ -180,7 +180,8 @@ class ServerFactory { \OC::$server->getTagManager(), $this->userSession, \OC::$server->getGroupManager(), - $userFolder + $userFolder, + \OC::$server->getAppManager() )); // custom properties plugin must be the last one $server->addPlugin( diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 84a914dbe9..7eb68ce587 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -271,7 +271,8 @@ class Server { \OC::$server->getTagManager(), $userSession, \OC::$server->getGroupManager(), - $userFolder + $userFolder, + \OC::$server->getAppManager() )); $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend( $this->server->tree, diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php index b6290719e7..09f9ea4dbf 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php @@ -28,6 +28,7 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre; use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\FilesReportPlugin as FilesReportPluginImplementation; +use OCP\App\IAppManager; use OCP\Files\File; use OCP\IConfig; use OCP\IPreview; @@ -81,6 +82,9 @@ class FilesReportPluginTest extends \Test\TestCase { /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject * */ private $previewManager; + /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject * */ + private $appManager; + public function setUp() { parent::setUp(); $this->tree = $this->getMockBuilder(Tree::class) @@ -112,6 +116,10 @@ class FilesReportPluginTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); + $this->appManager = $this->getMockBuilder(IAppManager::class) + ->disableOriginalConstructor() + ->getMock(); + $this->tagManager = $this->createMock(ISystemTagManager::class); $this->tagMapper = $this->createMock(ISystemTagObjectMapper::class); $this->userSession = $this->createMock(IUserSession::class); @@ -140,7 +148,8 @@ class FilesReportPluginTest extends \Test\TestCase { $privateTagManager, $this->userSession, $this->groupManager, - $this->userFolder + $this->userFolder, + $this->appManager ); } From de8e02628efa546c66f88b49516e2c21b79c27d8 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 13 Mar 2019 23:01:47 +0100 Subject: [PATCH 3/3] Improve code Signed-off-by: Morris Jobke --- apps/dav/lib/Connector/Sabre/FilesReportPlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php index 6451030c32..d679fa307c 100644 --- a/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesReportPlugin.php @@ -358,8 +358,8 @@ class FilesReportPlugin extends ServerPlugin { * @return array */ private function getCirclesFileIds(array $circlesIds) { - if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { - return array(); + if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { + return []; } return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds); }