diff options
author | Morris Jobke <hey@morrisjobke.de> | 2015-01-23 12:59:39 +0100 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-01-23 12:59:39 +0100 |
commit | 80560e70b7133739f87cf6a03acb55169fb2462f (patch) | |
tree | a7ba5dc6981c79dde9302a6c12700b3b489d5ca6 | |
parent | 56c51d481c8370d0171f4ebb073ed757ccb5c07e (diff) | |
parent | 18d46dfdeb2facc9f5c5d9f1827b27714acb96dd (diff) | |
download | nextcloud-server-80560e70b7133739f87cf6a03acb55169fb2462f.tar.gz nextcloud-server-80560e70b7133739f87cf6a03acb55169fb2462f.zip |
Merge pull request #13470 from owncloud/stable7-share-fixfindshareforuserwithmultiplegroups
[stable7] Fix getItemSharedWithUser for groups
-rw-r--r-- | lib/private/share/share.php | 29 | ||||
-rw-r--r-- | tests/lib/share/share.php | 90 |
2 files changed, 107 insertions, 12 deletions
diff --git a/lib/private/share/share.php b/lib/private/share/share.php index b637b87b5eb..be113568a3b 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -287,7 +287,7 @@ class Share extends \OC\Share\Constants { * Get the item of item type shared with a given user by source * @param string $itemType * @param string $itemSource - * @param string $user User user to whom the item was shared + * @param string $user User to whom the item was shared * @param int $shareType only look for a specific share type * @return array Return list of items with file_target, permissions and expiration */ @@ -332,18 +332,23 @@ class Share extends \OC\Share\Constants { if(empty($shares) && $user !== null) { $groups = \OC_Group::getUserGroups($user); - $query = \OC_DB::prepare( - 'SELECT * - FROM - `*PREFIX*share` - WHERE - `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)' - ); - - $result = \OC_DB::executeAudited($query, array($itemSource, $itemType, implode(',', $groups))); + if (!empty($groups)) { + $where = 'WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; + $arguments = array($itemSource, $itemType, $groups); + $types = array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY); + + // TODO: inject connection, hopefully one day in the future when this + // class isn't static anymore... + $conn = \OC_DB::getConnection(); + $result = $conn->executeQuery( + 'SELECT * FROM `*PREFIX*share` ' . $where, + $arguments, + $types + ); - while ($row = $result->fetchRow()) { - $shares[] = $row; + while ($row = $result->fetch()) { + $shares[] = $row; + } } } diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index c3264a8637d..d2825f320ca 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -27,6 +27,7 @@ class Test_Share extends Test\TestCase { protected $user2; protected $user3; protected $user4; + protected $user5; protected $groupAndUser; protected $groupBackend; protected $group1; @@ -43,11 +44,13 @@ class Test_Share extends Test\TestCase { $this->user2 = $this->getUniqueID('user2_'); $this->user3 = $this->getUniqueID('user3_'); $this->user4 = $this->getUniqueID('user4_'); + $this->user5 = $this->getUniqueID('user5_'); $this->groupAndUser = $this->getUniqueID('groupAndUser_'); OC_User::createUser($this->user1, 'pass'); OC_User::createUser($this->user2, 'pass'); OC_User::createUser($this->user3, 'pass'); OC_User::createUser($this->user4, 'pass'); + OC_User::createUser($this->user5, 'pass'); // no group OC_User::createUser($this->groupAndUser, 'pass'); OC_User::setUserId($this->user1); OC_Group::clearBackends(); @@ -81,6 +84,18 @@ class Test_Share extends Test\TestCase { $query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?'); $query->execute(array('test')); OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing); + + OC_User::deleteUser($this->user1); + OC_User::deleteUser($this->user2); + OC_User::deleteUser($this->user3); + OC_User::deleteUser($this->user4); + OC_User::deleteUser($this->user5); + OC_User::deleteUser($this->groupAndUser); + + OC_Group::deleteGroup($this->group1); + OC_Group::deleteGroup($this->group2); + OC_Group::deleteGroup($this->groupAndUser); + parent::tearDown(); } @@ -632,6 +647,81 @@ class Test_Share extends Test\TestCase { return $row; } + public function testGetItemSharedWithUser() { + OC_User::setUserId($this->user1); + + //add dummy values to the share table + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' + .' `item_type`, `item_source`, `item_target`, `share_type`,' + .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); + $args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_USER, $this->user2, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_USER, $this->user4, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user2); + $query->execute($args); + $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user4); + $query->execute($args); + $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user5, $this->user4); + $query->execute($args); + + + $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2); + $this->assertSame(1, count($result1)); + $this->verifyResult($result1, array('target1')); + + $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null); + $this->assertSame(5, count($result2)); + $this->verifyResult($result2, array('target1', 'target2')); + + $result3 = \OCP\Share::getItemSharedWithUser('test', 99, null); + $this->assertSame(5, count($result3)); // 5 because target4 appears twice + $this->verifyResult($result3, array('target1', 'target2', 'target3', 'target4')); + + $result5 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user5); + $this->assertSame(1, count($result5)); + $this->verifyResult($result5, array('target4')); + } + + public function testGetItemSharedWithUserFromGroupShare() { + OC_User::setUserId($this->user1); + + //add dummy values to the share table + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' + .' `item_type`, `item_source`, `item_target`, `share_type`,' + .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); + $args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_GROUP, $this->group2, $this->user1); + $query->execute($args); + $args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user2); + $query->execute($args); + $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user4); + $query->execute($args); + + // user2 is in group1 and group2 + $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2); + $this->assertSame(4, count($result1)); + $this->verifyResult($result1, array('target1', 'target2', 'target3', 'target4')); + + $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null); + $this->assertSame(4, count($result2)); + $this->verifyResult($result2, array('target1', 'target2', 'target3', 'target4')); + + $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user5); + $this->assertSame(0, count($result3)); + } + + public function verifyResult($result, $expected) { + foreach ($result as $r) { + if (in_array($r['item_target'], $expected)) { + $key = array_search($r['item_target'], $expected); + unset($expected[$key]); + } + } + $this->assertEmpty($expected, 'did not found all expected values'); + } + public function testShareItemWithLink() { OC_User::setUserId($this->user1); $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, OCP\PERMISSION_READ); |