Roles stage 1: Admin > Roles rebuilt showing each role's access per module as level-colored pills or Admin - everything, its agents with a link to Users filtered by role, search, Current and Archived tabs, and Edit, Copy, Archive, Restore and Delete greyed out with the reason when blocked; one role window for New, Edit and Copy with an Admin switch and a None, Read, Modify or Full level per module, each level listing what it allows, Reports limited to None and Read, and a copy dropping the Built-in description prefix; hardcoded role 3 checks replaced by guards so admin can't be taken off your own role or the only admins' role, archive needs no active agents, delete needs no agents at all, and names are unique in any case with the description optional; Administrator role can now be opened and renamed; role names escaped in flash messages, fixing an XSS; audit log records what changed on a role; lookupUserPermission reads the role's levels once per request instead of one query per check; new functions/roles.php, shared role_form.php and js/role_form.js; database 3.1.6 removes orphaned and duplicate user_role_permissions rows, clamps levels to 3 and adds a primary key on role and module; changelog updated

This commit is contained in:
johnnyq
2026-10-17 20:43:45 -04:00
parent bf89a86ab1
commit 5e87f6fa25
13 changed files with 798 additions and 571 deletions

View File

@@ -3,38 +3,29 @@
// Role and permission enforcement
// Split from the former monolithic functions.php
// When provided a module name (e.g. module_support), returns the associated permission level (false=none, 1=read, 2=write, 3=full)
// When provided a module name (e.g. module_support), returns the associated permission level (false=none, 1=read, 2=write, 3=full).
// The role's levels are read once a request - a page asks dozens of times.
function lookupUserPermission($module) {
global $mysqli, $session_is_admin, $session_user_role;
static $levels = [];
if (isset($session_is_admin) && $session_is_admin === true) {
return 3;
}
$module = escapeSql($module);
$sql = mysqli_query(
$mysqli,
"SELECT
user_role_permissions.user_role_permission_level
FROM
modules
JOIN
user_role_permissions
ON
modules.module_id = user_role_permissions.module_id
WHERE
module_name = '$module' AND user_role_permissions.user_role_id = $session_user_role"
);
$row = mysqli_fetch_assoc($sql);
if (isset($row['user_role_permission_level'])) {
return intval($row['user_role_permission_level']);
$role_id = intval($session_user_role ?? 0);
if (!isset($levels[$role_id])) {
$levels[$role_id] = [];
$sql = mysqli_query($mysqli, "SELECT module_name, MAX(user_role_permission_level) FROM modules
JOIN user_role_permissions ON modules.module_id = user_role_permissions.module_id
WHERE user_role_permissions.user_role_id = $role_id GROUP BY module_name");
while ($row = mysqli_fetch_row($sql)) {
$levels[$role_id][$row[0]] = intval($row[1]);
}
}
// Default return for no module permission
return false;
return !empty($levels[$role_id][$module]) ? $levels[$role_id][$module] : false;
}
// Enforce admin portal access - single canonical admin gate ($session_is_admin)