public function testCheckPasswordValidPassword() { $share = $this->getMock('\\OC\\Share20\\IShare'); $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); $share->method('getPassword')->willReturn('passwordHash'); $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true); $this->assertTrue($this->manager->checkPassword($share, 'password')); }
public function testMoveShareGroup() { $share = $this->manager->newShare(); $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP); $sharedWith = $this->getMock('\\OCP\\IGroup'); $share->setSharedWith($sharedWith); $recipient = $this->getMock('\\OCP\\IUser'); $sharedWith->method('inGroup')->with($recipient)->willReturn(true); $this->defaultProvider->method('move')->with($share, $recipient)->will($this->returnArgument(0)); $this->manager->moveShare($share, $recipient); }
/** * @dataProvider dataGetShareById */ public function testGetShareById($currentUserIs) { $otherUser1 = $this->getMock('\\OCP\\IUser'); $otherUser2 = $this->getMock('\\OCP\\IUser'); $otherUser3 = $this->getMock('\\OCP\\IUser'); $share = $this->getMock('\\OC\\Share20\\IShare'); $share->method('getSharedWith')->with()->willReturn($currentUserIs === 'getSharedWith' ? $this->user : $otherUser1); $share->method('getSharedBy')->with()->willReturn($currentUserIs === 'getSharedBy' ? $this->user : $otherUser2); $share->method('getShareOwner')->with()->willReturn($currentUserIs === 'getShareOwner' ? $this->user : $otherUser3); $this->defaultProvider->expects($this->once())->method('getShareById')->with(42)->willReturn($share); $this->assertEquals($share, $this->manager->getShareById(42)); }
/** * @dataProvider dataGetShare */ public function testGetShare(\OC\Share20\IShare $share, array $result) { $ocs = $this->getMockBuilder('OCA\\Files_Sharing\\API\\Share20OCS')->setConstructorArgs([$this->shareManager, $this->groupManager, $this->userManager, $this->request, $this->rootFolder, $this->urlGenerator, $this->currentUser])->setMethods(['canAccessShare'])->getMock(); $ocs->method('canAccessShare')->willReturn(true); $this->shareManager->expects($this->once())->method('getShareById')->with($share->getId())->willReturn($share); $userFolder = $this->getMock('OCP\\Files\\Folder'); $userFolder->method('getRelativePath')->will($this->returnArgument(0)); $this->rootFolder->method('getUserFolder')->with($share->getShareOwner()->getUID())->willReturn($userFolder); $this->urlGenerator->method('linkToRouteAbsolute')->willReturn('url'); $expected = new \OC_OCS_Result($result); $this->assertEquals($expected->getData(), $ocs->getShare($share->getId())->getData()); }
/** * @param int $id * @return \OC_OCS_Result */ public function updateShare($id) { // Try both our default and our federated provider $share = null; try { $share = $this->shareManager->getShareById('ocinternal:' . $id); } catch (\OC\Share20\Exception\ShareNotFound $e) { //Ignore for now //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); } // Could not find the share as internal share... maybe it is a federated share if ($share === null) { return \OCA\Files_Sharing\API\Local::updateShare(['id' => $id]); } if (!$this->canAccessShare($share)) { return new \OC_OCS_Result(null, 404, "wrong share Id, share doesn't exist."); } $permissions = $this->request->getParam('permissions', null); $password = $this->request->getParam('password', null); $publicUpload = $this->request->getParam('publicUpload', null); $expireDate = $this->request->getParam('expireDate', null); if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) { return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); } if ($expireDate !== null) { try { $expireDate = $this->parseDate($expireDate); } catch (\Exception $e) { return new \OC_OCS_Result(null, 400, $e->getMessage()); } $share->setExpirationDate($expireDate); } if ($permissions !== null) { $permissions = (int) $permissions; $share->setPermissions($permissions); } if ($password !== null) { $share->setPassword($password); } if ($publicUpload === 'true') { $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); } else { if ($publicUpload === 'false') { $share->setPermissions(\OCP\Constants::PERMISSION_READ); } } try { $share = $this->shareManager->updateShare($share); } catch (\Exception $e) { return new \OC_OCS_Result(null, 400, $e->getMessage()); } return new \OC_OCS_Result($this->formatShare($share)); }
/** * @dataProvider dataIsSharingDisabledForUser * * @param string $excludeGroups * @param string $groupList * @param string $setList * @param string[] $groupIds * @param bool $expected */ public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) { $user = $this->getMock('\\OCP\\IUser'); $this->config->method('getAppValue')->will($this->returnValueMap([['core', 'shareapi_exclude_groups', 'no', $excludeGroups], ['core', 'shareapi_exclude_groups_list', '', $groupList]])); if ($setList !== null) { $this->config->expects($this->once())->method('setAppValue')->with('core', 'shareapi_exclude_groups_list', $setList); } else { $this->config->expects($this->never())->method('setAppValue'); } $this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groupIds); $res = $this->manager->isSharingDisabledForUser($user); $this->assertEquals($expected, $res); }
public function testMoveShareGroup() { $share = $this->manager->newShare(); $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP)->setId('42')->setProviderId('foo'); $group = $this->getMock('\\OCP\\IGroup'); $share->setSharedWith('group'); $recipient = $this->getMock('\\OCP\\IUser'); $group->method('inGroup')->with($recipient)->willReturn(true); $this->groupManager->method('get')->with('group')->willReturn($group); $this->userManager->method('get')->with('recipient')->willReturn($recipient); $this->defaultProvider->method('move')->with($share, 'recipient')->will($this->returnArgument(0)); $this->manager->moveShare($share, 'recipient'); }
/** * Delete a share * * @param string $id * @return \OC_OCS_Result */ public function deleteShare($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.'); } /* * FIXME * User the old code path for remote shares until we have our remoteshareprovider */ if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { \OCA\Files_Sharing\API\Local::deleteShare(['id' => $id]); } try { $this->shareManager->deleteShare($share); } catch (\OC\Share20\Exception\BackendError $e) { return new \OC_OCS_Result(null, 404, 'could not delete share'); } return new \OC_OCS_Result(); }
public function testUpdateShareLink() { $manager = $this->createManagerMock()->setMethods(['canShare', 'getShareById', 'generalCreateChecks', 'linkCreateChecks', 'pathCreateChecks', 'verifyPassword', 'validateExpirationDate'])->getMock(); $user = $this->getMock('\\OCP\\IUser'); $user->method('getUID')->willReturn('owner'); $originalShare = new \OC\Share20\Share(); $originalShare->setShareType(\OCP\Share::SHARE_TYPE_LINK); $tomorrow = new \DateTime(); $tomorrow->setTime(0, 0, 0); $tomorrow->add(new \DateInterval('P1D')); $file = $this->getMock('OCP\\Files\\File', [], [], 'File'); $file->method('getId')->willReturn(100); $share = $this->manager->newShare(); $share->setProviderId('foo')->setId('42')->setShareType(\OCP\Share::SHARE_TYPE_LINK)->setSharedBy($user)->setShareOwner($user)->setPassword('password')->setExpirationDate($tomorrow)->setNode($file); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); $manager->expects($this->once())->method('validateExpirationDate')->with($share); $this->defaultProvider->expects($this->once())->method('update')->with($share)->willReturn($share); $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); \OCP\Util::connectHook('OCP\\Share', 'post_set_expiration_date', $hookListner, 'post'); $hookListner->expects($this->once())->method('post')->with(['itemType' => 'file', 'itemSource' => 100, 'date' => $tomorrow, 'uidOwner' => 'owner']); $manager->updateShare($share); }
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()); }
/** * The getShares function. * * - Get shares by the current user * - Get shares by the current user and reshares (?reshares=true) * - Get shares with the current user (?shared_with_me=true) * - Get shares for a specific path (?path=...) * - Get all shares in a folder (?subfiles=true&path=..) * * @return \OC_OCS_Result */ public function getShares() { $sharedWithMe = $this->request->getParam('shared_with_me', null); $reshares = $this->request->getParam('reshares', null); $subfiles = $this->request->getParam('subfiles'); $path = $this->request->getParam('path', null); if ($sharedWithMe === 'true') { return $this->getSharedWithMe(); } if ($path !== null) { $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'); } } if ($subfiles === 'true') { return $this->getSharesInDir($path); } if ($reshares === 'true') { $reshares = true; } else { $reshares = false; } // Get all shares $userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); $groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); $linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); //TODO: Add federated shares $shares = array_merge($userShares, $groupShares, $linkShares); $formatted = []; foreach ($shares as $share) { $formatted[] = $this->formatShare($share); } return new \OC_OCS_Result($formatted); }
/** * @PublicPage * @NoCSRFRequired * * @param string $token * @param string $files * @param string $path * @param string $downloadStartSecret * @return void|RedirectResponse */ public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { \OC_User::setIncognitoMode(true); $share = $this->shareManager->getShareByToken($token); // Share is password protected - check whether the user is permitted to access the share if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', ['token' => $token])); } $files_list = null; if (!is_null($files)) { // download selected files $files_list = json_decode($files); // in case we get only a single file if ($files_list === null) { $files_list = [$files]; } } $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()->getUID()); $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); // Single file share if ($share->getNode() instanceof \OCP\Files\File) { // Single file download $event = $this->activityManager->generateEvent(); $event->setApp('files_sharing')->setType(Activity::TYPE_PUBLIC_LINKS)->setSubject(Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED, [$userFolder->getRelativePath($share->getNode()->getPath())])->setAffectedUser($share->getShareOwner()->getUID())->setObject('files', $share->getNode()->getId(), $userFolder->getRelativePath($share->getNode()->getPath())); $this->activityManager->publish($event); } else { /** @var \OCP\Files\Folder $node */ $node = $share->getNode(); // Try to get the path if ($path !== '') { try { $node = $node->get($path); } catch (NotFoundException $e) { return new NotFoundResponse(); } } $originalSharePath = $userFolder->getRelativePath($node->getPath()); if ($node instanceof \OCP\Files\File) { // Single file download $event = $this->activityManager->generateEvent(); $event->setApp('files_sharing')->setType(Activity::TYPE_PUBLIC_LINKS)->setSubject(Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED, [$userFolder->getRelativePath($node->getPath())])->setAffectedUser($share->getShareOwner()->getUID())->setObject('files', $node->getId(), $userFolder->getRelativePath($node->getPath())); $this->activityManager->publish($event); } else { if (!empty($files_list)) { /** @var \OCP\Files\Folder $node */ // Subset of files is downloaded foreach ($files_list as $file) { $subNode = $node->get($file); $event = $this->activityManager->generateEvent(); $event->setApp('files_sharing')->setType(Activity::TYPE_PUBLIC_LINKS)->setAffectedUser($share->getShareOwner()->getUID())->setObject('files', $subNode->getId(), $userFolder->getRelativePath($subNode->getPath())); if ($subNode instanceof \OCP\Files\File) { $event->setSubject(Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED, [$userFolder->getRelativePath($subNode->getPath())]); } else { $event->setSubject(Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED, [$userFolder->getRelativePath($subNode->getPath())]); } $this->activityManager->publish($event); } } else { // The folder is downloaded $event = $this->activityManager->generateEvent(); $event->setApp('files_sharing')->setType(Activity::TYPE_PUBLIC_LINKS)->setSubject(Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED, [$userFolder->getRelativePath($node->getPath())])->setAffectedUser($share->getShareOwner()->getUID())->setObject('files', $node->getId(), $userFolder->getRelativePath($node->getPath())); $this->activityManager->publish($event); } } } /* FIXME: We should do this all nicely in OCP */ OC_Util::tearDownFS(); OC_Util::setupFS($share->getShareOwner()->getUID()); /** * this sets a cookie to be able to recognize the start of the download * the content must not be longer than 32 characters and must only contain * alphanumeric characters */ if (!empty($downloadStartSecret) && !isset($downloadStartSecret[32]) && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { // FIXME: set on the response once we use an actual app framework response setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); } // download selected files if (!is_null($files)) { // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well // after dispatching the request which results in a "Cannot modify header information" notice. OC_Files::get($originalSharePath, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD'); exit; } else { // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well // after dispatching the request which results in a "Cannot modify header information" notice. OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $_SERVER['REQUEST_METHOD'] == 'HEAD'); exit; } }
/** * @param int $id * @return \OC_OCS_Result */ public function updateShare($id) { // Try both our default and our federated provider $share = null; try { $share = $this->shareManager->getShareById('ocinternal:' . $id); } catch (ShareNotFound $e) { //Ignore for now //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); } // Could not find the share as internal share... maybe it is a federated share if ($share === null) { return \OCA\Files_Sharing\API\Local::updateShare(['id' => $id]); } if (!$this->canAccessShare($share)) { return new \OC_OCS_Result(null, 404, 'wrong share Id, share doesn\'t exist.'); } $permissions = $this->request->getParam('permissions', null); $password = $this->request->getParam('password', null); $publicUpload = $this->request->getParam('publicUpload', null); $expireDate = $this->request->getParam('expireDate', null); /* * expirationdate, password and publicUpload only make sense for link shares */ if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) { return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); } $newPermissions = null; if ($publicUpload === 'true') { $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE; } else { if ($publicUpload === 'false') { $newPermissions = \OCP\Constants::PERMISSION_READ; } } if ($permissions !== null) { $newPermissions = (int) $permissions; } if ($newPermissions !== null && $newPermissions !== \OCP\Constants::PERMISSION_READ && $newPermissions !== (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { return new \OC_OCS_Result(null, 400, 'can\'t change permission for public link share'); } if ($newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); } if (!$share->getNode() instanceof \OCP\Files\Folder) { return new \OC_OCS_Result(null, 400, "public upload is only possible for public shared folders"); } } if ($newPermissions !== null) { $share->setPermissions($newPermissions); } if ($expireDate === '') { $share->setExpirationDate(null); } else { if ($expireDate !== null) { try { $expireDate = $this->parseDate($expireDate); } catch (\Exception $e) { return new \OC_OCS_Result(null, 400, $e->getMessage()); } $share->setExpirationDate($expireDate); } } if ($password === '') { $share->setPassword(null); } else { if ($password !== null) { $share->setPassword($password); } } } else { // For other shares only permissions is valid. if ($permissions === null) { return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); } else { $permissions = (int) $permissions; $share->setPermissions($permissions); } } try { $share = $this->shareManager->updateShare($share); } catch (\Exception $e) { return new \OC_OCS_Result(null, 400, $e->getMessage()); } return new \OC_OCS_Result($this->formatShare($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->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 (\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()); } $share = $this->formatShare($share); return new \OC_OCS_Result($share); }
public function testGetShareById() { $share = $this->getMock('\\OC\\Share20\\IShare'); $this->defaultProvider->expects($this->once())->method('getShareById')->with(42)->willReturn($share); $this->assertEquals($share, $this->manager->getShareById(42)); }