use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Files\IRootFolder;
+use OCP\Share\IManager;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\Exceptions\GenericShareException;
class Share20OCS {
- /** @var \OC\Share20\Manager */
+ /** @var IManager */
private $shareManager;
-
/** @var IGroupManager */
private $groupManager;
-
/** @var IUserManager */
private $userManager;
-
/** @var IRequest */
private $request;
-
/** @var IRootFolder */
private $rootFolder;
-
/** @var IUrlGenerator */
private $urlGenerator;
-
/** @var IUser */
private $currentUser;
+ /**
+ * Share20OCS constructor.
+ *
+ * @param IManager $shareManager
+ * @param IGroupManager $groupManager
+ * @param IUserManager $userManager
+ * @param IRequest $request
+ * @param IRootFolder $rootFolder
+ * @param IURLGenerator $urlGenerator
+ * @param IUser $currentUser
+ */
public function __construct(
- \OC\Share20\Manager $shareManager,
+ IManager $shareManager,
IGroupManager $groupManager,
IUserManager $userManager,
IRequest $request,
* @return array
*/
protected function formatShare(\OCP\Share\IShare $share) {
+ $sharedBy = $this->userManager->get($share->getSharedBy());
+ $shareOwner = $this->userManager->get($share->getShareOwner());
$result = [
'id' => $share->getId(),
'share_type' => $share->getShareType(),
- 'uid_owner' => $share->getSharedBy()->getUID(),
- 'displayname_owner' => $share->getSharedBy()->getDisplayName(),
+ 'uid_owner' => $share->getSharedBy(),
+ 'displayname_owner' => $sharedBy->getDisplayName(),
'permissions' => $share->getPermissions(),
'stime' => $share->getShareTime()->getTimestamp(),
'parent' => null,
'expiration' => null,
'token' => null,
- 'uid_file_owner' => $share->getShareOwner()->getUID(),
- 'displayname_file_owner' => $share->getShareOwner()->getDisplayName(),
+ 'uid_file_owner' => $share->getShareOwner(),
+ 'displayname_file_owner' => $shareOwner->getDisplayName(),
];
$node = $share->getNode();
- $result['path'] = $this->rootFolder->getUserFolder($share->getShareOwner()->getUID())->getRelativePath($node->getPath());
+ $result['path'] = $this->rootFolder->getUserFolder($share->getShareOwner())->getRelativePath($node->getPath());
if ($node instanceOf \OCP\Files\Folder) {
$result['item_type'] = 'folder';
} else {
$result['file_target'] = $share->getTarget();
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
- $sharedWith = $share->getSharedWith();
+ $sharedWith = $this->userManager->get($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();
+ $result['share_with'] = $share->getSharedWith();
+ $result['share_with_displayname'] = $share->getSharedWith();
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
$result['share_with'] = $share->getPassword();
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->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($this->groupManager->get($shareWith));
+ $share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
//Can we even share links?
}
$share->setShareType($shareType);
- $share->setSharedBy($this->currentUser);
+ $share->setSharedBy($this->currentUser->getUID());
try {
$share = $this->shareManager->createShare($share);
* @return \OC_OCS_Result
*/
private function getSharedWithMe($node = null) {
- $userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
- $groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
+ $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
+ $groupShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
$shares = array_merge($userShares, $groupShares);
/** @var \OCP\Share\IShare[] $shares */
$shares = [];
foreach ($nodes as $node) {
- $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
- $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
- $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
+ $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
+ $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
+ $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
//TODO: Add federated shares
}
}
// 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);
+ $userShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
+ $groupShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
+ $linkShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
//TODO: Add federated shares
$shares = array_merge($userShares, $groupShares, $linkShares);
return new \OC_OCS_Result($this->formatShare($share));
}
- public function validatePermissions($permissions) {
- if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
- return false;
- }
-
-
- }
-
/**
* @param \OCP\Share\IShare $share
* @return bool
}
// Owner of the file and the sharer of the file can always get share
- if ($share->getShareOwner() === $this->currentUser ||
- $share->getSharedBy() === $this->currentUser
+ if ($share->getShareOwner() === $this->currentUser->getUID() ||
+ $share->getSharedBy() === $this->currentUser->getUID()
) {
return true;
}
// If the share is shared with you (or a group you are a member of)
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
- $share->getSharedWith() === $this->currentUser) {
+ $share->getSharedWith() === $this->currentUser->getUID()) {
return true;
}
- if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
- $share->getSharedWith()->inGroup($this->currentUser)) {
- return true;
+ if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
+ $sharedWith = $this->groupManager->get($share->getSharedWith());
+ if ($sharedWith->inGroup($this->currentUser)) {
+ return true;
+ }
}
return false;
private $ocs;
protected function setUp() {
- $this->shareManager = $this->getMockBuilder('OC\Share20\Manager')
+ $this->shareManager = $this->getMockBuilder('OCP\Share\IManager')
->disableOriginalConstructor()
->getMock();
$this->groupManager = $this->getMock('OCP\IGroupManager');
->getMock();
}
+ private function newShare() {
+ return \OC::$server->getShareManager()->newShare();
+ }
+
public function testDeleteShareShareNotFound() {
$this->shareManager
->expects($this->once())
}
public function testDeleteShare() {
- $share = $this->getMock('OCP\Share\IShare');
- $share->method('getSharedBy')->willReturn($this->currentUser);
+ $share = $this->newShare();
+ $share->setSharedBy($this->currentUser->getUID());
$this->shareManager
->expects($this->once())
->method('getShareById')
public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions,
$shareTime, $expiration, $parent, $target, $mail_send, $token=null,
$password=null) {
- $share = $this->getMock('OCP\Share\IShare');
+ $share = $this->getMock('\OCP\Share\IShare');
$share->method('getId')->willReturn($id);
$share->method('getShareType')->willReturn($shareType);
$share->method('getSharedWith')->willReturn($sharedWith);
public function dataGetShare() {
$data = [];
- $initiator = $this->getMock('OCP\IUser');
- $initiator->method('getUID')->willReturn('initiatorId');
- $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
-
- $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');
-
$cache = $this->getMockBuilder('OC\Files\Cache\Cache')
->disableOriginalConstructor()
->getMock();
$share = $this->createShare(
100,
\OCP\Share::SHARE_TYPE_USER,
- $user,
- $initiator,
- $owner,
+ 'userId',
+ 'initiatorId',
+ 'ownerId',
$file,
4,
5,
$share = $this->createShare(
101,
\OCP\Share::SHARE_TYPE_GROUP,
- $group,
- $initiator,
- $owner,
+ 'groupId',
+ 'initiatorId',
+ 'ownerId',
$folder,
4,
5,
101,
\OCP\Share::SHARE_TYPE_LINK,
null,
- $initiator,
- $owner,
+ 'initiatorId',
+ 'ownerId',
$folder,
4,
5,
->will($this->returnArgument(0));
$this->rootFolder->method('getUserFolder')
- ->with($share->getShareOwner()->getUID())
+ ->with($share->getShareOwner())
->willReturn($userFolder);
$this->urlGenerator
->method('linkToRouteAbsolute')
->willReturn('url');
+ $initiator = $this->getMock('OCP\IUser');
+ $initiator->method('getUID')->willReturn('initiatorId');
+ $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
+
+ $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');
+
+ $this->userManager->method('get')->will($this->returnValueMap([
+ ['userId', $user],
+ ['initiatorId', $initiator],
+ ['ownerId', $owner],
+ ]));
+ $this->groupManager->method('get')->will($this->returnValueMap([
+ ['group', $group],
+ ]));
+
$expected = new \OC_OCS_Result($result);
$this->assertEquals($expected->getData(), $ocs->getShare($share->getId())->getData());
}
public function testCanAccessShare() {
$share = $this->getMock('OCP\Share\IShare');
- $share->method('getShareOwner')->willReturn($this->currentUser);
+ $share->method('getShareOwner')->willReturn($this->currentUser->getUID());
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
$share = $this->getMock('OCP\Share\IShare');
- $share->method('getSharedBy')->willReturn($this->currentUser);
+ $share->method('getSharedBy')->willReturn($this->currentUser->getUID());
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
$share = $this->getMock('OCP\Share\IShare');
$share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER);
- $share->method('getSharedWith')->willReturn($this->currentUser);
+ $share->method('getSharedWith')->willReturn($this->currentUser->getUID());
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
$share = $this->getMock('OCP\Share\IShare');
$share = $this->getMock('OCP\Share\IShare');
$share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
+ $share->method('getSharedWith')->willReturn('group');
+
$group = $this->getMock('OCP\IGroup');
$group->method('inGroup')->with($this->currentUser)->willReturn(true);
- $share->method('getSharedWith')->willReturn($group);
+ $group2 = $this->getMock('OCP\IGroup');
+ $group2->method('inGroup')->with($this->currentUser)->willReturn(false);
+
+
+ $this->groupManager->method('get')->will($this->returnValueMap([
+ ['group', $group],
+ ['group2', $group2],
+ ]));
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
$share = $this->getMock('OCP\Share\IShare');
$share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
- $group = $this->getMock('OCP\IGroup');
- $group->method('inGroup')->with($this->currentUser)->willReturn(false);
- $share->method('getSharedWith')->willReturn($group);
+ $share->method('getSharedWith')->willReturn('group2');
+
+ $this->groupManager->method('get')->with('group2')->willReturn($group);
$this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
$share = $this->getMock('OCP\Share\IShare');
$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')
~\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);
+ $share->method('setSharedWith')->with('validUser');
+ $share->method('setSharedBy')->with('currentUser');
$expected = new \OC_OCS_Result();
$result = $ocs->createShare();
$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);
+ $share->method('setSharedWith')->with('validGroup');
+ $share->method('setSharedBy')->with('currentUser');
$expected = new \OC_OCS_Result();
$result = $ocs->createShare();
$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
- $currentUser = $this->currentUser;
-
$this->shareManager->expects($this->once())->method('createShare')->with(
- $this->callback(function (\OCP\Share\IShare $share) use ($path, $currentUser) {
+ $this->callback(function (\OCP\Share\IShare $share) use ($path) {
return $share->getNode() === $path &&
$share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
$share->getPermissions() === \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE &&
- $share->getSharedBy() === $currentUser &&
+ $share->getSharedBy() === 'currentUser' &&
$share->getPassword() === null &&
$share->getExpirationDate() === null;
})
$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
- $currentUser = $this->currentUser;
-
$this->shareManager->expects($this->once())->method('createShare')->with(
- $this->callback(function (\OCP\Share\IShare $share) use ($path, $currentUser) {
+ $this->callback(function (\OCP\Share\IShare $share) use ($path) {
return $share->getNode() === $path &&
$share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
$share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
- $share->getSharedBy() === $currentUser &&
+ $share->getSharedBy() === 'currentUser' &&
$share->getPassword() === 'password' &&
$share->getExpirationDate() === null;
})
$this->shareManager->method('shareApiAllowLinks')->willReturn(true);
$this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
- $currentUser = $this->currentUser;
-
$this->shareManager->expects($this->once())->method('createShare')->with(
- $this->callback(function (\OCP\Share\IShare $share) use ($path, $currentUser) {
+ $this->callback(function (\OCP\Share\IShare $share) use ($path) {
$date = new \DateTime('2000-01-01');
$date->setTime(0,0,0);
return $share->getNode() === $path &&
$share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
$share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
- $share->getSharedBy() === $currentUser &&
+ $share->getSharedBy() === 'currentUser' &&
$share->getPassword() === null &&
$share->getExpirationDate() == $date;
})
public function testUpdateNoParametersLink() {
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK);
$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
public function testUpdateNoParametersOther() {
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
$this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share);
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setPassword('password')
->setExpirationDate(new \DateTime())
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setNode($folder);
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setNode($folder);
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setNode($folder);
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setNode($file);
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setPassword('password')
->setExpirationDate($date)
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setPassword('password')
->setExpirationDate(new \DateTime())
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setPassword('password')
->setExpirationDate($date)
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setPassword('password')
->setExpirationDate($date)
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_LINK)
->setPassword('password')
->setExpirationDate($date)
$share = \OC::$server->getShareManager()->newShare();
$share->setPermissions(\OCP\Constants::PERMISSION_ALL)
- ->setSharedBy($this->currentUser)
+ ->setSharedBy($this->currentUser->getUID())
->setShareType(\OCP\Share::SHARE_TYPE_USER)
->setNode($file);