Skip to content

Commit c291feb

Browse files
committed
Admin: Give set order to roles in api_get_roles() (by commonality + growing permissions level)
1 parent 6f445de commit c291feb

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6328,17 +6328,32 @@ function api_get_roles(): array
63286328
'USER' => 'User',
63296329
];
63306330

6331+
// Set a fixed order for roles (ANONYMOUS is excluded by get_user_roles())
6332+
$sortedRoles = [
6333+
'STUDENT',
6334+
'TEACHER',
6335+
'SESSION_MANAGER',
6336+
'HR',
6337+
'ADMIN',
6338+
'INVITEE',
6339+
'STUDENT_BOSS',
6340+
'QUESTION_MANAGER',
6341+
'GLOBAL_ADMIN',
6342+
];
6343+
// Make sure any custom role is added at the end
6344+
$diff = array_diff($codes, $sortedRoles);
6345+
$sortedRoles = array_merge($sortedRoles, $diff);
6346+
63316347
$out = [];
6332-
foreach ((array) $codes as $code) {
6333-
$canon = strtoupper(trim((string) $code));
6348+
foreach ($sortedRoles as $code) {
6349+
$canon = strtoupper(trim($code));
63346350
$label =
63356351
($labels[$canon] ?? null) ??
63366352
($labels['ROLE_'.$canon] ?? null) ??
63376353
ucwords(strtolower(str_replace('_', ' ', preg_replace('/^ROLE_/', '', $canon))));
63386354
$out[$code] = $label;
63396355
}
63406356

6341-
ksort($out, SORT_STRING);
63426357
return $cache = $out;
63436358
}
63446359

0 commit comments

Comments
 (0)