summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorblizzz <blizzz@owncloud.com>2014-07-01 14:37:09 +0200
committerblizzz <blizzz@owncloud.com>2014-07-01 14:37:09 +0200
commit616f9b1e03b7939605ec233f7af5cc78b0be9fde (patch)
tree70db45859bf3662302a012323bb0e1bfac2344a6
parent16ce23d19df352cd2653387bf00a0e210016d2f2 (diff)
parent16275eca845f7bb7c4872f1120f76177f593a693 (diff)
downloadnextcloud-server-616f9b1e03b7939605ec233f7af5cc78b0be9fde.tar.gz
nextcloud-server-616f9b1e03b7939605ec233f7af5cc78b0be9fde.zip
Merge pull request #9156 from owncloud/solve-8959
search term for users and groups may occur anywhere in the name or displ...
-rw-r--r--lib/private/group/database.php12
-rw-r--r--lib/private/group/dummy.php33
-rw-r--r--lib/private/user/database.php4
-rw-r--r--lib/private/user/dummy.php11
-rw-r--r--tests/lib/group/backend.php44
-rw-r--r--tests/lib/group/database.php6
-rw-r--r--tests/lib/user/backend.php17
-rw-r--r--tests/lib/user/database.php3
8 files changed, 115 insertions, 15 deletions
diff --git a/lib/private/group/database.php b/lib/private/group/database.php
index baaf2cf2739..b7148f38fe3 100644
--- a/lib/private/group/database.php
+++ b/lib/private/group/database.php
@@ -169,7 +169,7 @@ class OC_Group_Database extends OC_Group_Backend {
*/
public function getGroups($search = '', $limit = null, $offset = null) {
$stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` LIKE ?', $limit, $offset);
- $result = $stmt->execute(array($search.'%'));
+ $result = $stmt->execute(array('%' . $search . '%'));
$groups = array();
while ($row = $result->fetchRow()) {
$groups[] = $row['gid'];
@@ -203,7 +203,7 @@ class OC_Group_Database extends OC_Group_Backend {
$stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?',
$limit,
$offset);
- $result = $stmt->execute(array($gid, $search.'%'));
+ $result = $stmt->execute(array($gid, '%'.$search.'%'));
$users = array();
while ($row = $result->fetchRow()) {
$users[] = $row['uid'];
@@ -220,8 +220,12 @@ class OC_Group_Database extends OC_Group_Backend {
*/
public function countUsersInGroup($gid, $search = '') {
$stmt = OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?');
- $result = $stmt->execute(array($gid, $search.'%'));
- return $result->fetchOne();
+ $result = $stmt->execute(array($gid, '%' . $search . '%'));
+ $count = $result->fetchOne();
+ if($count !== false) {
+ $count = intval($count);
+ }
+ return $count;
}
}
diff --git a/lib/private/group/dummy.php b/lib/private/group/dummy.php
index e48c6a0e266..4af18b267bc 100644
--- a/lib/private/group/dummy.php
+++ b/lib/private/group/dummy.php
@@ -143,7 +143,16 @@ class OC_Group_Dummy extends OC_Group_Backend {
* @return array an array of group names
*/
public function getGroups($search = '', $limit = -1, $offset = 0) {
- return array_keys($this->groups);
+ if(empty($search)) {
+ return array_keys($this->groups);
+ }
+ $result = array();
+ foreach(array_keys($this->groups) as $group) {
+ if(stripos($group, $search) !== false) {
+ $result[] = $group;
+ }
+ }
+ return $result;
}
/**
@@ -156,7 +165,16 @@ class OC_Group_Dummy extends OC_Group_Backend {
*/
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
if(isset($this->groups[$gid])) {
- return $this->groups[$gid];
+ if(empty($search)) {
+ return $this->groups[$gid];
+ }
+ $result = array();
+ foreach($this->groups[$gid] as $user) {
+ if(stripos($user, $search) !== false) {
+ $result[] = $user;
+ }
+ }
+ return $result;
}else{
return array();
}
@@ -172,7 +190,16 @@ class OC_Group_Dummy extends OC_Group_Backend {
*/
public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
if(isset($this->groups[$gid])) {
- return count($this->groups[$gid]);
+ if(empty($search)) {
+ return count($this->groups[$gid]);
+ }
+ $count = 0;
+ foreach($this->groups[$gid] as $user) {
+ if(stripos($user, $search) !== false) {
+ $count++;
+ }
+ }
+ return $count;
}
}
diff --git a/lib/private/user/database.php b/lib/private/user/database.php
index d9263f6b5de..e9844f0f79c 100644
--- a/lib/private/user/database.php
+++ b/lib/private/user/database.php
@@ -158,7 +158,7 @@ class OC_User_Database extends OC_User_Backend {
$query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`'
. ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR '
. 'LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
- $result = $query->execute(array($search . '%', $search . '%'));
+ $result = $query->execute(array('%' . $search . '%', '%' . $search . '%'));
$users = array();
while ($row = $result->fetchRow()) {
$displayNames[$row['uid']] = $row['displayname'];
@@ -232,7 +232,7 @@ class OC_User_Database extends OC_User_Backend {
*/
public function getUsers($search = '', $limit = null, $offset = null) {
$query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users` WHERE LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
- $result = $query->execute(array($search . '%'));
+ $result = $query->execute(array('%' . $search . '%'));
$users = array();
while ($row = $result->fetchRow()) {
$users[] = $row['uid'];
diff --git a/lib/private/user/dummy.php b/lib/private/user/dummy.php
index 776168048f6..dbcbb2a46f7 100644
--- a/lib/private/user/dummy.php
+++ b/lib/private/user/dummy.php
@@ -105,7 +105,16 @@ class OC_User_Dummy extends OC_User_Backend {
* Get a list of all users.
*/
public function getUsers($search = '', $limit = null, $offset = null) {
- return array_keys($this->users);
+ if(empty($search)) {
+ return array_keys($this->users);
+ }
+ $result = array();
+ foreach(array_keys($this->users) as $user) {
+ if(stripos($user, $search) !== false) {
+ $result[] = $user;
+ }
+ }
+ return $result;
}
/**
diff --git a/tests/lib/group/backend.php b/tests/lib/group/backend.php
index 2c563ae9ac9..95a5cf5f49c 100644
--- a/tests/lib/group/backend.php
+++ b/tests/lib/group/backend.php
@@ -31,8 +31,12 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase {
* test cases can override this in order to clean up created groups
* @return string
*/
- public function getGroupName() {
- return uniqid('test_');
+ public function getGroupName($name = null) {
+ if(is_null($name)) {
+ return uniqid('test_');
+ } else {
+ return $name;
+ }
}
/**
@@ -88,7 +92,7 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase {
$this->assertFalse($this->backend->inGroup($user2, $group1));
$this->assertFalse($this->backend->inGroup($user1, $group2));
$this->assertFalse($this->backend->inGroup($user2, $group2));
-
+
$this->assertFalse($this->backend->addToGroup($user1, $group1));
$this->assertEquals(array($user1), $this->backend->usersInGroup($group1));
@@ -102,4 +106,38 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase {
$this->assertEquals(array(), $this->backend->usersInGroup($group1));
$this->assertFalse($this->backend->inGroup($user1, $group1));
}
+
+ public function testSearchGroups() {
+ $name1 = $this->getGroupName('foobarbaz');
+ $name2 = $this->getGroupName('bazbarfoo');
+ $name3 = $this->getGroupName('notme');
+
+ $this->backend->createGroup($name1);
+ $this->backend->createGroup($name2);
+ $this->backend->createGroup($name3);
+
+ $result = $this->backend->getGroups('bar');
+ $this->assertSame(2, count($result));
+ }
+
+ public function testSearchUsers() {
+ $group = $this->getGroupName();
+ $this->backend->createGroup($group);
+
+ $name1 = 'foobarbaz';
+ $name2 = 'bazbarfoo';
+ $name3 = 'notme';
+
+ $this->backend->addToGroup($name1, $group);
+ $this->backend->addToGroup($name2, $group);
+ $this->backend->addToGroup($name3, $group);
+
+ $result = $this->backend->usersInGroup($group, 'bar');
+ $this->assertSame(2, count($result));
+
+ $result = $this->backend->countUsersInGroup($group, 'bar');
+ $this->assertSame(2, $result);
+ }
+
+
}
diff --git a/tests/lib/group/database.php b/tests/lib/group/database.php
index 3e05c656061..9b39ac00452 100644
--- a/tests/lib/group/database.php
+++ b/tests/lib/group/database.php
@@ -28,8 +28,10 @@ class Test_Group_Database extends Test_Group_Backend {
* test cases can override this in order to clean up created groups
* @return string
*/
- public function getGroupName() {
- $name=uniqid('test_');
+ public function getGroupName($name = null) {
+ if(is_null($name)) {
+ $name=uniqid('test_');
+ }
$this->groups[]=$name;
return $name;
}
diff --git a/tests/lib/user/backend.php b/tests/lib/user/backend.php
index 1384c54a921..0d3914c7ca6 100644
--- a/tests/lib/user/backend.php
+++ b/tests/lib/user/backend.php
@@ -96,4 +96,21 @@ abstract class Test_User_Backend extends PHPUnit_Framework_TestCase {
$this->assertSame($name1, $this->backend->checkPassword($name1, 'newpass1'));
$this->assertFalse($this->backend->checkPassword($name2, 'newpass1'));
}
+
+ public function testSearch() {
+ $name1 = 'foobarbaz';
+ $name2 = 'bazbarfoo';
+ $name3 = 'notme';
+
+ $this->backend->createUser($name1, 'pass1');
+ $this->backend->createUser($name2, 'pass2');
+ $this->backend->createUser($name3, 'pass3');
+
+ $result = $this->backend->getUsers('bar');
+ $this->assertSame(2, count($result));
+
+ $result = $this->backend->getDisplayNames('bar');
+ $this->assertSame(2, count($result));
+ }
+
}
diff --git a/tests/lib/user/database.php b/tests/lib/user/database.php
index d7cc39ae387..a8e497720c2 100644
--- a/tests/lib/user/database.php
+++ b/tests/lib/user/database.php
@@ -32,6 +32,9 @@ class Test_User_Database extends Test_User_Backend {
}
public function tearDown() {
+ if(!isset($this->users)) {
+ return;
+ }
foreach($this->users as $user) {
$this->backend->deleteUser($user);
}