diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-10-16 19:57:31 +0200 |
---|---|---|
committer | Côme Chilliet <come.chilliet@nextcloud.com> | 2023-04-27 12:00:01 +0200 |
commit | a4c599c1c91dcd6e041cbf65198e1f1200513be8 (patch) | |
tree | 9a2a0f0b0ecb4c6775f7382402a147ae8ca110d2 /lib | |
parent | 35dc2235001bf61f07c78b50e74ca029bb9fc05d (diff) | |
download | nextcloud-server-a4c599c1c91dcd6e041cbf65198e1f1200513be8.tar.gz nextcloud-server-a4c599c1c91dcd6e041cbf65198e1f1200513be8.zip |
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Group/Database.php | 57 | ||||
-rw-r--r-- | lib/private/Group/Group.php | 25 | ||||
-rw-r--r-- | lib/private/User/LazyUser.php | 2 | ||||
-rw-r--r-- | lib/private/User/Manager.php | 6 | ||||
-rw-r--r-- | lib/public/Group/Backend/ABackend.php | 6 | ||||
-rw-r--r-- | lib/public/Group/Backend/ISearchableGroupBackend.php | 52 | ||||
-rw-r--r-- | lib/public/GroupInterface.php | 22 |
9 files changed, 82 insertions, 90 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index eb2f3e46354..292e9b7b3af 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -412,6 +412,7 @@ return array( 'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php', 'OCP\\Group\\Backend\\INamedBackend' => $baseDir . '/lib/public/Group/Backend/INamedBackend.php', 'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php', + 'OCP\\Group\\Backend\\ISearchableGroupBackend' => $baseDir . '/lib/public/Group/Backend/ISearchableGroupBackend.php', 'OCP\\Group\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/Group/Backend/ISetDisplayNameBackend.php', 'OCP\\Group\\Events\\BeforeGroupChangedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupChangedEvent.php', 'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 9343231a55b..270d3857ca1 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -445,6 +445,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php', 'OCP\\Group\\Backend\\INamedBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/INamedBackend.php', 'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php', + 'OCP\\Group\\Backend\\ISearchableGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISearchableGroupBackend.php', 'OCP\\Group\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISetDisplayNameBackend.php', 'OCP\\Group\\Events\\BeforeGroupChangedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupChangedEvent.php', 'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php', diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index bfc95c574e2..13e4906c298 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -40,12 +40,12 @@ use OCP\Group\Backend\IDeleteGroupBackend; use OCP\Group\Backend\IGetDisplayNameBackend; use OCP\Group\Backend\IGroupDetailsBackend; use OCP\Group\Backend\IRemoveFromGroupBackend; +use OCP\Group\Backend\ISearchableGroupBackend; use OCP\Group\Backend\ISetDisplayNameBackend; use OCP\Group\Backend\INamedBackend; use OCP\IDBConnection; use OCP\IUserManager; use OC\User\LazyUser; -use OC\User\DisplayNameCache; /** * Class for group management in a SQL Database (e.g. MySQL, SQLite) @@ -60,6 +60,7 @@ class Database extends ABackend implements IGroupDetailsBackend, IRemoveFromGroupBackend, ISetDisplayNameBackend, + ISearchableGroupBackend, INamedBackend { /** @var string[] */ private $groupCache = []; @@ -327,56 +328,15 @@ class Database extends ABackend implements } /** - * get a list of all users in a group + * Get a list of all users in a group * @param string $gid * @param string $search * @param int $limit * @param int $offset - * @return array an array of user ids + * @return array<string> an array of user ids */ - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { - $this->fixDI(); - - $query = $this->dbConn->getQueryBuilder(); - $query->select('g.uid') - ->from('group_user', 'g') - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) - ->orderBy('g.uid', 'ASC'); - - if ($search !== '') { - $query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid')) - ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( - $query->expr()->eq('p.userid', 'u.uid'), - $query->expr()->eq('p.appid', $query->expr()->literal('settings')), - $query->expr()->eq('p.configkey', $query->expr()->literal('email'))) - ) - // sqlite doesn't like re-using a single named parameter here - ->andWhere( - $query->expr()->orX( - $query->expr()->ilike('g.uid', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')), - $query->expr()->ilike('u.displayname', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')), - $query->expr()->ilike('p.configvalue', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')) - ) - ) - ->orderBy('u.uid_lower', 'ASC'); - } - - if ($limit !== -1) { - $query->setMaxResults($limit); - } - if ($offset !== 0) { - $query->setFirstResult($offset); - } - - $result = $query->execute(); - - $users = []; - while ($row = $result->fetch()) { - $users[] = $row['uid']; - } - $result->closeCursor(); - - return $users; + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0): array { + return array_map(fn ($user) => $user->getUid(), $this->searchInGroup($gid, $search, $limit, $offset)); } public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array { @@ -389,7 +349,7 @@ class Database extends ABackend implements ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) ->orderBy('g.uid', 'ASC'); - $query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid')) + $query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid')); if ($search !== '') { $query->leftJoin('u', 'preferences', 'p', $query->expr()->andX( @@ -419,9 +379,8 @@ class Database extends ABackend implements $users = []; $userManager = \OCP\Server::get(IUserManager::class); - $displayNameCache = \OCP\Server::get(DisplayNameCache::class); while ($row = $result->fetch()) { - $users[$row['uid']] = new LazyUser($row['uid'], $displayNameCache, $userManager, $row['displayname'] ?? null); + $users[$row['uid']] = new LazyUser($row['uid'], $userManager, $row['displayname'] ?? null); } $result->closeCursor(); diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php index b2903bcdb26..bfe985b266e 100644 --- a/lib/private/Group/Group.php +++ b/lib/private/Group/Group.php @@ -33,14 +33,16 @@ namespace OC\Group; use OC\Hooks\PublicEmitter; +use OC\User\LazyUser; +use OCP\GroupInterface; use OCP\Group\Backend\ICountDisabledInGroup; use OCP\Group\Backend\IGetDisplayNameBackend; use OCP\Group\Backend\IHideFromCollaborationBackend; use OCP\Group\Backend\INamedBackend; +use OCP\Group\Backend\ISearchableGroupBackend; use OCP\Group\Backend\ISetDisplayNameBackend; use OCP\Group\Events\BeforeGroupChangedEvent; use OCP\Group\Events\GroupChangedEvent; -use OCP\GroupInterface; use OCP\IGroup; use OCP\IUser; use OCP\IUserManager; @@ -248,7 +250,15 @@ class Group implements IGroup { public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array { $users = []; foreach ($this->backends as $backend) { - $users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0)); + if ($backend instanceof ISearchableGroupBackend) { + $users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0)); + } else { + $userIds = $backend->usersInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0); + $userManager = \OCP\Server::get(IUserManager::class); + $users = array_merge($users, array_map(function (string $userId) use ($userManager): IUser { + return new LazyUser($userId, $userManager); + }, $userIds)); + } if (!is_null($limit) and $limit <= 0) { return $users; } @@ -303,18 +313,11 @@ class Group implements IGroup { * @param string $search * @param int $limit * @param int $offset - * @return \OC\User\User[] + * @return IUser[] * @deprecated 25.0.0 Use searchUsers instead (same implementation) */ public function searchDisplayName($search, $limit = null, $offset = null) { - $users = []; - foreach ($this->backends as $backend) { - $users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0)); - if (!is_null($limit) and $limit <= 0) { - return array_values($users); - } - } - return array_values($users); + return $this->searchUsers($search, $limit, $offset); } /** diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index c36ff86eff4..5472cf6f2b4 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -65,7 +65,7 @@ class LazyUser implements IUser { return $this->displayName; } - return $this->userManager->getDisplayName($this->uid); + return $this->userManager->getDisplayName($this->uid) ?? $this->uid; } public function setDisplayName($displayName) { diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index c3275934a5a..dcf9bbab2f3 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -303,12 +303,11 @@ class Manager extends PublicEmitter implements IUserManager { */ public function search($pattern, $limit = null, $offset = null) { $users = []; - $displayNameCache = \OCP\Server::get(DisplayNameCache::class); foreach ($this->backends as $backend) { $backendUsers = $backend->getUsers($pattern, $limit, $offset); if (is_array($backendUsers)) { foreach ($backendUsers as $uid) { - $users[$uid] = new LazyUser($uid, $displayNameCache, $this, null, $backend); + $users[$uid] = new LazyUser($uid, $this, null, $backend); } } } @@ -329,12 +328,11 @@ class Manager extends PublicEmitter implements IUserManager { */ public function searchDisplayName($pattern, $limit = null, $offset = null) { $users = []; - $displayNameCache = \OCP\Server::get(DisplayNameCache::class); foreach ($this->backends as $backend) { $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); if (is_array($backendUsers)) { foreach ($backendUsers as $uid => $displayName) { - $users[] = new LazyUser($uid, $displayNameCache, $this, $displayName, $backend); + $users[] = new LazyUser($uid, $this, $displayName, $backend); } } } diff --git a/lib/public/Group/Backend/ABackend.php b/lib/public/Group/Backend/ABackend.php index e76285dfda9..ee1b62c887b 100644 --- a/lib/public/Group/Backend/ABackend.php +++ b/lib/public/Group/Backend/ABackend.php @@ -29,12 +29,11 @@ use OCP\GroupInterface; use OCP\IUserManager; use OCP\Server; use OC\User\LazyUser; -use OC\User\DisplayNameCache; /** * @since 14.0.0 */ -abstract class ABackend implements GroupInterface { +abstract class ABackend implements GroupInterface, ISearchableGroupBackend { /** * @deprecated 14.0.0 * @since 14.0.0 @@ -72,11 +71,10 @@ abstract class ABackend implements GroupInterface { public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array { // Default implementation for compatibility reasons - $displayNameCache = Server::get(DisplayNameCache::class); $userManager = Server::get(IUserManager::class); $users = []; foreach ($this->usersInGroup($gid, $search, $limit, $offset) as $userId) { - $users[$userId] = new LazyUser($userId, $displayNameCache, $userManager); + $users[$userId] = new LazyUser($userId, $userManager); } return $users; } diff --git a/lib/public/Group/Backend/ISearchableGroupBackend.php b/lib/public/Group/Backend/ISearchableGroupBackend.php new file mode 100644 index 00000000000..4e03680f75a --- /dev/null +++ b/lib/public/Group/Backend/ISearchableGroupBackend.php @@ -0,0 +1,52 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCP\Group\Backend; + +use OCP\IUser; + +/** + * @since 26.0.0 + */ +interface ISearchableGroupBackend { + + /** + * @brief Get a list of users matching the given search parameters. + * + * Implementations of this method should return lazy evaluated user objects and + * preload if possible the display name. + * + * <code> + * $users = $groupBackend->searchInGroup('admin', 'John', 10, 0); + * </code> + * + * @param string $gid The group id of the user we want to search + * @param string $search The part of the display name or user id of the users we + * want to search. This can be empty to get all the users. + * @param int $limit The limit of results + * @param int $offset The offset of the results + * @return IUser[] + * @since 26.0.0 + */ + public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array; +} diff --git a/lib/public/GroupInterface.php b/lib/public/GroupInterface.php index 07fd5cb50c4..7a3c3960541 100644 --- a/lib/public/GroupInterface.php +++ b/lib/public/GroupInterface.php @@ -114,27 +114,7 @@ interface GroupInterface { * @param int $offset * @return array an array of user ids * @since 4.5.0 - * @deprecated 25.0.0 Use searchInGroup instead, for performance reasons + * @deprecated 26.0.0 Use searchInGroup instead, for performance reasons */ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0); - - /** - * @brief Get a list of users matching the given search parameters. - * - * Implementations of this method should return lazy evaluated user objects and - * preload if possible the display name. - * - * <code> - * $users = $groupBackend->searchInGroup('admin', 'John', 10, 0); - * </code> - * - * @param string $gid The group id of the user we want to search - * @param string $search The part of the display name or user id of the users we - * want to search. This can be empty to get all the users. - * @param int $limit The limit of results - * @param int $offset The offset of the results - * @return IUser[] - * @since 25.0.0 - */ - public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array; } |