[Pkg-owncloud-commits] [owncloud] 55/90: Fix getItemSharedWithUser for groups

David Prévot taffit at moszumanska.debian.org
Fri Feb 6 21:10:52 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 18d46dfdeb2facc9f5c5d9f1827b27714acb96dd
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Fri Jan 16 18:11:13 2015 +0100

    Fix getItemSharedWithUser for groups
    
    Fixed SQL query for whenever a user has more than one group.
    Added missing $owner where clause for group lookup.
    Added unit tests for the group cases.
    
    Backport of 40931a8b0d5d10c0f711756a4e8a423ff055621e from master
---
 lib/private/share/share.php | 29 +++++++++------
 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 b637b87..be11356 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 c3264a8..d2825f3 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);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list