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/private/Group | |
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/private/Group')
-rw-r--r-- | lib/private/Group/Database.php | 57 | ||||
-rw-r--r-- | lib/private/Group/Group.php | 25 |
2 files changed, 22 insertions, 60 deletions
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); } /** |