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

[stable31] Fix disabled user list for SAML group subadmin #51376

Open
wants to merge 3 commits into
base: stable31
Choose a base branch
from
Open
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
18 changes: 6 additions & 12 deletions apps/settings/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class UsersController extends Controller {
/** Limit for counting users for subadmins, to avoid spending too much time */
private const COUNT_LIMIT_FOR_SUBADMINS = 999;

public function __construct(
string $appName,
Expand Down Expand Up @@ -150,20 +152,12 @@ public function usersList(): TemplateResponse {
}, 0);
} else {
// User is subadmin !
// Map group list to ids to retrieve the countDisabledUsersOfGroups
$userGroups = $this->groupManager->getUserGroups($user);
$groupsIds = [];

foreach ($groups as $key => $group) {
// $userCount += (int)$group['usercount'];
$groupsIds[] = $group['id'];
}

$userCount += $this->userManager->countUsersOfGroups($groupsInfo->getGroups());
$disabledUsers = $this->userManager->countDisabledUsersOfGroups($groupsIds);
[$userCount,$disabledUsers] = $this->userManager->countUsersAndDisabledUsersOfGroups($groupsInfo->getGroups(), self::COUNT_LIMIT_FOR_SUBADMINS);
}

$userCount -= $disabledUsers;
if ($disabledUsers > 0) {
$userCount -= $disabledUsers;
}
}

$recentUsersGroup = [
Expand Down
54 changes: 15 additions & 39 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,25 @@ public function countUsersTotal(int $limit = 0, bool $onlyMappedUsers = false):
/**
* returns how many users per backend exist in the requested groups (if supported by backend)
*
* @param IGroup[] $groups an array of gid to search in
* @return array|int an array of backend class as key and count number as value
* if $hasLoggedIn is true only an int is returned
* @param IGroup[] $groups an array of groups to search in
* @param int $limit limit to stop counting
* @return array{int,int} total number of users, and number of disabled users in the given groups, below $limit. If limit is reached, -1 is returned for number of disabled users
*/
public function countUsersOfGroups(array $groups) {
public function countUsersAndDisabledUsersOfGroups(array $groups, int $limit): array {
$users = [];
$disabled = [];
foreach ($groups as $group) {
$usersIds = array_map(function ($user) {
return $user->getUID();
}, $group->getUsers());
$users = array_merge($users, $usersIds);
foreach ($group->getUsers() as $user) {
$users[$user->getUID()] = 1;
if (!$user->isEnabled()) {
$disabled[$user->getUID()] = 1;
}
if (count($users) >= $limit) {
return [count($users),-1];
}
}
}
return count(array_unique($users));
return [count($users),count($disabled)];
}

/**
Expand Down Expand Up @@ -601,36 +607,6 @@ public function countDisabledUsers(): int {
return $count;
}

/**
* returns how many users are disabled in the requested groups
*
* @param array $groups groupids to search
* @return int
* @since 14.0.0
*/
public function countDisabledUsersOfGroups(array $groups): int {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT ' . $queryBuilder->getColumnName('uid') . ')'))
->from('preferences', 'p')
->innerJoin('p', 'group_user', 'g', $queryBuilder->expr()->eq('p.userid', 'g.uid'))
->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled')))
->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)));

$result = $queryBuilder->execute();
$count = $result->fetchOne();
$result->closeCursor();

if ($count !== false) {
$count = (int)$count;
} else {
$count = 0;
}

return $count;
}

/**
* returns how many users have logged in once
*
Expand Down
Loading