Skip to content

Commit

Permalink
Split new method in a new group backend interface
Browse files Browse the repository at this point in the history
Better for backward compatibility, also move new interfaces to nc 26

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Oct 16, 2022
1 parent 9385c15 commit 0544e8c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 88 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@
'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\\BeforeGroupCreatedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
'OCP\\Group\\Events\\BeforeGroupDeletedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupDeletedEvent.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,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\\BeforeGroupCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
'OCP\\Group\\Events\\BeforeGroupDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupDeletedEvent.php',
Expand Down
57 changes: 8 additions & 49 deletions lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
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)
Expand All @@ -60,6 +60,7 @@ class Database extends ABackend implements
IGroupDetailsBackend,
IRemoveFromGroupBackend,
ISetDisplayNameBackend,
ISearchableGroupBackend,
INamedBackend {

/** @var string[] */
Expand Down Expand Up @@ -328,56 +329,15 @@ public function groupExists($gid) {
}

/**
* 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 {
Expand All @@ -390,7 +350,7 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,
->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(
Expand Down Expand Up @@ -420,9 +380,8 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,

$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();

Expand Down
23 changes: 13 additions & 10 deletions lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
namespace OC\Group;

use OC\Hooks\PublicEmitter;
use OC\User\LazyUser;
use OCP\Group\Backend\ICountDisabledInGroup;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\IHideFromCollaborationBackend;
use OCP\Group\Backend\INamedBackend;
use OCP\Group\Backend\ISetDisplayNameBackend;
use OCP\Group\Backend\ISearchableGroupBackend;
use OCP\GroupInterface;
use OCP\IGroup;
use OCP\IUser;
Expand Down Expand Up @@ -244,7 +246,15 @@ public function removeUser($user) {
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;
}
Expand Down Expand Up @@ -299,18 +309,11 @@ public function countDisabled() {
* @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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/User/LazyUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getDisplayName() {
return $this->displayName;
}

return $this->userManager->getDisplayName($this->uid);
return $this->userManager->getDisplayName($this->uid) ?? $this->uid;
}

public function setDisplayName($displayName) {
Expand Down
5 changes: 2 additions & 3 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,11 @@ public function checkPasswordNoLogging($loginName, $password) {
*/
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);
}
}
}
Expand All @@ -332,7 +331,7 @@ public function searchDisplayName($pattern, $limit = null, $offset = null) {
$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);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions lib/public/Group/Backend/ABackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
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
Expand Down Expand Up @@ -72,11 +71,10 @@ public function implementsActions($actions): bool {

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;
}
Expand Down
52 changes: 52 additions & 0 deletions lib/public/Group/Backend/ISearchableGroupBackend.php
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 1 addition & 21 deletions lib/public/GroupInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,7 @@ public function groupExists($gid);
* @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;
}

0 comments on commit 0544e8c

Please sign in to comment.