From baccc8f584940d607393ef2bdd9c6d3e511b75b8 Mon Sep 17 00:00:00 2001 From: ben-denham Date: Mon, 6 Jan 2014 11:14:43 +1300 Subject: [PATCH 1/2] Unshare all will now delete all shares for the item, instead of only for a single owner. --- lib/public/share.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/public/share.php b/lib/public/share.php index f0fd8e1ab1..eb1dd8d1c9 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -655,7 +655,15 @@ class Share { * @return Returns true on success or false on failure */ public static function unshareAll($itemType, $itemSource) { - if ($shares = self::getItemShared($itemType, $itemSource)) { + // Get all of the owners of shares of this item. + $query = \OC_DB::prepare( 'SELECT `uid_owner` from `*PREFIX*share` WHERE `item_type`=? AND `item_source`=?' ); + $result = $query->execute(array($itemType, $itemSource)); + $shares = array(); + // Add each owner's shares to the array of all shares for this item. + while ($row = $result->fetchRow()) { + $shares = array_merge($shares, self::getItems($itemType, $itemSource, null, null, $row['uid_owner'])); + } + if (!empty($shares)) { // Pass all the vars we have for now, they may be useful $hookParams = array( 'itemType' => $itemType, From 30f77f53d5c97e369a879eab9381b856a5caf79c Mon Sep 17 00:00:00 2001 From: ben-denham Date: Fri, 10 Jan 2014 14:08:29 +1300 Subject: [PATCH 2/2] updated the unit test for OCP\Share::unshareAll() to verify that shares by all users are removed for an item. --- tests/lib/share/share.php | 58 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index 84e2e5c63e..2fe2837019 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -149,6 +149,26 @@ class Test_Share extends PHPUnit_Framework_TestCase { ); } + protected function shareUserTestFileWithUser($sharer, $receiver) { + OC_User::setUserId($sharer); + $this->assertTrue( + OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $receiver, OCP\PERMISSION_READ | OCP\PERMISSION_SHARE), + 'Failed asserting that ' . $sharer . ' successfully shared text.txt with ' . $receiver . '.' + ); + $this->assertContains( + 'test.txt', + OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), + 'Failed asserting that test.txt is a shared file of ' . $sharer . '.' + ); + + OC_User::setUserId($receiver); + $this->assertContains( + 'test.txt', + OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), + 'Failed asserting that ' . $receiver . ' has access to test.txt after initial sharing.' + ); + } + public function testShareWithUser() { // Invalid shares $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner'; @@ -585,25 +605,55 @@ class Test_Share extends PHPUnit_Framework_TestCase { } public function testUnshareAll() { - $this->shareUserOneTestFileWithUserTwo(); + $this->shareUserTestFileWithUser($this->user1, $this->user2); + $this->shareUserTestFileWithUser($this->user2, $this->user3); + $this->shareUserTestFileWithUser($this->user3, $this->user4); $this->shareUserOneTestFileWithGroupOne(); OC_User::setUserId($this->user1); $this->assertEquals( array('test.txt', 'test.txt'), OCP\Share::getItemsShared('test', 'test.txt'), - 'Failed asserting that the test.txt file is shared exactly two times.' + 'Failed asserting that the test.txt file is shared exactly two times by user1.' + ); + + OC_User::setUserId($this->user2); + $this->assertEquals( + array('test.txt'), + OCP\Share::getItemsShared('test', 'test.txt'), + 'Failed asserting that the test.txt file is shared exactly once by user2.' + ); + + OC_User::setUserId($this->user3); + $this->assertEquals( + array('test.txt'), + OCP\Share::getItemsShared('test', 'test.txt'), + 'Failed asserting that the test.txt file is shared exactly once by user3.' ); $this->assertTrue( OCP\Share::unshareAll('test', 'test.txt'), - 'Failed asserting that user 1 successfully unshared all shares of the test.txt share.' + 'Failed asserting that user 3 successfully unshared all shares of the test.txt share.' ); $this->assertEquals( array(), OCP\Share::getItemsShared('test'), - 'Failed asserting that both shares of the test.txt file have been removed.' + 'Failed asserting that the share of the test.txt file by user 3 has been removed.' + ); + + OC_User::setUserId($this->user1); + $this->assertEquals( + array(), + OCP\Share::getItemsShared('test'), + 'Failed asserting that both shares of the test.txt file by user 1 have been removed.' + ); + + OC_User::setUserId($this->user2); + $this->assertEquals( + array(), + OCP\Share::getItemsShared('test'), + 'Failed asserting that the share of the test.txt file by user 2 has been removed.' ); } }