Skip to content

Commit 33777d8

Browse files
Internal: Fix role preselection in user_edit: normalize option keys and POST checks
1 parent b61ae79 commit 33777d8

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

public/main/admin/user_edit.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ function confirmation(name) {
264264

265265
$display = 'none';
266266
if (isset($_POST['roles']) && is_array($_POST['roles'])) {
267-
$display = in_array('ROLE_TEACHER', $_POST['roles']) || in_array('ROLE_SESSION_MANAGER', $_POST['roles']) ? 'block' : 'none';
267+
$norm = array_map('api_normalize_role_code', $_POST['roles']);
268+
$display = (in_array('ROLE_TEACHER', $norm, true) || in_array('ROLE_SESSION_MANAGER', $norm, true)) ? 'block' : 'none';
268269
}
269270

270271
// Platform admin
@@ -398,9 +399,22 @@ function confirmation(name) {
398399
$user_data['expiration_date'] = api_get_local_time($expiration_date);
399400
}
400401
}
401-
$availableRoles = array_keys(api_get_roles());
402-
$userRoles = array_intersect($userObj->getRoles(), $availableRoles);
403-
$user_data['roles'] = $userRoles;
402+
403+
$roleOptions = api_get_roles();
404+
$optionKeyByCanon = [];
405+
foreach ($roleOptions as $optKey => $label) {
406+
$optionKeyByCanon[api_normalize_role_code((string) $optKey)] = $optKey;
407+
}
408+
409+
$userCanonRoles = array_map('api_normalize_role_code', (array) $userObj->getRoles());
410+
$selectedOptionKeys = [];
411+
foreach ($userCanonRoles as $canon) {
412+
if (isset($optionKeyByCanon[$canon])) {
413+
$selectedOptionKeys[] = $optionKeyByCanon[$canon];
414+
}
415+
}
416+
417+
$user_data['roles'] = array_values(array_unique($selectedOptionKeys));
404418
$form->setDefaults($user_data);
405419

406420
$error_drh = false;

public/main/inc/lib/api.lib.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6499,8 +6499,6 @@ function api_normalize_role_code(string $code): string {
64996499
'QUESTION_MANAGER' => 'ROLE_QUESTION_MANAGER',
65006500
'ADMIN' => 'ROLE_ADMIN',
65016501
'GLOBAL_ADMIN' => 'ROLE_GLOBAL_ADMIN',
6502-
'SUPER_ADMIN' => 'ROLE_GLOBAL_ADMIN',
6503-
'ROLE_SUPER_ADMIN' => 'ROLE_GLOBAL_ADMIN',
65046502
];
65056503
if (!str_starts_with($c, 'ROLE_')) {
65066504
return $map[$c] ?? ('ROLE_'.$c);

0 commit comments

Comments
 (0)