summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@owncloud.com>2015-11-06 12:05:19 +0100
committerRoeland Jago Douma <rullzer@owncloud.com>2015-11-16 13:32:28 +0100
commit1e9fc332125aa8e730c3cdccd88102cfcd6bce9d (patch)
tree7345388bab10435c94c382ddf172cbe1551d4bf7 /apps
parent5e3d29b661e5fb8aa40943a6dec621e3343092f3 (diff)
downloadnextcloud-server-1e9fc332125aa8e730c3cdccd88102cfcd6bce9d.tar.gz
nextcloud-server-1e9fc332125aa8e730c3cdccd88102cfcd6bce9d.zip
[Share2.0] OCS Share API getShare uses new code
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/api/ocssharewrapper.php14
-rw-r--r--apps/files_sharing/api/share20ocs.php100
-rw-r--r--apps/files_sharing/tests/api/share20ocstest.php259
3 files changed, 347 insertions, 26 deletions
diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php
index 8c0d8f7d150..3ce2901dfb4 100644
--- a/apps/files_sharing/api/ocssharewrapper.php
+++ b/apps/files_sharing/api/ocssharewrapper.php
@@ -35,15 +35,16 @@ class OCSShareWrapper {
\OC::$server->getUserFolder(),
new \OC\Share20\DefaultShareProvider(
\OC::$server->getDatabaseConnection(),
- \OC::$server->getUserManager(),
- \OC::$server->getGroupManager(),
- \OC::$server->getUserFolder()
+ \OC::$server->getUserManager(),
+ \OC::$server->getGroupManager(),
+ \OC::$server->getUserFolder()
)
),
\OC::$server->getGroupManager(),
\OC::$server->getUserManager(),
\OC::$server->getRequest(),
- \OC::$server->getUserFolder());
+ \OC::$server->getUserFolder(),
+ \OC::$server->getURLGenerator());
}
public function getAllShares($params) {
@@ -55,7 +56,8 @@ class OCSShareWrapper {
}
public function getShare($params) {
- return \OCA\Files_Sharing\API\Local::getShare($params);
+ $id = $params['id'];
+ return $this->getShare20OCS()->getShare($id);
}
public function updateShare($params) {
@@ -63,7 +65,7 @@ class OCSShareWrapper {
}
public function deleteShare($params) {
- $id = (int)$params['id'];
+ $id = $params['id'];
return $this->getShare20OCS()->deleteShare($id);
}
}
diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php
index 8a7f90c0023..aaf5a3c72b6 100644
--- a/apps/files_sharing/api/share20ocs.php
+++ b/apps/files_sharing/api/share20ocs.php
@@ -20,39 +20,125 @@
*/
namespace OCA\Files_Sharing\API;
+use OC\Share20\IShare;
+
class Share20OCS {
- /** @var OC\Share20\Manager */
+ /** @var \OC\Share20\Manager */
private $shareManager;
- /** @var OCP\IGroupManager */
+ /** @var \OCP\IGroupManager */
private $groupManager;
- /** @var OCP\IUserManager */
+ /** @var \OCP\IUserManager */
private $userManager;
- /** @var OCP\IRequest */
+ /** @var \OCP\IRequest */
private $request;
- /** @var OCP\Files\Folder */
+ /** @var \OCP\Files\Folder */
private $userFolder;
public function __construct(\OC\Share20\Manager $shareManager,
\OCP\IGroupManager $groupManager,
\OCP\IUserManager $userManager,
\OCP\IRequest $request,
- \OCP\Files\Folder $userFolder) {
+ \OCP\Files\Folder $userFolder,
+ \OCP\IURLGenerator $urlGenerator) {
$this->shareManager = $shareManager;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->request = $request;
$this->userFolder = $userFolder;
+ $this->urlGenerator = $urlGenerator;
+ }
+
+ /**
+ * Convert an IShare to an array for OCS output
+ *
+ * @param IShare $share
+ * @return array
+ */
+ protected function formatShare($share) {
+ $result = [
+ 'id' => $share->getId(),
+ 'share_type' => $share->getShareType(),
+ 'uid_owner' => $share->getSharedBy()->getUID(),
+ 'displayname_owner' => $share->getSharedBy()->getDisplayName(),
+ 'permissions' => $share->getPermissions(),
+ 'stime' => $share->getShareTime(),
+ 'parent' => $share->getParent(),
+ 'expiration' => null,
+ 'token' => null,
+ ];
+
+ $path = $share->getPath();
+ $result['path'] = $this->userFolder->getRelativePath($path->getPath());
+ if ($path instanceOf \OCP\Files\Folder) {
+ $result['item_type'] = 'folder';
+ } else {
+ $result['item_type'] = 'file';
+ }
+ $result['storage_id'] = $path->getStorage()->getId();
+ $result['storage'] = \OC\Files\Cache\Storage::getNumericStorageId($path->getStorage()->getId());
+ $result['item_source'] = $path->getId();
+ $result['file_source'] = $path->getId();
+ $result['file_parent'] = $path->getParent()->getId();
+ $result['file_target'] = $share->getTarget();
+
+ if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
+ $sharedWith = $share->getSharedWith();
+ $result['share_with'] = $sharedWith->getUID();
+ $result['share_with_displayname'] = $sharedWith->getDisplayName();
+ } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
+ $sharedWith = $share->getSharedWith();
+ $result['share_with'] = $sharedWith->getGID();
+ $result['share_with_displayname'] = $sharedWith->getGID();
+ } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
+
+ $result['share_with'] = $share->getPassword();
+ $result['share_with_displayname'] = $share->getPassword();
+
+ $result['token'] = $share->getToken();
+ $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
+
+ $expiration = $share->getExpirationDate();
+ if ($expiration !== null) {
+ $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
+ }
+
+ } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
+ $result['share_with'] = $share->getSharedWith();
+ $result['share_with_displayname'] = $share->getSharedWith();
+ $result['token'] = $share->getToken();
+ }
+
+ $result['mail_send'] = $share->getMailSend() ? 1 : 0;
+
+ return $result;
+ }
+
+ /**
+ * Get a specific share by id
+ *
+ * @param string $id
+ * @return \OC_OCS_Result
+ */
+ public function getShare($id) {
+ try {
+ $share = $this->shareManager->getShareById($id);
+ } catch (\OC\Share20\Exception\ShareNotFound $e) {
+ return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
+ }
+
+ $share = $this->formatShare($share);
+ return new \OC_OCS_Result($share);
}
/**
* Delete a share
*
- * @param int $id
+ * @param string $id
* @return \OC_OCS_Result
*/
public function deleteShare($id) {
diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php
index 9c4377a2a7f..f74585eb47d 100644
--- a/apps/files_sharing/tests/api/share20ocstest.php
+++ b/apps/files_sharing/tests/api/share20ocstest.php
@@ -39,6 +39,9 @@ class Share20OCSTest extends \Test\TestCase {
/** @var OCP\Files\Folder */
private $userFolder;
+ /** @var OCP\IURLGenerator */
+ private $urlGenerator;
+
/** @var OCS */
private $ocs;
@@ -46,24 +49,18 @@ class Share20OCSTest extends \Test\TestCase {
$this->shareManager = $this->getMockBuilder('OC\Share20\Manager')
->disableOriginalConstructor()
->getMock();
- $this->groupManager = $this->getMockBuilder('OCP\IGroupManager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->userManager = $this->getMockBuilder('OCP\IUserManager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->request = $this->getMockBuilder('OCP\IRequest')
- ->disableOriginalConstructor()
- ->getMock();
- $this->userFolder = $this->getMockBuilder('OCP\Files\Folder')
- ->disableOriginalConstructor()
- ->getMock();
+ $this->groupManager = $this->getMock('OCP\IGroupManager');
+ $this->userManager = $this->getMock('OCP\IUserManager');
+ $this->request = $this->getMock('OCP\IRequest');
+ $this->userFolder = $this->getMock('OCP\Files\Folder');
+ $this->urlGenerator = $this->getMock('OCP\IURLGenerator');
$this->ocs = new Share20OCS($this->shareManager,
$this->groupManager,
$this->userManager,
$this->request,
- $this->userFolder);
+ $this->userFolder,
+ $this->urlGenerator);
}
public function testDeleteShareShareNotFound() {
@@ -110,4 +107,240 @@ class Share20OCSTest extends \Test\TestCase {
$expected = new \OC_OCS_Result();
$this->assertEquals($expected, $this->ocs->deleteShare(42));
}
+
+ public function testGetGetShareNotExists() {
+ $this->shareManager
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with(42)
+ ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound()));
+
+ $expected = new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
+ $this->assertEquals($expected, $this->ocs->getShare(42));
+ }
+
+ public function createShare($id, $shareType, $sharedWith, $sharedBy, $path, $permissions,
+ $shareTime, $expiration, $parent, $target, $mail_send, $token=null,
+ $password=null) {
+ $share = $this->getMock('OC\Share20\IShare');
+ $share->method('getId')->willReturn($id);
+ $share->method('getShareType')->willReturn($shareType);
+ $share->method('getSharedWith')->willReturn($sharedWith);
+ $share->method('getSharedBy')->willReturn($sharedBy);
+ $share->method('getPath')->willReturn($path);
+ $share->method('getPermissions')->willReturn($permissions);
+ $share->method('getShareTime')->willReturn($shareTime);
+ $share->method('getExpirationDate')->willReturn($expiration);
+ $share->method('getParent')->willReturn($parent);
+ $share->method('getTarget')->willReturn($target);
+ $share->method('getMailSend')->willReturn($mail_send);
+ $share->method('getToken')->willReturn($token);
+ $share->method('getPassword')->willReturn($password);
+
+ return $share;
+ }
+
+ public function dataGetShare() {
+ $data = [];
+
+ $owner = $this->getMock('OCP\IUser');
+ $owner->method('getUID')->willReturn('ownerId');
+ $owner->method('getDisplayName')->willReturn('ownerDisplay');
+
+ $user = $this->getMock('OCP\IUser');
+ $user->method('getUID')->willReturn('userId');
+ $user->method('getDisplayName')->willReturn('userDisplay');
+
+ $group = $this->getMock('OCP\IGroup');
+ $group->method('getGID')->willReturn('groupId');
+
+ $storage = $this->getMock('OCP\Files\Storage');
+ $storage->method('getId')->willReturn('STORAGE');
+
+ $parentFolder = $this->getMock('OCP\Files\Folder');
+ $parentFolder->method('getId')->willReturn(3);
+
+ $file = $this->getMock('OCP\Files\File');
+ $file->method('getId')->willReturn(1);
+ $file->method('getPath')->willReturn('file');
+ $file->method('getStorage')->willReturn($storage);
+ $file->method('getParent')->willReturn($parentFolder);
+
+ $folder = $this->getMock('OCP\Files\Folder');
+ $folder->method('getId')->willReturn(2);
+ $folder->method('getPath')->willReturn('folder');
+ $folder->method('getStorage')->willReturn($storage);
+ $folder->method('getParent')->willReturn($parentFolder);
+
+ // File shared with user
+ $share = $this->createShare(100,
+ \OCP\Share::SHARE_TYPE_USER,
+ $user,
+ $owner,
+ $file,
+ 4,
+ 5,
+ null,
+ 6,
+ 'target',
+ 0);
+ $expected = [
+ 'id' => 100,
+ 'share_type' => \OCP\Share::SHARE_TYPE_USER,
+ 'share_with' => 'userId',
+ 'share_with_displayname' => 'userDisplay',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'file',
+ 'item_source' => 1,
+ 'file_source' => 1,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => null,
+ 'expiration' => null,
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'file',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ ];
+ $data[] = [$share, $expected];
+
+ // Folder shared with group
+ $share = $this->createShare(101,
+ \OCP\Share::SHARE_TYPE_GROUP,
+ $group,
+ $owner,
+ $folder,
+ 4,
+ 5,
+ null,
+ 6,
+ 'target',
+ 0);
+ $expected = [
+ 'id' => 101,
+ 'share_type' => \OCP\Share::SHARE_TYPE_GROUP,
+ 'share_with' => 'groupId',
+ 'share_with_displayname' => 'groupId',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'folder',
+ 'item_source' => 2,
+ 'file_source' => 2,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => null,
+ 'expiration' => null,
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'folder',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ ];
+ $data[] = [$share, $expected];
+
+ // Folder shared with remote
+ $share = $this->createShare(101,
+ \OCP\Share::SHARE_TYPE_REMOTE,
+ 'user@remote.com',
+ $owner,
+ $folder,
+ 4,
+ 5,
+ null,
+ 6,
+ 'target',
+ 0);
+ $expected = [
+ 'id' => 101,
+ 'share_type' => \OCP\Share::SHARE_TYPE_REMOTE,
+ 'share_with' => 'user@remote.com',
+ 'share_with_displayname' => 'user@remote.com',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'folder',
+ 'item_source' => 2,
+ 'file_source' => 2,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => null,
+ 'expiration' => null,
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'folder',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ ];
+ $data[] = [$share, $expected];
+
+ // File shared by link with Expire
+ $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
+ $share = $this->createShare(101,
+ \OCP\Share::SHARE_TYPE_LINK,
+ null,
+ $owner,
+ $folder,
+ 4,
+ 5,
+ $expire,
+ 6,
+ 'target',
+ 0,
+ 'token',
+ 'password');
+ $expected = [
+ 'id' => 101,
+ 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
+ 'share_with' => 'password',
+ 'share_with_displayname' => 'password',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'folder',
+ 'item_source' => 2,
+ 'file_source' => 2,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => 'token',
+ 'expiration' => '2000-01-02 00:00:00',
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'folder',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ 'url' => 'url',
+ ];
+ $data[] = [$share, $expected];
+
+ return $data;
+ }
+
+ /**
+ * @dataProvider dataGetShare
+ */
+ public function testGetShare(\OC\Share20\IShare $share, array $result) {
+ $this->shareManager
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with($share->getId())
+ ->willReturn($share);
+
+ $this->userFolder
+ ->method('getRelativePath')
+ ->will($this->returnArgument(0));
+
+ $this->urlGenerator
+ ->method('linkToRouteAbsolute')
+ ->willReturn('url');
+
+ $expected = new \OC_OCS_Result($result);
+ $this->assertEquals($expected->getData(), $this->ocs->getShare($share->getId())->getData()); }
}