Beispiel #1
0
 /**
  * @dataProvider dataCanShare
  *
  * @param bool $expected
  * @param string $sharingEnabled
  * @param bool $disabledForUser
  */
 public function testCanShare($expected, $sharingEnabled, $disabledForUser)
 {
     $this->config->method('getAppValue')->will($this->returnValueMap([['core', 'shareapi_enabled', 'yes', $sharingEnabled]]));
     $manager = $this->createManagerMock()->setMethods(['sharingDisabledForUser'])->getMock();
     $manager->method('sharingDisabledForUser')->willReturn($disabledForUser);
     $user = $this->getMock('\\OCP\\IUser');
     $share = $this->manager->newShare();
     $share->setSharedBy($user);
     $res = $this->invokePrivate($manager, 'canShare', [$share]);
     $this->assertEquals($expected, $res);
 }
Beispiel #2
0
 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);
 }
Beispiel #3
0
 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');
 }
 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);
 }
Beispiel #5
0
 /**
  * @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);
 }