Skip to content

Commit 9e67ff9

Browse files
committed
Split new method in a new group backend interface
Better for backward compatibility, also move new interfaces to nc 26 Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent 9385c15 commit 9e67ff9

File tree

9 files changed

+82
-87
lines changed

9 files changed

+82
-87
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@
395395
'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php',
396396
'OCP\\Group\\Backend\\INamedBackend' => $baseDir . '/lib/public/Group/Backend/INamedBackend.php',
397397
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
398+
'OCP\\Group\\Backend\\ISearchableGroupBackend' => $baseDir . '/lib/public/Group/Backend/ISearchableGroupBackend.php',
398399
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
399400
'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
400401
'OCP\\Group\\Events\\BeforeGroupDeletedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupDeletedEvent.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
428428
'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php',
429429
'OCP\\Group\\Backend\\INamedBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/INamedBackend.php',
430430
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
431+
'OCP\\Group\\Backend\\ISearchableGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISearchableGroupBackend.php',
431432
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
432433
'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
433434
'OCP\\Group\\Events\\BeforeGroupDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupDeletedEvent.php',

lib/private/Group/Database.php

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
use OCP\Group\Backend\IGetDisplayNameBackend;
4141
use OCP\Group\Backend\IGroupDetailsBackend;
4242
use OCP\Group\Backend\IRemoveFromGroupBackend;
43+
use OCP\Group\Backend\ISearchableGroupBackend;
4344
use OCP\Group\Backend\ISetDisplayNameBackend;
4445
use OCP\Group\Backend\INamedBackend;
4546
use OCP\IDBConnection;
47+
use OCP\IUser;
4648
use OCP\IUserManager;
4749
use OC\User\LazyUser;
48-
use OC\User\DisplayNameCache;
4950

5051
/**
5152
* Class for group management in a SQL Database (e.g. MySQL, SQLite)
@@ -60,6 +61,7 @@ class Database extends ABackend implements
6061
IGroupDetailsBackend,
6162
IRemoveFromGroupBackend,
6263
ISetDisplayNameBackend,
64+
ISearchableGroupBackend,
6365
INamedBackend {
6466

6567
/** @var string[] */
@@ -328,56 +330,15 @@ public function groupExists($gid) {
328330
}
329331

330332
/**
331-
* get a list of all users in a group
333+
* Get a list of all users in a group
332334
* @param string $gid
333335
* @param string $search
334336
* @param int $limit
335337
* @param int $offset
336-
* @return array an array of user ids
338+
* @return array<string> an array of user ids
337339
*/
338-
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
339-
$this->fixDI();
340-
341-
$query = $this->dbConn->getQueryBuilder();
342-
$query->select('g.uid')
343-
->from('group_user', 'g')
344-
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
345-
->orderBy('g.uid', 'ASC');
346-
347-
if ($search !== '') {
348-
$query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'))
349-
->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
350-
$query->expr()->eq('p.userid', 'u.uid'),
351-
$query->expr()->eq('p.appid', $query->expr()->literal('settings')),
352-
$query->expr()->eq('p.configkey', $query->expr()->literal('email')))
353-
)
354-
// sqlite doesn't like re-using a single named parameter here
355-
->andWhere(
356-
$query->expr()->orX(
357-
$query->expr()->ilike('g.uid', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
358-
$query->expr()->ilike('u.displayname', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
359-
$query->expr()->ilike('p.configvalue', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))
360-
)
361-
)
362-
->orderBy('u.uid_lower', 'ASC');
363-
}
364-
365-
if ($limit !== -1) {
366-
$query->setMaxResults($limit);
367-
}
368-
if ($offset !== 0) {
369-
$query->setFirstResult($offset);
370-
}
371-
372-
$result = $query->execute();
373-
374-
$users = [];
375-
while ($row = $result->fetch()) {
376-
$users[] = $row['uid'];
377-
}
378-
$result->closeCursor();
379-
380-
return $users;
340+
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0): array {
341+
return array_map(fn ($user) => $user->getUid(), $this->searchInGroup($gid, $search, $limit, $offset));
381342
}
382343

383344
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
@@ -390,7 +351,7 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,
390351
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
391352
->orderBy('g.uid', 'ASC');
392353

393-
$query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'))
354+
$query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'));
394355

395356
if ($search !== '') {
396357
$query->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
@@ -420,9 +381,8 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,
420381

421382
$users = [];
422383
$userManager = \OCP\Server::get(IUserManager::class);
423-
$displayNameCache = \OCP\Server::get(DisplayNameCache::class);
424384
while ($row = $result->fetch()) {
425-
$users[$row['uid']] = new LazyUser($row['uid'], $displayNameCache, $userManager, $row['displayname'] ?? null);
385+
$users[$row['uid']] = new LazyUser($row['uid'], $userManager, $row['displayname'] ?? null);
426386
}
427387
$result->closeCursor();
428388

lib/private/Group/Group.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
namespace OC\Group;
3434

3535
use OC\Hooks\PublicEmitter;
36+
use OC\User\LazyUser;
3637
use OCP\Group\Backend\ICountDisabledInGroup;
3738
use OCP\Group\Backend\IGetDisplayNameBackend;
3839
use OCP\Group\Backend\IHideFromCollaborationBackend;
3940
use OCP\Group\Backend\INamedBackend;
4041
use OCP\Group\Backend\ISetDisplayNameBackend;
42+
use OCP\Group\Backend\ISearchableGroupBackend;
4143
use OCP\GroupInterface;
4244
use OCP\IGroup;
4345
use OCP\IUser;
@@ -244,7 +246,15 @@ public function removeUser($user) {
244246
public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array {
245247
$users = [];
246248
foreach ($this->backends as $backend) {
247-
$users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0));
249+
if ($backend instanceof ISearchableGroupBackend) {
250+
$users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0));
251+
} else {
252+
$userIds = $backend->usersInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
253+
$userManager = \OCP\Server::get(IUserManager::class);
254+
$users = array_merge($users, array_map(function (string $userId) use ($userManager): IUser {
255+
return new LazyUser($userId, $userManager);
256+
}, $userIds));
257+
}
248258
if (!is_null($limit) and $limit <= 0) {
249259
return $users;
250260
}
@@ -299,18 +309,11 @@ public function countDisabled() {
299309
* @param string $search
300310
* @param int $limit
301311
* @param int $offset
302-
* @return \OC\User\User[]
312+
* @return IUser[]
303313
* @deprecated 25.0.0 Use searchUsers instead (same implementation)
304314
*/
305315
public function searchDisplayName($search, $limit = null, $offset = null) {
306-
$users = [];
307-
foreach ($this->backends as $backend) {
308-
$users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0));
309-
if (!is_null($limit) and $limit <= 0) {
310-
return array_values($users);
311-
}
312-
}
313-
return array_values($users);
316+
return $this->searchUsers($search, $limit, $offset);
314317
}
315318

316319
/**

lib/private/User/LazyUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getDisplayName() {
6565
return $this->displayName;
6666
}
6767

68-
return $this->userManager->getDisplayName($this->uid);
68+
return $this->userManager->getDisplayName($this->uid) ?? $this->uid;
6969
}
7070

7171
public function setDisplayName($displayName) {

lib/private/User/Manager.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,11 @@ public function checkPasswordNoLogging($loginName, $password) {
301301
*/
302302
public function search($pattern, $limit = null, $offset = null) {
303303
$users = [];
304-
$displayNameCache = \OCP\Server::get(DisplayNameCache::class);
305304
foreach ($this->backends as $backend) {
306305
$backendUsers = $backend->getUsers($pattern, $limit, $offset);
307306
if (is_array($backendUsers)) {
308307
foreach ($backendUsers as $uid) {
309-
$users[$uid] = new LazyUser($uid, $displayNameCache, $this, null, $backend);
308+
$users[$uid] = new LazyUser($uid, $this, null, $backend);
310309
}
311310
}
312311
}
@@ -332,7 +331,7 @@ public function searchDisplayName($pattern, $limit = null, $offset = null) {
332331
$backendUsers = $backend->getDisplayNames($pattern, $limit, $offset);
333332
if (is_array($backendUsers)) {
334333
foreach ($backendUsers as $uid => $displayName) {
335-
$users[] = new LazyUser($uid, $displayNameCache, $this, $displayName, $backend);
334+
$users[] = new LazyUser($uid, $this, $displayName, $backend);
336335
}
337336
}
338337
}

lib/public/Group/Backend/ABackend.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/**
3535
* @since 14.0.0
3636
*/
37-
abstract class ABackend implements GroupInterface {
37+
abstract class ABackend implements GroupInterface, ISearchableGroupBackend {
3838

3939
/**
4040
* @deprecated 14.0.0
@@ -72,11 +72,10 @@ public function implementsActions($actions): bool {
7272

7373
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
7474
// Default implementation for compatibility reasons
75-
$displayNameCache = Server::get(DisplayNameCache::class);
7675
$userManager = Server::get(IUserManager::class);
7776
$users = [];
7877
foreach ($this->usersInGroup($gid, $search, $limit, $offset) as $userId) {
79-
$users[$userId] = new LazyUser($userId, $displayNameCache, $userManager);
78+
$users[$userId] = new LazyUser($userId, $userManager);
8079
}
8180
return $users;
8281
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
namespace OCP\Group\Backend;
25+
26+
use OCP\IUser;
27+
28+
/**
29+
* @since 26.0.0
30+
*/
31+
interface ISearchableGroupBackend {
32+
33+
/**
34+
* @brief Get a list of users matching the given search parameters.
35+
*
36+
* Implementations of this method should return lazy evaluated user objects and
37+
* preload if possible the display name.
38+
*
39+
* <code>
40+
* $users = $groupBackend->searchInGroup('admin', 'John', 10, 0);
41+
* </code>
42+
*
43+
* @param string $gid The group id of the user we want to search
44+
* @param string $search The part of the display name or user id of the users we
45+
* want to search. This can be empty to get all the users.
46+
* @param int $limit The limit of results
47+
* @param int $offset The offset of the results
48+
* @return IUser[]
49+
* @since 26.0.0
50+
*/
51+
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array;
52+
}

lib/public/GroupInterface.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,7 @@ public function groupExists($gid);
115115
* @param int $offset
116116
* @return array an array of user ids
117117
* @since 4.5.0
118-
* @deprecated 25.0.0 Use searchInGroup instead, for performance reasons
118+
* @deprecated 26.0.0 Use searchInGroup instead, for performance reasons
119119
*/
120120
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0);
121-
122-
/**
123-
* @brief Get a list of users matching the given search parameters.
124-
*
125-
* Implementations of this method should return lazy evaluated user objects and
126-
* preload if possible the display name.
127-
*
128-
* <code>
129-
* $users = $groupBackend->searchInGroup('admin', 'John', 10, 0);
130-
* </code>
131-
*
132-
* @param string $gid The group id of the user we want to search
133-
* @param string $search The part of the display name or user id of the users we
134-
* want to search. This can be empty to get all the users.
135-
* @param int $limit The limit of results
136-
* @param int $offset The offset of the results
137-
* @return IUser[]
138-
* @since 25.0.0
139-
*/
140-
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array;
141121
}

0 commit comments

Comments
 (0)