Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Generator for users in searchInGroups, type it as iterable #38026

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Group_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function getBackendName(): string {
return 'LDAP';
}

public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): iterable {
return $this->handleRequest($gid, 'searchInGroup', [$gid, $search, $limit, $offset]);
}
}
13 changes: 8 additions & 5 deletions lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,15 @@ public function groupExists($gid) {
* @return array<int,string> an array of user ids
*/
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0): array {
return array_values(array_map(fn ($user) => $user->getUid(), $this->searchInGroup($gid, $search, $limit, $offset)));
$userIds = [];
$users = $this->searchInGroup($gid, $search, $limit, $offset);
foreach ($users as $userId => $user) {
$userIds[] = $userId;
}
return $userIds;
}

public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): iterable {
$this->fixDI();

$query = $this->dbConn->getQueryBuilder();
Expand Down Expand Up @@ -380,11 +385,9 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,
$users = [];
$userManager = \OCP\Server::get(IUserManager::class);
while ($row = $result->fetch()) {
$users[$row['uid']] = new LazyUser($row['uid'], $userManager, $row['displayname'] ?? null);
yield $row['uid'] => new LazyUser($row['uid'], $userManager, $row['displayname'] ?? null);
}
$result->closeCursor();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not get called if the Generator is not exhausted by the caller, so this approach may be problematic.
Anyone have a better idea on how to do that?


return $users;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ public function searchUsers(string $search, ?int $limit = null, ?int $offset = n
$users = [];
foreach ($this->backends as $backend) {
if ($backend instanceof ISearchableGroupBackend) {
$users += $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
$backendUsers = $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
foreach ($backendUsers as $userId => $user) {
if (!isset($users[$userId])) {
$users[$userId] = $user;
}
}
} else {
$userIds = $backend->usersInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
$userManager = \OCP\Server::get(IUserManager::class);
Expand Down
4 changes: 2 additions & 2 deletions lib/public/Group/Backend/ISearchableGroupBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ interface ISearchableGroupBackend {
* 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 array<string,IUser> Users indexed by uid
* @return iterable<string,IUser> Users indexed by uid
* @since 27.0.0
*/
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array;
public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): iterable;
}