From 38d3a638ed2ec055ddf4a09da682961b6976bc4d Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 15 Dec 2015 09:54:50 +0100 Subject: [Share 2.0] Enable share creation via OCS API --- apps/files_sharing/api/ocssharewrapper.php | 12 +- apps/files_sharing/api/share20ocs.php | 148 ++++++++++++- apps/files_sharing/tests/api/share20ocstest.php | 277 ++++++++++++++++++++++++ 3 files changed, 432 insertions(+), 5 deletions(-) (limited to 'apps') diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php index ca04c656c28..2896f3fbf01 100644 --- a/apps/files_sharing/api/ocssharewrapper.php +++ b/apps/files_sharing/api/ocssharewrapper.php @@ -29,13 +29,17 @@ class OCSShareWrapper { return new Share20OCS( new \OC\Share20\Manager( \OC::$server->getLogger(), - \OC::$server->getAppConfig(), + \OC::$server->getConfig(), new \OC\Share20\DefaultShareProvider( \OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getGroupManager(), \OC::$server->getRootFolder() - ) + ), + \OC::$server->getSecureRandom(), + \OC::$server->getHasher(), + \OC::$server->getMountManager(), + \OC::$server->getGroupManager() ), \OC::$server->getGroupManager(), \OC::$server->getUserManager(), @@ -49,8 +53,8 @@ class OCSShareWrapper { return \OCA\Files_Sharing\API\Local::getAllShares($params); } - public function createShare($params) { - return \OCA\Files_Sharing\API\Local::createShare($params); + public function createShare() { + return $this->getShare20OCS()->createShare(); } public function getShare($params) { diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index 6c25b4a4426..bf644ce00f6 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -25,7 +25,6 @@ use OC\Share20\IShare; use OCP\IGroupManager; use OCP\IUserManager; use OCP\IRequest; -use OCP\Files\Folder; use OCP\IURLGenerator; use OCP\IUser; use OCP\Files\IRootFolder; @@ -191,6 +190,127 @@ class Share20OCS { return new \OC_OCS_Result(); } + /** + * @return \OC_OCS_Result + */ + public function createShare() { + $share = $this->shareManager->newShare(); + + // Verify path + $path = $this->request->getParam('path', null); + if ($path === null) { + return new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); + } + + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); + try { + $path = $userFolder->get($path); + } catch (\OCP\Files\NotFoundException $e) { + return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + } + + $share->setPath($path); + + // Parse permissions (if available) + $permissions = $this->request->getParam('permissions', null); + if ($permissions === null) { + $permissions = \OCP\Constants::PERMISSION_ALL; + } else { + $permissions = (int)$permissions; + } + + if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { + return new \OC_OCS_Result(null, 404, 'invalid permissions'); + } + + // Shares always require read permissions + $permissions |= \OCP\Constants::PERMISSION_READ; + + if ($path instanceof \OCP\Files\File) { + // Single file shares should never have delete or create permissions + $permissions &= ~\OCP\Constants::PERMISSION_DELETE; + $permissions &= ~\OCP\Constants::PERMISSION_CREATE; + } + + $shareWith = $this->request->getParam('shareWith', null); + $shareType = (int)$this->request->getParam('shareType', '-1'); + + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { + // Valid user is required to share + if ($shareWith === null || !$this->userManager->userExists($shareWith)) { + return new \OC_OCS_Result(null, 404, 'please specify a valid user'); + } + $share->setSharedWith($this->userManager->get($shareWith)); + $share->setPermissions($permissions); + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { + // Valid group is required to share + if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { + return new \OC_OCS_Result(null, 404, 'please specify a valid group'); + } + $share->setSharedWith($this->groupManager->get($shareWith)); + $share->setPermissions($permissions); + } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { + //Can we even share links? + if (!$this->shareManager->shareApiAllowLinks()) { + return new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator'); + } + + $publicUpload = $this->request->getParam('publicUpload', null); + if ($publicUpload === 'true') { + // Check if public upload is allowed + if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { + return new \OC_OCS_Result(null, 403, '"public upload disabled by the administrator'); + } + + // Public upload can only be set for folders + if ($path instanceof \OCP\Files\File) { + return new \OC_OCS_Result(null, 404, '"public upload is only possible for public shared folders'); + } + + $share->setPermissions( + \OCP\Constants::PERMISSION_READ | + \OCP\Constants::PERMISSION_CREATE | + \OCP\Constants::PERMISSION_UPDATE + ); + } else { + $share->setPermissions(\OCP\Constants::PERMISSION_READ); + } + + // Set password + $share->setPassword($this->request->getParam('password', null)); + + //Expire date + $expireDate = $this->request->getParam('expireDate', null); + + if ($expireDate !== null) { + try { + $expireDate = $this->parseDate($expireDate); + $share->setExpirationDate($expireDate); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.'); + } + } + + } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { + //fixme Remote shares are handled by old code path for now + return \OCA\Files_Sharing\API\Local::createShare([]); + } else { + return new \OC_OCS_Result(null, 400, "unknown share type"); + } + + $share->setShareType($shareType); + $share->setSharedBy($this->currentUser); + + try { + $share = $this->shareManager->createShare($share); + } catch (\Exception $e) { + return new \OC_OCS_Result(null, 404, $e->getMessage()); + } + + $share = $this->formatShare($share); + return new \OC_OCS_Result($share); + } + /** * @param IShare $share * @return bool @@ -216,4 +336,30 @@ class Share20OCS { return false; } + + /** + * Make sure that the passed date is valid ISO 8601 + * So YYYY-MM-DD + * If not throw an exception + * + * @param string $expireDate + * + * @throws \Exception + * @return \DateTime + */ + private function parseDate($expireDate) { + try { + $date = new \DateTime($expireDate); + } catch (\Exception $e) { + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); + } + + if ($date === false) { + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); + } + + $date->setTime(0,0,0); + + return $date; + } } diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php index b7c56fe17f6..2d69636541e 100644 --- a/apps/files_sharing/tests/api/share20ocstest.php +++ b/apps/files_sharing/tests/api/share20ocstest.php @@ -65,6 +65,7 @@ class Share20OCSTest extends \Test\TestCase { $this->rootFolder = $this->getMock('OCP\Files\IRootFolder'); $this->urlGenerator = $this->getMock('OCP\IURLGenerator'); $this->currentUser = $this->getMock('OCP\IUser'); + $this->currentUser->method('getUID')->willReturn('currentUser'); $this->ocs = new Share20OCS( $this->shareManager, @@ -391,4 +392,280 @@ class Share20OCSTest extends \Test\TestCase { $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); } + + public function testCreateShareNoPath() { + $expected = new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); + + $result = $this->ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareInvalidPath() { + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'invalid-path'], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $userFolder->expects($this->once()) + ->method('get') + ->with('invalid-path') + ->will($this->throwException(new \OCP\Files\NotFoundException())); + + $expected = new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + + $result = $this->ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareInvalidPermissions() { + $share = $this->getMock('\OC\Share20\IShare'); + $this->shareManager->method('newShare')->willReturn($share); + + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'valid-path'], + ['permissions', null, 32], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path = $this->getMock('\OCP\Files\File'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + + $expected = new \OC_OCS_Result(null, 404, 'invalid permissions'); + + $result = $this->ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareUserNoShareWith() { + $share = $this->getMock('\OC\Share20\IShare'); + $this->shareManager->method('newShare')->willReturn($share); + + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'valid-path'], + ['permissions', null, \OCP\Constants::PERMISSION_ALL], + ['shareType', $this->any(), \OCP\Share::SHARE_TYPE_USER], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path = $this->getMock('\OCP\Files\File'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + + $expected = new \OC_OCS_Result(null, 404, 'please specify a valid user'); + + $result = $this->ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareUserNoValidShareWith() { + $share = $this->getMock('\OC\Share20\IShare'); + $this->shareManager->method('newShare')->willReturn($share); + + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'valid-path'], + ['permissions', null, \OCP\Constants::PERMISSION_ALL], + ['shareType', $this->any(), \OCP\Share::SHARE_TYPE_USER], + ['shareWith', $this->any(), 'invalidUser'], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path = $this->getMock('\OCP\Files\File'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + + $expected = new \OC_OCS_Result(null, 404, 'please specify a valid user'); + + $result = $this->ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareUser() { + $share = $this->getMock('\OC\Share20\IShare'); + $this->shareManager->method('newShare')->willReturn($share); + + $ocs = $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS') + ->setConstructorArgs([ + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->request, + $this->rootFolder, + $this->urlGenerator, + $this->currentUser + ])->setMethods(['formatShare']) + ->getMock(); + + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'valid-path'], + ['permissions', null, \OCP\Constants::PERMISSION_ALL], + ['shareType', $this->any(), \OCP\Share::SHARE_TYPE_USER], + ['shareWith', null, 'validUser'], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path = $this->getMock('\OCP\Files\File'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + + $user = $this->getMock('\OCP\IUser'); + $this->userManager->method('userExists')->with('validUser')->willReturn(true); + $this->userManager->method('get')->with('validUser')->willReturn($user); + + $share->method('setPath')->with($path); + $share->method('setPermissions') + ->with( + \OCP\Constants::PERMISSION_ALL & + ~\OCP\Constants::PERMISSION_DELETE & + ~\OCP\Constants::PERMISSION_CREATE); + $share->method('setShareType')->with(\OCP\Share::SHARE_TYPE_USER); + $share->method('setSharedWith')->with($user); + $share->method('setSharedBy')->with($this->currentUser); + + $expected = new \OC_OCS_Result(); + $result = $ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareGroupNoValidShareWith() { + $share = $this->getMock('\OC\Share20\IShare'); + $this->shareManager->method('newShare')->willReturn($share); + + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'valid-path'], + ['permissions', null, \OCP\Constants::PERMISSION_ALL], + ['shareType', $this->any(), \OCP\Share::SHARE_TYPE_GROUP], + ['shareWith', $this->any(), 'invalidGroup'], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path = $this->getMock('\OCP\Files\File'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + + $expected = new \OC_OCS_Result(null, 404, 'please specify a valid user'); + + $result = $this->ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareGroup() { + $share = $this->getMock('\OC\Share20\IShare'); + $this->shareManager->method('newShare')->willReturn($share); + + $ocs = $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS') + ->setConstructorArgs([ + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->request, + $this->rootFolder, + $this->urlGenerator, + $this->currentUser + ])->setMethods(['formatShare']) + ->getMock(); + + $this->request + ->method('getParam') + ->will($this->returnValueMap([ + ['path', null, 'valid-path'], + ['permissions', null, \OCP\Constants::PERMISSION_ALL], + ['shareType', '-1', \OCP\Share::SHARE_TYPE_GROUP], + ['shareWith', null, 'validGroup'], + ])); + + $userFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path = $this->getMock('\OCP\Files\Folder'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + + $group = $this->getMock('\OCP\IGroup'); + $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true); + $this->groupManager->method('get')->with('validGroup')->willReturn($group); + + $share->method('setPath')->with($path); + $share->method('setPermissions')->with(\OCP\Constants::PERMISSION_ALL); + $share->method('setShareType')->with(\OCP\Share::SHARE_TYPE_GROUP); + $share->method('setSharedWith')->with($group); + $share->method('setSharedBy')->with($this->currentUser); + + $expected = new \OC_OCS_Result(); + $result = $ocs->createShare(); + + $this->assertEquals($expected->getMeta(), $result->getMeta()); + $this->assertEquals($expected->getData(), $result->getData()); + } } -- cgit v1.2.3 From 0ab227310f3519dc2805b80518ad91923bc4dc1f Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 5 Jan 2016 11:00:09 +0100 Subject: [Sharing 2.0] General exceptions return 403 This is the same as the old behaviour --- apps/files_sharing/api/share20ocs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apps') diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index bf644ce00f6..8bab8a871e3 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -304,7 +304,7 @@ class Share20OCS { try { $share = $this->shareManager->createShare($share); } catch (\Exception $e) { - return new \OC_OCS_Result(null, 404, $e->getMessage()); + return new \OC_OCS_Result(null, 403, $e->getMessage()); } $share = $this->formatShare($share); -- cgit v1.2.3 From 527b434cd270b46bd47559760023385ba83024ba Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 5 Jan 2016 11:42:54 +0100 Subject: [Sharing 2.0] Do not use static function to get numeric storage id --- apps/files_sharing/api/share20ocs.php | 2 +- apps/files_sharing/tests/api/share20ocstest.php | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'apps') diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index 8bab8a871e3..69e2f570499 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -97,7 +97,7 @@ class Share20OCS { $result['item_type'] = 'file'; } $result['storage_id'] = $path->getStorage()->getId(); - $result['storage'] = \OC\Files\Cache\Storage::getNumericStorageId($path->getStorage()->getId()); + $result['storage'] = $path->getStorage()->getCache()->getNumericStorageId(); $result['item_source'] = $path->getId(); $result['file_source'] = $path->getId(); $result['file_parent'] = $path->getParent()->getId(); diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php index 2d69636541e..74a5d0752a4 100644 --- a/apps/files_sharing/tests/api/share20ocstest.php +++ b/apps/files_sharing/tests/api/share20ocstest.php @@ -172,8 +172,18 @@ class Share20OCSTest extends \Test\TestCase { $group = $this->getMock('OCP\IGroup'); $group->method('getGID')->willReturn('groupId'); - $storage = $this->getMock('OCP\Files\Storage'); + $cache = $this->getMockBuilder('OC\Files\Cache\Cache') + ->disableOriginalConstructor() + ->getMock(); + $cache->method('getNumericStorageId')->willReturn(101); + + $storage = $this->getMockBuilder('OC\Files\Storage\Storage') + ->disableOriginalConstructor() + ->getMock(); $storage->method('getId')->willReturn('STORAGE'); + $storage->method('getCache')->willReturn($cache); + + $parentFolder = $this->getMock('OCP\Files\Folder'); $parentFolder->method('getId')->willReturn(3); @@ -224,7 +234,7 @@ class Share20OCSTest extends \Test\TestCase { 'parent' => 6, 'storage_id' => 'STORAGE', 'path' => 'file', - 'storage' => null, // HACK around static function + 'storage' => 101, 'mail_send' => 0, ]; $data[] = [$share, $expected]; @@ -263,7 +273,7 @@ class Share20OCSTest extends \Test\TestCase { 'parent' => 6, 'storage_id' => 'STORAGE', 'path' => 'folder', - 'storage' => null, // HACK around static function + 'storage' => 101, 'mail_send' => 0, ]; $data[] = [$share, $expected]; @@ -305,7 +315,7 @@ class Share20OCSTest extends \Test\TestCase { 'parent' => 6, 'storage_id' => 'STORAGE', 'path' => 'folder', - 'storage' => null, // HACK around static function + 'storage' => 101, 'mail_send' => 0, 'url' => 'url', ]; -- cgit v1.2.3 From 26280e1f1965cd50325a05715989b1c0f7e508d0 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 5 Jan 2016 12:50:00 +0100 Subject: [Sharing 2.0] Add L10N instance to manager for translated errors --- apps/files_sharing/api/ocssharewrapper.php | 3 +- apps/files_sharing/api/share20ocs.php | 11 +++--- lib/private/share20/manager.php | 25 ++++++++++---- tests/lib/share20/managertest.php | 54 +++++++++++++++++++++--------- 4 files changed, 65 insertions(+), 28 deletions(-) (limited to 'apps') diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php index 2896f3fbf01..4640f4ea185 100644 --- a/apps/files_sharing/api/ocssharewrapper.php +++ b/apps/files_sharing/api/ocssharewrapper.php @@ -39,7 +39,8 @@ class OCSShareWrapper { \OC::$server->getSecureRandom(), \OC::$server->getHasher(), \OC::$server->getMountManager(), - \OC::$server->getGroupManager() + \OC::$server->getGroupManager(), + \OC::$server->getL10N('core') ), \OC::$server->getGroupManager(), \OC::$server->getUserManager(), diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index 69e2f570499..003c028bf97 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -268,9 +268,9 @@ class Share20OCS { } $share->setPermissions( - \OCP\Constants::PERMISSION_READ | - \OCP\Constants::PERMISSION_CREATE | - \OCP\Constants::PERMISSION_UPDATE + \OCP\Constants::PERMISSION_READ | + \OCP\Constants::PERMISSION_CREATE | + \OCP\Constants::PERMISSION_UPDATE ); } else { $share->setPermissions(\OCP\Constants::PERMISSION_READ); @@ -303,7 +303,10 @@ class Share20OCS { try { $share = $this->shareManager->createShare($share); - } catch (\Exception $e) { + } catch (\OC\HintException $e) { + $code = $e->getCode() === 0 ? 403 : $e->getCode(); + return new \OC_OCS_Result(null, $code, $e->getHint()); + }catch (\Exception $e) { return new \OC_OCS_Result(null, 403, $e->getMessage()); } diff --git a/lib/private/share20/manager.php b/lib/private/share20/manager.php index 46bbd0ef4cf..216c4b9f5c1 100644 --- a/lib/private/share20/manager.php +++ b/lib/private/share20/manager.php @@ -22,11 +22,15 @@ namespace OC\Share20; use OCP\IConfig; +use OCP\IL10N; use OCP\ILogger; use OCP\Security\ISecureRandom; use OCP\Security\IHasher; use OCP\Files\Mount\IMountManager; use OCP\IGroupManager; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\IUser; use OC\Share20\Exception\ShareNotFound; @@ -35,9 +39,7 @@ use OC\Share20\Exception\ShareNotFound; */ class Manager { - /** - * @var IShareProvider[] - */ + /** @var IShareProvider[] */ private $defaultProvider; /** @var ILogger */ @@ -58,6 +60,9 @@ class Manager { /** @var IGroupManager */ private $groupManager; + /** @var IL10N */ + private $l; + /** * Manager constructor. * @@ -68,6 +73,7 @@ class Manager { * @param IHasher $hasher * @param IMountManager $mountManager * @param IGroupManager $groupManager + * @param IL10N $l */ public function __construct( ILogger $logger, @@ -76,7 +82,8 @@ class Manager { ISecureRandom $secureRandom, IHasher $hasher, IMountManager $mountManager, - IGroupManager $groupManager + IGroupManager $groupManager, + IL10N $l ) { $this->logger = $logger; $this->config = $config; @@ -84,6 +91,7 @@ class Manager { $this->hasher = $hasher; $this->mountManager = $mountManager; $this->groupManager = $groupManager; + $this->l = $l; // TEMP SOLUTION JUST TO GET STARTED $this->defaultProvider = $defaultProvider; @@ -191,6 +199,7 @@ class Manager { * * @param \DateTime $expireDate The current expiration date (can be null) * @return \DateTime|null The expiration date or null if $expireDate was null and it is not required + * @throws \OC\HintException */ protected function validateExpiredate($expireDate) { @@ -201,7 +210,8 @@ class Manager { $date = new \DateTime(); $date->setTime(0, 0, 0); if ($date >= $expireDate) { - throw new \InvalidArgumentException('Expiration date is in the past'); + $message = $this->l->t('Expiration date is in the past'); + throw new \OC\HintException($message, $message, 404); } } @@ -215,7 +225,8 @@ class Manager { $date->setTime(0, 0, 0); $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); if ($date < $expireDate) { - throw new \InvalidArgumentException('Cannot set expiration date more than ' . $this->shareApiLinkDefaultExpireDays() . ' in the future'); + $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); + throw new \OC\HintException($message, $message, 404); } return $expireDate; @@ -390,7 +401,7 @@ class Manager { * For now ignore a set token. */ $share->setToken( - $this->secureRandom->getMediumStrengthGenerator()->generate( + $this->secureRandom->generate( \OC\Share\Constants::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER. \OCP\Security\ISecureRandom::CHAR_UPPER. diff --git a/tests/lib/share20/managertest.php b/tests/lib/share20/managertest.php index 1a0c3285e52..c7f4c0afdf2 100644 --- a/tests/lib/share20/managertest.php +++ b/tests/lib/share20/managertest.php @@ -23,6 +23,7 @@ namespace Test\Share20; use OC\Share20\Manager; use OC\Share20\Exception; +use OCP\IL10N; use OCP\ILogger; use OCP\IConfig; use OC\Share20\IShareProvider; @@ -63,6 +64,9 @@ class ManagerTest extends \Test\TestCase { /** @var IGroupManager */ protected $groupManager; + /** @var IL10N */ + protected $l; + public function setUp() { $this->logger = $this->getMock('\OCP\ILogger'); @@ -73,6 +77,12 @@ class ManagerTest extends \Test\TestCase { $this->mountManager = $this->getMock('\OCP\Files\Mount\IMountManager'); $this->groupManager = $this->getMock('\OCP\IGroupManager'); + $this->l = $this->getMock('\OCP\IL10N'); + $this->l->method('t') + ->will($this->returnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + })); + $this->manager = new Manager( $this->logger, $this->config, @@ -80,7 +90,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l ); } @@ -126,7 +137,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l ]) ->setMethods(['getShareById', 'deleteChildren']) ->getMock(); @@ -216,7 +228,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods(['getShareById']) ->getMock(); @@ -360,7 +373,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods(['deleteShare']) ->getMock(); @@ -569,7 +583,7 @@ class ManagerTest extends \Test\TestCase { } /** - * @expectedException InvalidArgumentException + * @expectedException \OC\HintException * @expectedExceptionMessage Expiration date is in the past */ public function testValidateExpiredateInPast() { @@ -594,10 +608,6 @@ class ManagerTest extends \Test\TestCase { $this->invokePrivate($this->manager, 'validateExpiredate', [null]); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Cannot set expiration date more than 3 in the future - */ public function testValidateExpiredateEnforceToFarIntoFuture() { // Expire date in the past $future = new \DateTime(); @@ -609,7 +619,13 @@ class ManagerTest extends \Test\TestCase { ['core', 'shareapi_expire_after_n_days', '7', '3'], ])); - $this->invokePrivate($this->manager, 'validateExpiredate', [$future]); + try { + $this->invokePrivate($this->manager, 'validateExpiredate', [$future]); + } catch (\OC\HintException $e) { + $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getMessage()); + $this->assertEquals('Cannot set expiration date more than 3 days in the future', $e->getHint()); + $this->assertEquals(404, $e->getCode()); + } } public function testValidateExpiredateEnforceValid() { @@ -1139,7 +1155,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods(['isSharingDisabledForUser']) ->getMock(); @@ -1167,7 +1184,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods(['canShare']) ->getMock(); @@ -1186,7 +1204,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) ->getMock(); @@ -1247,7 +1266,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks']) ->getMock(); @@ -1308,7 +1328,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods([ 'canShare', @@ -1448,7 +1469,8 @@ class ManagerTest extends \Test\TestCase { $this->secureRandom, $this->hasher, $this->mountManager, - $this->groupManager + $this->groupManager, + $this->l, ]) ->setMethods([ 'canShare', -- cgit v1.2.3