mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
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>
This commit is contained in:
parent
35dc223500
commit
a4c599c1c9
9 changed files with 82 additions and 90 deletions
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
52
lib/public/Group/Backend/ISearchableGroupBackend.php
Normal file
52
lib/public/Group/Backend/ISearchableGroupBackend.php
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue