Refactor again so we can tell what item is being reshared in case the same source shows up. This is the end of the share_api branch, moving to master.

This commit is contained in:
Michael Gapczynski 2012-08-19 22:14:38 -04:00
parent f987e22d85
commit 5eca531f99
3 changed files with 79 additions and 65 deletions

View file

@ -26,7 +26,7 @@ if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['item']
case 'share':
if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
try {
OCP\Share::shareItem($_POST['itemType'], $_POST['item'], $_POST['shareType'], $_POST['shareWith'], $_POST['permissions']);
OCP\Share::shareItem($_POST['itemType'], $_POST['item'], $_POST['item'], (int)$_POST['shareType'], $_POST['shareWith'], $_POST['permissions']);
// TODO May need to return private link
OC_JSON::success();
} catch (Exception $exception) {

View file

@ -139,7 +139,7 @@ class Share {
* @param int CRUDS permissions
* @return bool Returns true on success or false on failure
*/
public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions) {
public static function shareItem($itemType, $itemName, $itemSource, $shareType, $shareWith, $permissions) {
$uidOwner = \OC_User::getUser();
// Verify share type and sharing conditions are met
if ($shareType === self::SHARE_TYPE_USER) {
@ -192,7 +192,7 @@ class Share {
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
return self::shareItem($itemType, $itemSource, self::SHARE_TYPE_EMAIL, $details['EMAIL'], $permissions);
return self::shareItem($itemType, $itemName, $itemSource, self::SHARE_TYPE_EMAIL, $details['EMAIL'], $permissions);
} else {
// Future share types need to include their own conditions
$message = 'Share type '.$shareType.' is not valid for '.$itemSource;
@ -223,7 +223,7 @@ class Share {
array_push($files, $children);
} else {
// Pass on to put() to check if this item should be converted, the item won't be inserted into the database unless it can be converted
self::put('file', $name, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder);
self::put('file', $name, $name, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder);
}
}
return true;
@ -231,7 +231,7 @@ class Share {
return false;
} else {
// Put the item into the database
return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions);
return self::put($itemType, $itemName, $itemSource, $shareType, $shareWith, $uidOwner, $permissions);
}
}
@ -406,6 +406,7 @@ class Share {
$where .= 'WHERE file_target IS NOT NULL';
}
$fileDependent = true;
$queryArgs = array();
} else {
$fileDependent = false;
$root = '';
@ -481,9 +482,10 @@ class Share {
$queryArgs[] = $item;
}
if ($includeCollections && $collectionTypes = self::getCollectionItemTypes($itemType)) {
$placeholders = join(',', array_fill(0, count($collectionTypes), '?'));
$where .= " OR item_type IN ('".$placeholders."')";
$queryArgs = array_merge($query_args, $collectionTypes);
// TODO Bart - this doesn't work with only one argument
// $placeholders = join(',', array_fill(0, count($collectionTypes), '?'));
// $where .= " OR item_type IN ('".$placeholders."')";
// $queryArgs = array_merge($queryArgs, $collectionTypes);
}
}
if ($limit != -1 && !$includeCollections) {
@ -529,26 +531,37 @@ class Share {
$query = \OC_DB::prepare('SELECT '.$select.' FROM *PREFIX*share '.$where);
$result = $query->execute($queryArgs);
$items = array();
$itemsSources = array();
$targets = array();
while ($row = $result->fetchRow()) {
// Filter out duplicate group shares for users with unique targets
if ($row['share_type'] == self::$shareTypeGroupUserUnique) {
if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) {
$row['share_type'] = self::SHARE_TYPE_GROUP;
$row['share_with'] = $items[$row['parent']]['share_with'];
// Remove the parent group share
if (isset($items[$row['parent']])) {
unset($items[$row['parent']]);
}
unset($items[$row['parent']]);
} else if (!isset($uidOwner)) {
// Check if the same item source already exists
if (isset($itemsSources[$row[$column]])) {
// Check if the same target already exists
if (isset($targets[$row[$column]])) {
// Check if the same owner shared with the user twice through a group and user share - this is allowed
$id = $itemsSources[$row[$column]];
$id = $targets[$row[$column]];
if ($items[$id]['uid_owner'] == $row['uid_owner']) {
// Switch to group share type to ensure resharing conditions aren't bypassed
if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) {
$items[$id]['share_type'] = self::SHARE_TYPE_GROUP;
$items[$id]['share_with'] = $row['share_with'];
}
// Switch ids if sharing permission is granted on only one share to ensure correct parent is used if resharing
if (~(int)$items[$id]['permissions'] & self::PERMISSION_SHARE && (int)$row['permissions'] & self::PERMISSION_SHARE) {
$items[$row['id']] = $items[$id];
unset($items[$id]);
$id = $row['id'];
}
// Combine the permissions for the item
$items[$id]['permissions'] |= (int)$row['permissions'];
continue;
}
} else {
$itemsSources[$row[$column]] = $row['id'];
$targets[$row[$column]] = $row['id'];
}
}
// Remove root from file source paths if retrieving own shared items
@ -631,7 +644,7 @@ class Share {
* @param bool|array Parent folder target (optional)
* @return bool Returns true on success or false on failure
*/
private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder = null) {
private static function put($itemType, $itemName, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder = null) {
// Check file extension for an equivalent item type to convert to
if ($itemType == 'file') {
$extension = strtolower(substr($itemSource, strrpos($itemSource, '.') + 1));
@ -648,16 +661,18 @@ class Share {
}
$backend = self::getBackend($itemType);
// Check if this is a reshare
// TODO This query has pretty bad performance if there are large collections, figure out a way to make the collection searching more efficient
if ($checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true)) {
if ($checkReshare = self::getItemSharedWith($itemType, $itemName, self::FORMAT_NONE, null, true)) {
// Check if attempting to share back to owner
if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) {
$message = 'Sharing '.$itemSource.' failed, because the user '.$shareWith.' is the original sharer';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
// Check if attempting to share back to group TODO Check unique user target
} else if ($shareType == self::SHARE_TYPE_GROUP && $checkReshare['share_with'] == $shareWith['group']) {
$message = 'Sharing '.$itemSource.' failed, because the item was orignally shared with the group '.$shareWith['group'];
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
// Check if attempting to share back a group share to a member of the same group
} else if (($checkReshare['share_type'] == self::SHARE_TYPE_GROUP || $checkReshare['share_type'] == self::$shareTypeGroupUserUnique) && $shareType == self::SHARE_TYPE_USER) {
if ($checkReshare['share_type'] == self::$shareTypeGroupUserUnique) {
$query = \OC_DB::prepare('SELECT share_with FROM *PREFIX*share WHERE id = ?');
@ -670,7 +685,9 @@ class Share {
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
throw new \Exception($message);
}
} else if ((int)$checkReshare['permissions'] & self::PERMISSION_SHARE) {
}
// Check if share permissions is granted
if ((int)$checkReshare['permissions'] & self::PERMISSION_SHARE) {
if (~(int)$checkReshare['permissions'] & $permissions) {
$message = 'Sharing '.$itemSource.' failed, because the permissions exceed permissions granted to '.$uidOwner;
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
@ -679,6 +696,8 @@ class Share {
// TODO Don't check if inside folder
$parent = $checkReshare['id'];
$itemSource = $checkReshare['item_source'];
// TODO Suggest item/file target
$suggestedItemTarget = $checkReshare['item_target'];
$fileSource = $checkReshare['file_source'];
$filePath = $checkReshare['file_target'];
}
@ -808,6 +827,9 @@ class Share {
* @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_PRIVATE_LINK
* @param string User or group the item is being shared with
* @return string Item target
*
* TODO Use a suggested item target by default
*
*/
private static function generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner) {
$backend = self::getBackend($itemType);
@ -883,7 +905,7 @@ class Share {
$query = \OC_DB::prepare('SELECT id FROM *PREFIX*share WHERE parent IN ('.$parents.') AND uid_owner = ?');
$result = $query->execute(array($uidOwner));
} else {
$query = \OC_DB::prepare('SELECT id, item_type, item_source, parent, uid_owner FROM *PREFIX*share WHERE parent IN ('.$parents.')');
$query = \OC_DB::prepare('SELECT id, item_type, item_target, parent, uid_owner FROM *PREFIX*share WHERE parent IN ('.$parents.')');
$result = $query->execute();
}
// Reset parents array, only go through loop again if items are found
@ -891,8 +913,8 @@ class Share {
while ($item = $result->fetchRow()) {
// Search for a duplicate parent share, this occurs when an item is shared to the same user through a group and user or the same item is shared by different users
$userAndGroups = array_merge(array($item['uid_owner']), \OC_Group::getUserGroups($item['uid_owner']));
$query = \OC_DB::prepare("SELECT id, permissions FROM *PREFIX*share WHERE item_type = ? AND item_source = ? AND share_type IN (?,?,?) AND share_with IN ('".implode("','", $userAndGroups)."') AND uid_owner != ? AND id != ?");
$duplicateParent = $query->execute(array($item['item_type'], $item['item_source'], self::SHARE_TYPE_USER, self::SHARE_TYPE_GROUP, self::$shareTypeGroupUserUnique, $item['uid_owner'], $item['parent']))->fetchRow();
$query = \OC_DB::prepare("SELECT id, permissions FROM *PREFIX*share WHERE item_type = ? AND item_target = ? AND share_type IN (?,?,?) AND share_with IN ('".implode("','", $userAndGroups)."') AND uid_owner != ? AND id != ?");
$duplicateParent = $query->execute(array($item['item_type'], $item['item_target'], self::SHARE_TYPE_USER, self::SHARE_TYPE_GROUP, self::$shareTypeGroupUserUnique, $item['uid_owner'], $item['parent']))->fetchRow();
if ($duplicateParent) {
// Change the parent to the other item id if share permission is granted
if ($duplicateParent['permissions'] & self::PERMISSION_SHARE) {

View file

@ -63,19 +63,13 @@ class Test_Share extends UnitTestCase {
public function testShareInvalidShareType() {
$this->expectException(new Exception('Share type foobar is not valid for test.txt'));
OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', 'foobar', $this->user2, OCP\Share::PERMISSION_READ);
}
public function testInvalidItemType() {
$message = 'Sharing backend for foobar not found';
try {
OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
try {
OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('foobar', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -122,28 +116,28 @@ class Test_Share extends UnitTestCase {
// Invalid shares
$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
$message = 'Sharing test.txt failed, because the user foobar does not exist';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
$message = 'Sharing foobar failed, because the sharing backend for test could not find its source';
try {
OCP\Share::shareItem('test', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'foobar', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
// Valid share
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
$this->assertEqual(OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt'));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt'));
@ -152,7 +146,7 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user1);
$message = 'Sharing test.txt failed, because this item is already shared with '.$this->user2;
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -162,7 +156,7 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user2);
$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -173,11 +167,11 @@ class Test_Share extends UnitTestCase {
$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
// Attempt reshare without share permission
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
OC_User::setUserId($this->user2);
$message = 'Sharing test.txt failed, because resharing is not allowed';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -191,14 +185,14 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user2);
$message = 'Sharing test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
// Valid reshare
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE));
$this->assertEqual(OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt'));
OC_User::setUserId($this->user3);
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt'));
@ -234,7 +228,7 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE));
OC_User::setUserId($this->user2);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ));
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
OC_User::setUserId($this->user2);
@ -244,9 +238,9 @@ class Test_Share extends UnitTestCase {
// Attempt target conflict
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
OC_User::setUserId($this->user3);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt', 'test1.txt'));
@ -260,21 +254,21 @@ class Test_Share extends UnitTestCase {
// Invalid shares
$message = 'Sharing test.txt failed, because the group foobar does not exist';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
$message = 'Sharing test.txt failed, because '.$this->user1.' is not a member of the group '.$this->group2;
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
}
// Valid share
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ));
$this->assertEqual(OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt'));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), array('test.txt'));
@ -285,7 +279,7 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user1);
$message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -295,7 +289,7 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user2);
$message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -304,7 +298,7 @@ class Test_Share extends UnitTestCase {
// Attempt to share back to group
$message = 'Sharing test.txt failed, because the item was orignally shared with the group '.$this->group1;
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -313,7 +307,7 @@ class Test_Share extends UnitTestCase {
// Attempt to share back to member of group
$message = 'Sharing test.txt failed, because the user '.$this->user3.' is a member of the original group share';
try {
OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ);
OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, OCP\Share::PERMISSION_READ);
$this->fail('Exception was expected: '.$message);
} catch (Exception $exception) {
$this->assertEqual($exception->getMessage(), $message);
@ -324,18 +318,18 @@ class Test_Share extends UnitTestCase {
$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
// Valid share with same person - user then group
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE | OCP\Share::PERMISSION_SHARE));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_SHARE));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE | OCP\Share::PERMISSION_SHARE));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt'));
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE | OCP\Share::PERMISSION_SHARE));
OC_User::setUserId($this->user3);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt'));
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_SHARE));
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE));
// Valid reshare
OC_User::setUserId($this->user2);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\Share::PERMISSION_READ));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\Share::PERMISSION_READ));
OC_User::setUserId($this->user4);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt'));
@ -343,33 +337,31 @@ class Test_Share extends UnitTestCase {
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_SHARE));
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE));
OC_User::setUserId($this->user4);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt'));
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array());
// Valid share with same person - group then user
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt'));
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE | OCP\Share::PERMISSION_SHARE));
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_UPDATE | OCP\Share::PERMISSION_DELETE));
// Unshare from group only
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS), array(OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_DELETE));
OC_User::setUserId($this->user4);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array());
// Attempt user specific target conflict
OC_User::setUserId($this->user3);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE));
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE));
OC_User::setUserId($this->user2);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test.txt', 'test1.txt'));
// Valid reshare
$this->assertTrue(OCP\Share::shareItem('test', 'test1.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE));
// Valid reshare TODO Broken
$this->assertTrue(OCP\Share::shareItem('test', 'test1.txt', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, OCP\Share::PERMISSION_READ | OCP\Share::PERMISSION_SHARE));
OC_User::setUserId($this->user4);
$this->assertEqual(OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET), array('test1.txt'));