Skip to content

Commit 3934edb

Browse files
authored
Merge pull request #5093 from christianbeeznest/majorel-21208-2
User: Fix usergroup add user behavior for default visibility settings - refs BT#21208
2 parents 62ba004 + 3e40997 commit 3934edb

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

main/admin/add_users_to_usergroup.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
// setting the name of the tool
2727
$tool_name = get_lang('SubscribeUsersToClass');
28+
$showAllStudentByDefault = api_get_configuration_value('usergroup_add_user_show_all_student_by_default');
2829

2930
$htmlHeadXtra[] = '
3031
<script>
@@ -35,6 +36,11 @@
3536
});
3637
});
3738
39+
function activeUsers(originalUrl) {
40+
var searchValue = document.getElementById("first_letter_user").value;
41+
window.location.href = originalUrl + "&firstLetterUser=" + encodeURIComponent(searchValue);
42+
}
43+
3844
function add_user_to_session (code, content) {
3945
document.getElementById("user_to_add").value = "";
4046
document.getElementById("ajax_list_users_single").innerHTML = "";
@@ -87,7 +93,7 @@ function change_select(reset) {
8793
if (reset) {
8894
document.formulaire["first_letter_user"].value = "";
8995
90-
if ('.(api_get_configuration_value('usergroup_add_user_show_all_student_by_default') ? 0 : 1).') {
96+
if ('.($showAllStudentByDefault ? 0 : 1).') {
9197
document.formulaire["form_sent"].value = "1";
9298
9399
return;
@@ -129,10 +135,10 @@ function change_select(reset) {
129135

130136
$first_letter_user = '';
131137

132-
if (isset($_POST['form_sent']) && $_POST['form_sent']) {
138+
if ((isset($_POST['form_sent']) && $_POST['form_sent']) || isset($_REQUEST['firstLetterUser'])) {
133139
$form_sent = $_POST['form_sent'];
134140
$elements_posted = $_POST['elements_in_name'] ?? null;
135-
$first_letter_user = $_POST['firstLetterUser'];
141+
$first_letter_user = Security::remove_XSS($_REQUEST['firstLetterUser']);
136142

137143
if (!is_array($elements_posted)) {
138144
$elements_posted = [];
@@ -247,7 +253,9 @@ function change_select(reset) {
247253
}
248254

249255
$activeUser = isset($_REQUEST['active_users']) ? (int) $_REQUEST['active_users'] : null;
250-
$conditions['active'] = $activeUser;
256+
if (1 === $activeUser) {
257+
$conditions['active'] = $activeUser;
258+
}
251259

252260
$filterData = [];
253261
if ($searchForm->validate()) {
@@ -268,7 +276,7 @@ function change_select(reset) {
268276
foreach ($list_in as $listedUserId) {
269277
$userInfo = api_get_user_info($listedUserId);
270278

271-
if (isset($activeUser) && ((int) $activeUser != $userInfo['active'])) {
279+
if (1 === $activeUser && empty($userInfo['active'])) {
272280
$hideElementsIn[] = $listedUserId;
273281
continue;
274282
}
@@ -279,9 +287,7 @@ function change_select(reset) {
279287
$user_with_any_group = !empty($_REQUEST['user_with_any_group']);
280288
$user_list = [];
281289

282-
if (!empty($conditions)) {
283-
$user_list = UserManager::getUserListLike($conditions, $order, true, 'OR');
284-
}
290+
$user_list = UserManager::getUserListLike($conditions, $order, true, 'OR');
285291

286292
if ($user_with_any_group) {
287293
$new_user_list = [];
@@ -306,17 +312,16 @@ function change_select(reset) {
306312
continue;
307313
}
308314

309-
if (isset($activeUser) && ((int) $activeUser != $item['active'])) {
310-
continue;
311-
}
312-
313315
if (!in_array($item['user_id'], $list_in)) {
314316
$elements_not_in[$item['user_id']] = formatCompleteName($item, $orderListByOfficialCode);
315317
}
316318
}
317319
}
318320

319-
if (api_get_configuration_value('usergroup_add_user_show_all_student_by_default')
321+
if (!$showAllStudentByDefault && !isset($_POST['firstLetterUser']) && !isset($_REQUEST['active_users'])) {
322+
$elements_not_in = [];
323+
}
324+
if ($showAllStudentByDefault
320325
&& empty($elements_not_in)
321326
&& empty($first_letter_user)
322327
) {
@@ -362,13 +367,13 @@ function formatCompleteName(array $userInfo, bool $orderListByOfficialCode): str
362367
echo '<a href="'.api_get_self().'?id='.$id.'&action=export">'.
363368
Display::return_icon('export_csv.png', get_lang('Export'), [], ICON_SIZE_MEDIUM).'</a>';
364369

365-
$newUrl = api_get_self().'?id='.$id.'&active_users=1';
366-
$buttonLabel = get_lang('OnlyShowActiveUsers');
367-
if ($activeUser) {
368-
$buttonLabel = get_lang('ShowAllUsers') ;
369-
$newUrl = api_get_self().'?id='.$id;
370-
}
371-
echo '<a href="' . htmlspecialchars($newUrl) . '" class="btn btn-default">' . $buttonLabel . '</a>';
370+
$isActiveUser = !empty($activeUser);
371+
$activeUsersParam = $isActiveUser ? '0' : '1';
372+
$newUrl = api_get_self() . '?id=' . $id . '&active_users=' . $activeUsersParam;
373+
$buttonLabelKey = $isActiveUser ? 'ShowAllUsers' : 'OnlyShowActiveUsers';
374+
$buttonLabel = get_lang($buttonLabelKey);
375+
376+
echo '<a href="#" onclick="activeUsers(\'' . htmlspecialchars($newUrl) . '\'); return false;" class="btn btn-default">' . $buttonLabel . '</a>';
372377

373378
echo '</div>';
374379

main/inc/lib/usermanager.lib.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,13 @@ public static function getUserListLike(
24072407

24082408
$sql_query .= ' WHERE 1 = 1 ';
24092409
if (count($conditions) > 0) {
2410+
2411+
$andActive = "";
2412+
if (isset($conditions['active'])) {
2413+
$andActive = " AND active = " . (int) $conditions['active'];
2414+
unset($conditions['active']);
2415+
}
2416+
24102417
$temp_conditions = [];
24112418
foreach ($conditions as $field => $value) {
24122419
$field = Database::escape_string($field);
@@ -2418,12 +2425,13 @@ public static function getUserListLike(
24182425
}
24192426
}
24202427
if (!empty($temp_conditions)) {
2421-
$sql_query .= ' AND '.implode(' '.$condition.' ', $temp_conditions);
2428+
$sql_query .= ' AND ('.implode(' '.$condition.' ', $temp_conditions).') ';
24222429
}
24232430

24242431
if (api_is_multiple_url_enabled()) {
24252432
$sql_query .= ' AND auru.access_url_id = '.api_get_current_access_url_id();
24262433
}
2434+
$sql_query .= $andActive;
24272435
} else {
24282436
if (api_is_multiple_url_enabled()) {
24292437
$sql_query .= ' AND auru.access_url_id = '.api_get_current_access_url_id();
@@ -8216,7 +8224,7 @@ private static function getGravatar(
82168224
* @param int user_id id of the user for whom we need the hash
82178225
*
82188226
* @return string containing the hash
8219-
*/
8227+
*/
82208228
public static function generateUserHash(int $user_id): string
82218229
{
82228230
$currentUserId = api_get_user_id();
@@ -8235,7 +8243,7 @@ public static function generateUserHash(int $user_id): string
82358243
* @param string hash hash that is to be decrypted
82368244
*
82378245
* @return string
8238-
*/
8246+
*/
82398247
public static function decryptUserHash(string $hash): string
82408248
{
82418249
$currentUserId = api_get_user_id();

0 commit comments

Comments
 (0)