Merge pull request #9915 from suraia/unsharefromself-source

Allow specifying the item source in unshareFromSelf().
This commit is contained in:
Björn Schießle 2014-08-27 10:31:35 +02:00
commit c35d60f6d8
3 changed files with 33 additions and 8 deletions

View File

@ -719,23 +719,24 @@ class Share extends \OC\Share\Constants {
/** /**
* Unshare an item shared with the current user * Unshare an item shared with the current user
* @param string $itemType * @param string $itemType
* @param string $itemTarget * @param string $itemOrigin Item target or source
* @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional)
* @return boolean true on success or false on failure * @return boolean true on success or false on failure
* *
* Unsharing from self is not allowed for items inside collections * Unsharing from self is not allowed for items inside collections
*/ */
public static function unshareFromSelf($itemType, $itemTarget) { public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) {
$originType = ($originIsSource) ? 'source' : 'target';
$uid = \OCP\User::getUser(); $uid = \OCP\User::getUser();
if ($itemType === 'file' || $itemType === 'folder') { if ($itemType === 'file' || $itemType === 'folder') {
$statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_target` = ?'; $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?';
} else { } else {
$statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_target` = ?'; $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?';
} }
$query = \OCP\DB::prepare($statement); $query = \OCP\DB::prepare($statement);
$result = $query->execute(array($itemType, $itemTarget)); $result = $query->execute(array($itemType, $itemOrigin));
$shares = $result->fetchAll(); $shares = $result->fetchAll();

View File

@ -265,8 +265,8 @@ class Share extends \OC\Share\Constants {
* *
* Unsharing from self is not allowed for items inside collections * Unsharing from self is not allowed for items inside collections
*/ */
public static function unshareFromSelf($itemType, $itemTarget) { public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) {
return \OC\Share\Share::unshareFromSelf($itemType, $itemTarget); return \OC\Share\Share::unshareFromSelf($itemType, $itemOrigin, $originIsSource);
} }
/** /**

View File

@ -314,6 +314,25 @@ class Test_Share extends PHPUnit_Framework_TestCase {
$this->assertTrue(in_array('test.txt', $to_test)); $this->assertTrue(in_array('test.txt', $to_test));
$this->assertTrue(in_array('test1.txt', $to_test)); $this->assertTrue(in_array('test1.txt', $to_test));
// Unshare from self
$this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
$this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
// Unshare from self via source
$this->assertTrue(OCP\Share::unshareFromSelf('test', 'share.txt', true));
$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
OC_User::setUserId($this->user3);
$this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, OCP\PERMISSION_READ));
OC_User::setUserId($this->user2);
$to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
$this->assertEquals(2, count($to_test));
$this->assertTrue(in_array('test.txt', $to_test));
$this->assertTrue(in_array('test1.txt', $to_test));
// Remove user // Remove user
OC_User::setUserId($this->user1); OC_User::setUserId($this->user1);
OC_User::deleteUser($this->user1); OC_User::deleteUser($this->user1);
@ -568,6 +587,11 @@ class Test_Share extends PHPUnit_Framework_TestCase {
OC_User::setUserId($this->user2); OC_User::setUserId($this->user2);
$this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET)); $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
// Unshare from self via source
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::unshareFromSelf('test', 'share.txt', true));
$this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
// Remove group // Remove group
OC_Group::deleteGroup($this->group1); OC_Group::deleteGroup($this->group1);
OC_User::setUserId($this->user4); OC_User::setUserId($this->user4);