Merge pull request #14429 from tobiasKaminsky/shareesOnDav

Show sharees via propfind
This commit is contained in:
Roeland Jago Douma 2019-08-13 20:22:47 +02:00 committed by GitHub
commit 34868f0964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 115 deletions

View File

@ -126,6 +126,7 @@ return array(
'OCA\\DAV\\Connector\\Sabre\\Server' => $baseDir . '/../lib/Connector/Sabre/Server.php', 'OCA\\DAV\\Connector\\Sabre\\Server' => $baseDir . '/../lib/Connector/Sabre/Server.php',
'OCA\\DAV\\Connector\\Sabre\\ServerFactory' => $baseDir . '/../lib/Connector/Sabre/ServerFactory.php', 'OCA\\DAV\\Connector\\Sabre\\ServerFactory' => $baseDir . '/../lib/Connector/Sabre/ServerFactory.php',
'OCA\\DAV\\Connector\\Sabre\\ShareTypeList' => $baseDir . '/../lib/Connector/Sabre/ShareTypeList.php', 'OCA\\DAV\\Connector\\Sabre\\ShareTypeList' => $baseDir . '/../lib/Connector/Sabre/ShareTypeList.php',
'OCA\\DAV\\Connector\\Sabre\\ShareeList' => $baseDir . '/../lib/Connector/Sabre/ShareeList.php',
'OCA\\DAV\\Connector\\Sabre\\SharesPlugin' => $baseDir . '/../lib/Connector/Sabre/SharesPlugin.php', 'OCA\\DAV\\Connector\\Sabre\\SharesPlugin' => $baseDir . '/../lib/Connector/Sabre/SharesPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\TagList' => $baseDir . '/../lib/Connector/Sabre/TagList.php', 'OCA\\DAV\\Connector\\Sabre\\TagList' => $baseDir . '/../lib/Connector/Sabre/TagList.php',
'OCA\\DAV\\Connector\\Sabre\\TagsPlugin' => $baseDir . '/../lib/Connector/Sabre/TagsPlugin.php', 'OCA\\DAV\\Connector\\Sabre\\TagsPlugin' => $baseDir . '/../lib/Connector/Sabre/TagsPlugin.php',

View File

@ -141,6 +141,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Connector\\Sabre\\Server' => __DIR__ . '/..' . '/../lib/Connector/Sabre/Server.php', 'OCA\\DAV\\Connector\\Sabre\\Server' => __DIR__ . '/..' . '/../lib/Connector/Sabre/Server.php',
'OCA\\DAV\\Connector\\Sabre\\ServerFactory' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ServerFactory.php', 'OCA\\DAV\\Connector\\Sabre\\ServerFactory' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ServerFactory.php',
'OCA\\DAV\\Connector\\Sabre\\ShareTypeList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ShareTypeList.php', 'OCA\\DAV\\Connector\\Sabre\\ShareTypeList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ShareTypeList.php',
'OCA\\DAV\\Connector\\Sabre\\ShareeList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/ShareeList.php',
'OCA\\DAV\\Connector\\Sabre\\SharesPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/SharesPlugin.php', 'OCA\\DAV\\Connector\\Sabre\\SharesPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/SharesPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\TagList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/TagList.php', 'OCA\\DAV\\Connector\\Sabre\\TagList' => __DIR__ . '/..' . '/../lib/Connector/Sabre/TagList.php',
'OCA\\DAV\\Connector\\Sabre\\TagsPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/TagsPlugin.php', 'OCA\\DAV\\Connector\\Sabre\\TagsPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/TagsPlugin.php',

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\Connector\Sabre;
use OCP\Share\IShare;
use Sabre\Xml\Element;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
use Sabre\Xml\XmlSerializable;
/**
* This property contains multiple "sharee" elements, each containing a share sharee
*/
class ShareeList implements XmlSerializable {
const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
/** @var IShare[] */
private $shares;
public function __construct(array $shares) {
$this->shares = $shares;
}
/**
* The xmlSerialize metod is called during xml writing.
*
* @param Writer $writer
* @return void
*/
function xmlSerialize(Writer $writer) {
foreach ($this->shares as $share) {
$writer->startElement('{' . self::NS_NEXTCLOUD . '}sharee');
$writer->writeElement('{' . self::NS_NEXTCLOUD . '}id', $share->getSharedWith());
$writer->writeElement('{' . self::NS_NEXTCLOUD . '}display-name', $share->getSharedWithDisplayName());
$writer->writeElement('{' . self::NS_NEXTCLOUD . '}type', $share->getShareType());
$writer->endElement();
}
}
}

View File

@ -37,7 +37,9 @@ use OCP\Share\IShare;
class SharesPlugin extends \Sabre\DAV\ServerPlugin { class SharesPlugin extends \Sabre\DAV\ServerPlugin {
const NS_OWNCLOUD = 'http://owncloud.org/ns'; const NS_OWNCLOUD = 'http://owncloud.org/ns';
const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types'; const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types';
const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees';
/** /**
* Reference to main server object * Reference to main server object
@ -66,10 +68,8 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
*/ */
private $userFolder; private $userFolder;
/** /** @var IShare[] */
* @var IShare[] private $cachedShares = [];
*/
private $cachedShareTypes;
private $cachedFolders = []; private $cachedFolders = [];
@ -89,7 +89,6 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
$this->shareManager = $shareManager; $this->shareManager = $shareManager;
$this->userFolder = $userFolder; $this->userFolder = $userFolder;
$this->userId = $userSession->getUser()->getUID(); $this->userId = $userSession->getUser()->getUID();
$this->cachedShareTypes = [];
} }
/** /**
@ -106,20 +105,14 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
$server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc'; $server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc';
$server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class; $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class;
$server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME; $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME;
$server->protectedProperties[] = self::SHAREES_PROPERTYNAME;
$this->server = $server; $this->server = $server;
$this->server->on('propFind', array($this, 'handleGetProperties')); $this->server->on('propFind', array($this, 'handleGetProperties'));
} }
/** private function getShare(\OCP\Files\Node $node): array {
* Return a list of share types for outgoing shares $result = [];
*
* @param \OCP\Files\Node $node file node
*
* @return int[] array of share types
*/
private function getShareTypes(\OCP\Files\Node $node) {
$shareTypes = [];
$requestedShareTypes = [ $requestedShareTypes = [
\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_GROUP,
@ -130,40 +123,47 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
\OCP\Share::SHARE_TYPE_CIRCLE, \OCP\Share::SHARE_TYPE_CIRCLE,
]; ];
foreach ($requestedShareTypes as $requestedShareType) { foreach ($requestedShareTypes as $requestedShareType) {
// one of each type is enough to find out about the types
$shares = $this->shareManager->getSharesBy( $shares = $this->shareManager->getSharesBy(
$this->userId, $this->userId,
$requestedShareType, $requestedShareType,
$node, $node,
false, false,
1 -1
); );
if (!empty($shares)) { foreach ($shares as $share) {
$shareTypes[] = $requestedShareType; $result[] = $share;
} }
} }
return $shareTypes; return $result;
} }
private function getSharesTypesInFolder(\OCP\Files\Folder $node) { private function getSharesFolder(\OCP\Files\Folder $node): array {
$shares = $this->shareManager->getSharesInFolder( return $this->shareManager->getSharesInFolder(
$this->userId, $this->userId,
$node, $node,
true true
); );
}
$shareTypesByFileId = []; private function getShares(\Sabre\DAV\INode $sabreNode): array {
if (isset($this->cachedShares[$sabreNode->getId()])) {
foreach($shares as $fileId => $sharesForFile) { $shares = $this->cachedShares[$sabreNode->getId()];
$types = array_map(function(IShare $share) { } else {
return $share->getShareType(); list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
}, $sharesForFile); if ($parentPath === '') {
$types = array_unique($types); $parentPath = '/';
sort($types); }
$shareTypesByFileId[$fileId] = $types; // if we already cached the folder this file is in we know there are no shares for this file
if (array_search($parentPath, $this->cachedFolders) === false) {
$node = $this->userFolder->get($sabreNode->getPath());
$shares = $this->getShare($node);
$this->cachedShares[$sabreNode->getId()] = $shares;
} else {
return [];
}
} }
return $shareTypesByFileId; return $shares;
} }
/** /**
@ -183,36 +183,34 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin {
// need prefetch ? // need prefetch ?
if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory
&& $propFind->getDepth() !== 0 && $propFind->getDepth() !== 0
&& !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) && (
!is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ||
!is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME))
)
) { ) {
$folderNode = $this->userFolder->get($sabreNode->getPath()); $folderNode = $this->userFolder->get($sabreNode->getPath());
$childShares = $this->getSharesTypesInFolder($folderNode);
$this->cachedFolders[] = $sabreNode->getPath(); $this->cachedFolders[] = $sabreNode->getPath();
$this->cachedShareTypes[$folderNode->getId()] = $this->getShareTypes($folderNode); $childShares = $this->getSharesFolder($folderNode);
foreach ($childShares as $id => $shares) { foreach ($childShares as $id => $shares) {
$this->cachedShareTypes[$id] = $shares; $this->cachedShares[$id] = $shares;
} }
} }
$propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) { $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) {
if (isset($this->cachedShareTypes[$sabreNode->getId()])) { $shares = $this->getShares($sabreNode);
$shareTypes = $this->cachedShareTypes[$sabreNode->getId()];
} else { $shareTypes = array_unique(array_map(function(IShare $share) {
list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath()); return $share->getShareType();
if ($parentPath === '') { }, $shares));
$parentPath = '/';
}
// if we already cached the folder this file is in we know there are no shares for this file
if (array_search($parentPath, $this->cachedFolders) === false) {
$node = $this->userFolder->get($sabreNode->getPath());
$shareTypes = $this->getShareTypes($node);
} else {
return [];
}
}
return new ShareTypeList($shareTypes); return new ShareTypeList($shareTypes);
}); });
$propFind->handle(self::SHAREES_PROPERTYNAME, function() use ($sabreNode) {
$shares = $this->getShares($sabreNode);
return new ShareeList($shares);
});
} }
} }

View File

@ -68,28 +68,17 @@ class SharesPluginTest extends \Test\TestCase {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->server = new \Sabre\DAV\Server(); $this->server = new \Sabre\DAV\Server();
$this->tree = $this->getMockBuilder(Tree::class) $this->tree = $this->createMock(Tree::class);
->disableOriginalConstructor() $this->shareManager = $this->createMock(IManager::class);
->getMock(); $user = $this->createMock(IUser::class);
$this->shareManager = $this->getMockBuilder(IManager::class)
->disableOriginalConstructor()
->getMock();
$user = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$user->expects($this->once()) $user->expects($this->once())
->method('getUID') ->method('getUID')
->will($this->returnValue('user1')); ->will($this->returnValue('user1'));
$userSession = $this->getMockBuilder(IUserSession::class) $userSession = $this->createMock(IUserSession::class);
->disableOriginalConstructor()
->getMock();
$userSession->expects($this->once()) $userSession->expects($this->once())
->method('getUser') ->method('getUser')
->will($this->returnValue($user)); ->will($this->returnValue($user));
$this->userFolder = $this->createMock(Folder::class);
$this->userFolder = $this->getMockBuilder(Folder::class)
->disableOriginalConstructor()
->getMock();
$this->plugin = new \OCA\DAV\Connector\Sabre\SharesPlugin( $this->plugin = new \OCA\DAV\Connector\Sabre\SharesPlugin(
$this->tree, $this->tree,
@ -131,11 +120,14 @@ class SharesPluginTest extends \Test\TestCase {
$this->anything(), $this->anything(),
$this->anything(), $this->anything(),
$this->equalTo(false), $this->equalTo(false),
$this->equalTo(1) $this->equalTo(-1)
) )
->will($this->returnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes){ ->will($this->returnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes){
if (in_array($requestedShareType, $shareTypes)) { if (in_array($requestedShareType, $shareTypes)) {
return ['dummyshare']; $share = $this->createMock(IShare::class);
$share->method('getShareType')
->willReturn($requestedShareType);
return [$share];
} }
return []; return [];
})); }));
@ -162,30 +154,18 @@ class SharesPluginTest extends \Test\TestCase {
* @dataProvider sharesGetPropertiesDataProvider * @dataProvider sharesGetPropertiesDataProvider
*/ */
public function testPreloadThenGetProperties($shareTypes) { public function testPreloadThenGetProperties($shareTypes) {
$sabreNode1 = $this->getMockBuilder(File::class) $sabreNode1 = $this->createMock(File::class);
->disableOriginalConstructor() $sabreNode1->method('getId')
->getMock(); ->willReturn(111);
$sabreNode1->expects($this->any()) $sabreNode2 = $this->createMock(File::class);
->method('getId') $sabreNode2->method('getId')
->will($this->returnValue(111)); ->willReturn(222);
$sabreNode1->expects($this->any()) $sabreNode2->method('getPath')
->method('getPath'); ->willReturn('/subdir/foo');
$sabreNode2 = $this->getMockBuilder(File::class)
->disableOriginalConstructor()
->getMock();
$sabreNode2->expects($this->any())
->method('getId')
->will($this->returnValue(222));
$sabreNode2->expects($this->any())
->method('getPath')
->will($this->returnValue('/subdir/foo'));
$sabreNode = $this->getMockBuilder(Directory::class) $sabreNode = $this->createMock(Directory::class);
->disableOriginalConstructor() $sabreNode->method('getId')
->getMock(); ->willReturn(123);
$sabreNode->expects($this->any())
->method('getId')
->will($this->returnValue(123));
// never, because we use getDirectoryListing from the Node API instead // never, because we use getDirectoryListing from the Node API instead
$sabreNode->expects($this->never()) $sabreNode->expects($this->never())
->method('getChildren'); ->method('getChildren');
@ -194,29 +174,19 @@ class SharesPluginTest extends \Test\TestCase {
->will($this->returnValue('/subdir')); ->will($this->returnValue('/subdir'));
// node API nodes // node API nodes
$node = $this->getMockBuilder(Folder::class) $node = $this->createMock(Folder::class);
->disableOriginalConstructor() $node->method('getId')
->getMock(); ->willReturn(123);
$node->expects($this->any()) $node1 = $this->createMock(File::class);
->method('getId') $node1->method('getId')
->will($this->returnValue(123)); ->willReturn(111);
$node1 = $this->getMockBuilder(File::class) $node2 = $this->createMock(File::class);
->disableOriginalConstructor() $node2->method('getId')
->getMock(); ->willReturn(222);
$node1->expects($this->any())
->method('getId')
->will($this->returnValue(111));
$node2 = $this->getMockBuilder(File::class)
->disableOriginalConstructor()
->getMock();
$node2->expects($this->any())
->method('getId')
->will($this->returnValue(222));
$this->userFolder->expects($this->once()) $this->userFolder->method('get')
->method('get')
->with('/subdir') ->with('/subdir')
->will($this->returnValue($node)); ->willReturn($node);
$dummyShares = array_map(function($type) { $dummyShares = array_map(function($type) {
$share = $this->getMockBuilder(IShare::class)->getMock(); $share = $this->getMockBuilder(IShare::class)->getMock();
@ -233,11 +203,15 @@ class SharesPluginTest extends \Test\TestCase {
$this->anything(), $this->anything(),
$this->anything(), $this->anything(),
$this->equalTo(false), $this->equalTo(false),
$this->equalTo(1) $this->equalTo(-1)
) )
->will($this->returnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes){ ->will($this->returnCallback(function($userId, $requestedShareType, $node, $flag, $limit) use ($shareTypes, $dummyShares){
if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) { if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) {
return ['dummyshare']; foreach ($dummyShares as $dummyShare) {
if ($dummyShare->getShareType() === $requestedShareType) {
return [$dummyShare];
}
}
} }
return []; return [];