Skip to content

Commit 750ca10

Browse files
committed
Implement ISearchableGroupBackend
1 parent 5f72a48 commit 750ca10

File tree

5 files changed

+114
-11
lines changed

5 files changed

+114
-11
lines changed

lib/Backend/GroupBackend.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
use OCP\Group\Backend\ICountUsersBackend;
3232
use OCP\Group\Backend\IGroupDetailsBackend;
3333
use OCP\Group\Backend\IIsAdminBackend;
34+
use OCP\Group\Backend\ISearchableGroupBackend;
3435
use OCP\ILogger;
36+
use OCP\IUserManager;
37+
38+
use OC\User\LazyUser;
3539

3640
/**
3741
* The SQL group backend manager.
@@ -41,7 +45,8 @@
4145
final class GroupBackend extends ABackend implements
4246
ICountUsersBackend,
4347
IGroupDetailsBackend,
44-
IIsAdminBackend
48+
IIsAdminBackend,
49+
ISearchableGroupBackend
4550
{
4651
const USER_SQL_GID = "user_sql";
4752

@@ -354,16 +359,16 @@ public function usersInGroup($gid, $search = "", $limit = -1, $offset = 0)
354359
["app" => $this->appName]
355360
);
356361

357-
$cacheKey = self::class . "group_users_" . $gid . "_" . $search . "_"
362+
$cacheKey = self::class . "group_uids_" . $gid . "_" . $search . "_"
358363
. $limit . "_" . $offset;
359-
$users = $this->cache->get($cacheKey);
364+
$uids = $this->cache->get($cacheKey);
360365

361-
if (!is_null($users)) {
366+
if (!is_null($uids)) {
362367
$this->logger->debug(
363368
"Returning from cache usersInGroup($gid, $search, $limit, $offset): count("
364-
. count($users) . ")", ["app" => $this->appName]
369+
. count($uids) . ")", ["app" => $this->appName]
365370
);
366-
return $users;
371+
return $uids;
367372
}
368373

369374
$uids = $this->groupRepository->findAllUidsBySearchTerm(
@@ -383,6 +388,50 @@ public function usersInGroup($gid, $search = "", $limit = -1, $offset = 0)
383388
return $uids;
384389
}
385390

391+
/**
392+
* @inheritdoc
393+
*/
394+
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array
395+
{
396+
$this->logger->debug(
397+
"Entering searchInGroup($gid, $search, $limit, $offset)",
398+
["app" => $this->appName]
399+
);
400+
401+
$cacheKey = self::class . "group_users_" . $gid . "_" . $search . "_"
402+
. $limit . "_" . $offset;
403+
$names = $this->cache->get($cacheKey);
404+
405+
if ($names === null) {
406+
$names = $this->groupRepository->findAllUsersBySearchTerm(
407+
$this->substituteGid($gid), "%" . $search . "%", $limit, $offset
408+
);
409+
410+
if ($names === false) {
411+
return [];
412+
}
413+
414+
$this->cache->set($cacheKey, $names);
415+
$this->logger->debug(
416+
"Using from DB searchInGroup($gid, $search, $limit, $offset): count("
417+
. count($names) . ")", ["app" => $this->appName]
418+
);
419+
} else {
420+
$this->logger->debug(
421+
"Using from cache searchInGroup($gid, $search, $limit, $offset): count("
422+
. count($names) . ")", ["app" => $this->appName]
423+
);
424+
}
425+
426+
$users = [];
427+
$userManager = \OCP\Server::get(IUserManager::class);
428+
foreach ($names as $uid => $name) {
429+
$users[$uid] = new LazyUser($uid, $userManager, $name);
430+
}
431+
432+
return $users;
433+
}
434+
386435
/**
387436
* @inheritdoc
388437
*/

lib/Constant/Query.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ final class Query
3232
const COUNT_GROUPS = "count_groups";
3333
const COUNT_USERS = "count_users";
3434
const FIND_GROUP = "find_group";
35+
const FIND_GROUP_UIDS = "find_group_uids";
3536
const FIND_GROUP_USERS = "find_group_users";
3637
const FIND_GROUPS = "find_groups";
3738
const FIND_USER_BY_UID = "find_user_by_uid";

lib/Query/DataQuery.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ public function queryColumn(
223223
return $result->fetchFirstColumn();
224224
}
225225

226+
/**
227+
* Fetch values from all columns which the given query returns.
228+
*
229+
* @param string $queryName The query to execute.
230+
* @param array $params The query parameters to bind.
231+
* @param int $limit Results limit. Defaults to -1 (no limit).
232+
* @param int $offset Results offset. Defaults to 0.
233+
*
234+
* @return array|bool Queried column or FALSE on failure.
235+
*/
236+
public function queryColumns(
237+
$queryName, $params = [], $limit = -1, $offset = 0
238+
) {
239+
$result = $this->execQuery($queryName, $params, $limit, $offset);
240+
if ($result === false) {
241+
return false;
242+
}
243+
244+
return $result->fetchAll();
245+
}
246+
226247
/**
227248
* Fetch entity returned by the given query.
228249
*

lib/Query/QueryProvider.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,23 @@ private function loadQueries()
154154
"FROM $group g " .
155155
"WHERE g.$gGID = :$gidParam",
156156

157+
Query::FIND_GROUP_UIDS =>
158+
"SELECT DISTINCT u.$uUID AS uid " .
159+
"FROM $user u " .
160+
"LEFT JOIN $userGroup ug ON u.$uUID = ug.$ugUID " .
161+
"WHERE ug.$ugGID LIKE :$gidParam " .
162+
"AND u.$uUID LIKE :$searchParam " .
163+
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled ") .
164+
"ORDER BY u.$uUID",
165+
157166
Query::FIND_GROUP_USERS =>
158-
"SELECT DISTINCT ug.$ugUID AS uid " .
159-
"FROM $userGroup ug " .
160-
"LEFT JOIN $user u ON u.$uUID = ug.$ugUID " .
167+
"SELECT DISTINCT u.$uUID AS uid, u.$uName AS name " .
168+
"FROM $user u " .
169+
"LEFT JOIN $userGroup ug ON u.$uUID = ug.$ugUID " .
161170
"WHERE ug.$ugGID LIKE :$gidParam " .
162-
"AND ug.$ugUID LIKE :$searchParam " .
171+
"AND u.$uUID LIKE :$searchParam " .
163172
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled ") .
164-
"ORDER BY ug.$ugUID",
173+
"ORDER BY u.$uUID",
165174

166175
Query::FIND_GROUPS =>
167176
"SELECT $groupColumns " .

lib/Repository/GroupRepository.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,33 @@ public function findAllUidsBySearchTerm(
9292
$gid, $search = "", $limit = -1, $offset = 0
9393
) {
9494
return $this->dataQuery->queryColumn(
95+
Query::FIND_GROUP_UIDS,
96+
[Query::GID_PARAM => $gid, Query::SEARCH_PARAM => $search], $limit,
97+
$offset
98+
);
99+
}
100+
101+
/**
102+
* Get a list of all user IDs and their display-name belonging to the group.
103+
*
104+
* @param string $gid The group ID.
105+
* @param string $search The UID search term. Defaults to "" (empty string).
106+
* @param int $limit (optional) Results limit.
107+
* Defaults to -1 (no limit).
108+
* @param int $offset (optional) Results offset. Defaults to 0.
109+
*
110+
* @return array<string, string> Array of display-names indexed by UIDs belonging to the group
111+
* or FALSE on failure.
112+
*/
113+
public function findAllUsersBySearchTerm(
114+
$gid, $search = "", $limit = -1, $offset = 0
115+
) {
116+
$data = $this->dataQuery->queryColumns(
95117
Query::FIND_GROUP_USERS,
96118
[Query::GID_PARAM => $gid, Query::SEARCH_PARAM => $search], $limit,
97119
$offset
98120
);
121+
return array_column($data, QUERY::NAME_PARAM, Query::UID_PARAM);
99122
}
100123

101124
/**

0 commit comments

Comments
 (0)