/** * @param int $type The share type * @param string $path The path to share relative to $initiators root * @param string $initiator * @param string $recipient * @param int $permissions * @return \OCP\Share\IShare */ protected function share($type, $path, $initiator, $recipient, $permissions) { $userFolder = $this->rootFolder->getUserFolder($initiator); $node = $userFolder->get($path); $share = $this->shareManager->newShare(); $share->setShareType($type)->setSharedWith($recipient)->setSharedBy($initiator)->setNode($node)->setPermissions($permissions); $share = $this->shareManager->createShare($share); return $share; }
public function testGetSharedByWithLimit() { $node = $this->getMock('\\OCP\\Files\\File'); $node->method('getId')->willReturn(42); $node->method('getName')->willReturn('myFile'); $this->tokenHandler->method('generateToken')->willReturn('token'); $this->notifications->method('sendRemoteShare')->willReturn(true); $this->rootFolder->expects($this->never())->method($this->anything()); $share = $this->shareManager->newShare(); $share->setSharedWith('*****@*****.**')->setSharedBy('sharedBy')->setShareOwner('shareOwner')->setPermissions(19)->setNode($node); $this->provider->create($share); $share2 = $this->shareManager->newShare(); $share2->setSharedWith('*****@*****.**')->setSharedBy('sharedBy')->setShareOwner('shareOwner')->setPermissions(19)->setNode($node); $this->provider->create($share2); $shares = $this->provider->getSharesBy('shareOwner', \OCP\Share::SHARE_TYPE_REMOTE, null, true, 1, 1); $this->assertCount(1, $shares); $this->assertEquals('*****@*****.**', $shares[0]->getSharedWith()); }
/** * Build super shares (virtual share) by grouping them by node id and target, * then for each group compute the super share and return it along with the matching * grouped shares. The most permissive permissions are used based on the permissions * of all shares within the group. * * @param \OCP\Share\IShare[] $allShares * @return array Tuple of [superShare, groupedShares] */ private function buildSuperShares(array $allShares) { $result = []; $groupedShares = $this->groupShares($allShares); /** @var \OCP\Share\IShare[] $shares */ foreach ($groupedShares as $shares) { if (count($shares) === 0) { continue; } $superShare = $this->shareManager->newShare(); // compute super share based on first entry of the group $superShare->setId($shares[0]->getId())->setShareOwner($shares[0]->getShareOwner())->setNodeId($shares[0]->getNodeId())->setTarget($shares[0]->getTarget()); // use most permissive permissions $permissions = 0; foreach ($shares as $share) { $permissions |= $share->getPermissions(); } $superShare->setPermissions($permissions); $result[] = [$superShare, $shares]; } return $result; }
public function testGetPathByIdShareSubFolder() { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); \OC\Files\Filesystem::mkdir('foo'); \OC\Files\Filesystem::mkdir('foo/bar'); \OC\Files\Filesystem::touch('foo/bar/test.txt'); $folderInfo = \OC\Files\Filesystem::getFileInfo('foo'); $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt'); $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $rootFolder->get('foo'); $share = $this->shareManager->newShare(); $share->setNode($node)->setShareType(\OCP\Share::SHARE_TYPE_USER)->setSharedWith(self::TEST_FILES_SHARING_API_USER2)->setSharedBy(self::TEST_FILES_SHARING_API_USER1)->setPermissions(\OCP\Constants::PERMISSION_ALL); $this->shareManager->createShare($share); \OC_Util::tearDownFS(); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo')); list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo'); /** * @var \OC\Files\Storage\Shared $sharedStorage */ $sharedCache = $sharedStorage->getCache(); $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId())); $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId())); }
/** * @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->setNode($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; } /* * Hack for https://github.com/owncloud/core/issues/22587 * We check the permissions via webdav. But the permissions of the mount point * do not equal the share permissions. Here we fix that for federated mounts. */ if ($path->getStorage()->instanceOfStorage('OCA\\Files_Sharing\\External\\Storage')) { $permissions &= ~($permissions & ~$path->getPermissions()); } $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($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($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'); } /* * For now we only allow 1 link share. * Return the existing link share if this is a duplicate */ $existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0); if (!empty($existingShares)) { return new \OC_OCS_Result($this->formatShare($existingShares[0])); } $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 $password = $this->request->getParam('password', ''); if ($password !== '') { $share->setPassword($password); } //Expire date $expireDate = $this->request->getParam('expireDate', ''); if ($expireDate !== '') { 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) { if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { return new \OC_OCS_Result(null, 403, 'Sharing ' . $path->getPath() . ' failed, because the backend does not allow shares from type ' . $shareType); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else { return new \OC_OCS_Result(null, 400, "unknown share type"); } } } } $share->setShareType($shareType); $share->setSharedBy($this->currentUser->getUID()); try { $share = $this->shareManager->createShare($share); } catch (GenericShareException $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()); } $share = $this->formatShare($share); return new \OC_OCS_Result($share); }
/** * @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->setNode($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($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($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 $password = $this->request->getParam('password', ''); if ($password !== '') { $share->setPassword($password); } //Expire date $expireDate = $this->request->getParam('expireDate', ''); if ($expireDate !== '') { 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->getUID()); try { $share = $this->shareManager->createShare($share); } catch (GenericShareException $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()); } $share = $this->formatShare($share); return new \OC_OCS_Result($share); }