Commit 02d5687b authored by Quentin Ribac's avatar Quentin Ribac
Browse files

FEAT #17436 TIME 0:15 UserController::get(): single db call when whole table is read

parent 3fae749a
......@@ -68,6 +68,8 @@ class UserController
$currentUser = UserModel::getById(['select' => ['"isRest"'], 'id' => $GLOBALS['id']]);
$manageableGroups = UserController::getManageableGroups(['userId' => $GLOBALS['id']]);
$manageableGroups = array_column($manageableGroups, 'id');
$allGroupLabels = GroupModel::get(['select' => ['label', 'id']]);
$allUsersGroups = UserGroupModel::get(['select' => ['user_id', 'group_id']]);
foreach ($users as $key => $user) {
$users[$key]['substitute'] = !empty($user['substitute']);
......@@ -77,8 +79,9 @@ class UserController
unset($users[$key]['x509_fingerprint']);
$users[$key]['groups'] = [];
$groupsIds = UserGroupModel::get(['select' => ['group_id'], 'where' => ['user_id = ?'], 'data' => [$user['id']]]);
$groupsIds = array_column($groupsIds, 'group_id');
$groupsIds = array_column(array_filter($allUsersGroups, function ($userGroup) use ($user) {
return $userGroup['user_id'] == $user['id'];
}), 'group_id');
$actuallyAlone = false;
if (empty($groupsIds)) {
$actuallyAlone = true;
......@@ -86,8 +89,9 @@ class UserController
$groupsIds = array_values(array_intersect($groupsIds, $manageableGroups));
}
if (!empty($groupsIds)) {
$groups = GroupModel::get(['select' => ['label', 'id'], 'where' => ['id in (?)'], 'data' => [$groupsIds]]);
$users[$key]['groups'] = $groups;
$users[$key]['groups'] = array_filter($allGroupLabels, function ($group) use ($groupsIds) {
return in_array($group['id'], $groupsIds);
});
} elseif (!$actuallyAlone) {
unset($users[$key]);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment