mirror of
https://github.com/itflow-org/itflow
synced 2026-11-24 13:27:27 +00:00
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:
25
admin/database_updates/3.1.6.php
Normal file
25
admin/database_updates/3.1.6.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 3.1.6 (from 3.1.5)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
// A role's level for each module - one row each. Rows left behind by a
|
||||
// role or module that is gone, or of no access, are dropped; a module a
|
||||
// role has twice keeps its highest level, held to 1 to 3. Then a primary
|
||||
// key, so it can only have one.
|
||||
mysqli_query($mysqli, "DELETE FROM user_role_permissions
|
||||
WHERE user_role_permission_level <= 0
|
||||
OR user_role_id NOT IN (SELECT role_id FROM user_roles)
|
||||
OR module_id NOT IN (SELECT module_id FROM modules)");
|
||||
mysqli_query($mysqli, "CREATE TEMPORARY TABLE role_permissions_kept AS
|
||||
SELECT user_role_id, module_id, LEAST(MAX(user_role_permission_level), 3) AS kept_level
|
||||
FROM user_role_permissions GROUP BY user_role_id, module_id");
|
||||
mysqli_query($mysqli, "DELETE FROM user_role_permissions");
|
||||
mysqli_query($mysqli, "INSERT INTO user_role_permissions (user_role_id, module_id, user_role_permission_level)
|
||||
SELECT user_role_id, module_id, kept_level FROM role_permissions_kept");
|
||||
mysqli_query($mysqli, "DROP TEMPORARY TABLE role_permissions_kept");
|
||||
mysqli_query($mysqli, "ALTER TABLE `user_role_permissions` ADD PRIMARY KEY (`user_role_id`, `module_id`)");
|
||||
@@ -1,199 +1,44 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* New Role - from nothing, or a copy of another (?copy=role_id) to start
|
||||
* from its levels. The fields are role_form.php.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$copy_of = isset($_GET['copy']) ? roleRow(intval($_GET['copy'])) : null;
|
||||
$role = null;
|
||||
if ($copy_of) {
|
||||
$role = $copy_of;
|
||||
$role['role_name'] = 'Copy of ' . $copy_of['role_name'];
|
||||
// A copy isn't built in
|
||||
$role['role_description'] = preg_replace('/^Built-in - /', '', (string) $copy_of['role_description']);
|
||||
}
|
||||
$role_admin_blocker = '';
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-user-shield me-2"></i>New Role</h5>
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-user-shield me-2"></i><?= $copy_of ? 'Copy ' . escapeHtml($copy_of['role_name']) : 'New Role' ?></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
|
||||
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<ul class="nav nav-pills nav-justified mb-3">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" data-bs-toggle="pill" href="#pills-role-details">Details</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" data-bs-toggle="pill" href="#pills-role-permissions">Permissions</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
||||
<div class="tab-content">
|
||||
|
||||
<!-- DETAILS TAB -->
|
||||
<div class="tab-pane fade show active" id="pills-role-details">
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-user-shield"></i></span>
|
||||
<input type="text" class="form-control" name="role_name" placeholder="Role Name" maxlength="200" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Description <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-chevron-right"></i></span>
|
||||
<input type="text" class="form-control" name="role_description" placeholder="Role Description" maxlength="200" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Admin Access <strong class="text-danger">*</strong></label>
|
||||
|
||||
<div class="form-check mb-2">
|
||||
<input type="radio" class="form-check-input" id="admin_no" name="role_is_admin" value="0" checked required>
|
||||
<label class="form-check-label" for="admin_no">
|
||||
No - use permissions on the next tab
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="form-check">
|
||||
<input type="radio" class="form-check-input" id="admin_yes" name="role_is_admin" value="1" required>
|
||||
<label class="form-check-label" for="admin_yes">
|
||||
Yes - this role should have full admin access
|
||||
</label>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- PERMISSIONS TAB -->
|
||||
<div class="tab-pane fade" id="pills-role-permissions">
|
||||
|
||||
<?php
|
||||
// Enumerate modules
|
||||
$sql_modules = mysqli_query($mysqli, "SELECT module_description, module_id, module_name FROM modules");
|
||||
while ($row_modules = mysqli_fetch_assoc($sql_modules)) {
|
||||
|
||||
$module_id = intval($row_modules['module_id']);
|
||||
|
||||
// raw for name, escaped for display
|
||||
$module_name_raw = $row_modules['module_name'];
|
||||
$module_name_display = ucfirst(str_replace("module_", "", $module_name_raw));
|
||||
|
||||
$module_name_display_safe = escapeHtml($module_name_display);
|
||||
$module_description = escapeHtml($row_modules['module_description']);
|
||||
|
||||
// default for new role
|
||||
$module_permission = 0;
|
||||
|
||||
$field_name = $module_id . "##" . $module_name_raw;
|
||||
$group_id = "perm_group_$module_id";
|
||||
?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label><?= $module_name_display_safe ?> <strong class="text-danger">*</strong></label>
|
||||
|
||||
<div class="btn-group w-100" role="group"
|
||||
aria-label="Permissions for <?= $module_name_display_safe ?>">
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_0"
|
||||
value="0"
|
||||
autocomplete="off"
|
||||
checked
|
||||
required
|
||||
>
|
||||
<label class="btn btn-outline-secondary btn-sm" title="No Access" for="<?= $group_id ?>_0">None</label>
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_1"
|
||||
value="1"
|
||||
autocomplete="off"
|
||||
>
|
||||
<label class="btn btn-outline-primary btn-sm" title="Viewing Only" for="<?= $group_id ?>_1"><i class="fas fa-fw fa-eye me-1"></i>Read</label>
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_2"
|
||||
value="2"
|
||||
autocomplete="off"
|
||||
>
|
||||
<label class="btn btn-outline-warning btn-sm" title="Read, Edit, Archive" for="<?= $group_id ?>_2"><i class="fas fa-fw fa-edit me-1"></i>Modify</label>
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_3"
|
||||
value="3"
|
||||
autocomplete="off"
|
||||
>
|
||||
<label class="btn btn-outline-danger btn-sm" title="Read, Edit, Archive, Delete" for="<?= $group_id ?>_3"><i class="fas fa-fw fa-trash me-1"></i>Full</label>
|
||||
|
||||
</div>
|
||||
|
||||
<small class="form-text text-muted mt-2"><?= $module_description ?></small>
|
||||
</div>
|
||||
|
||||
<?php } // end while ?>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<?php if ($copy_of) { ?>
|
||||
<div class="alert alert-secondary small py-2"><i class="fas fa-fw fa-copy me-1"></i>A new role, starting from <?= escapeHtml($copy_of['role_name']) ?>'s access - its agents keep theirs.</div>
|
||||
<?php } ?>
|
||||
<?php require __DIR__ . '/role_form.php'; ?>
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="add_role" class="btn btn-primary text-bold">
|
||||
<i class="fas fa-check me-2"></i>Create
|
||||
</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal">
|
||||
<i class="fas fa-times me-2"></i>Cancel
|
||||
</button>
|
||||
<button type="submit" name="add_role" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Create role</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
// Optional: when Admin Yes is selected, disable permission radios + switch to Details tab
|
||||
(function () {
|
||||
function setPermissionsEnabled(enabled) {
|
||||
const permsTab = document.getElementById('pills-role-permissions');
|
||||
if (!permsTab) return;
|
||||
|
||||
permsTab.querySelectorAll('input[type="radio"]').forEach(function (el) {
|
||||
el.disabled = !enabled;
|
||||
});
|
||||
|
||||
// also visually dim the tab content
|
||||
permsTab.style.opacity = enabled ? '1' : '0.5';
|
||||
}
|
||||
|
||||
const adminYes = document.getElementById('admin_yes');
|
||||
const adminNo = document.getElementById('admin_no');
|
||||
|
||||
function refresh() {
|
||||
const isAdmin = adminYes && adminYes.checked;
|
||||
setPermissionsEnabled(!isAdmin);
|
||||
|
||||
if (isAdmin) {
|
||||
// move user back to Details tab (avoids confusion)
|
||||
const detailsTab = document.querySelector('a[href="#pills-role-details"]');
|
||||
if (detailsTab) detailsTab.click();
|
||||
}
|
||||
}
|
||||
|
||||
if (adminYes && adminNo) {
|
||||
adminYes.addEventListener('change', refresh);
|
||||
adminNo.addEventListener('change', refresh);
|
||||
refresh();
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
|
||||
@@ -1,196 +1,44 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Edit a role - its name, description, whether it is an admin role, and
|
||||
* its level of each module. The fields are role_form.php.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$role_id = intval($_GET['id']);
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM user_roles WHERE role_id = $role_id LIMIT 1");
|
||||
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$role_name = escapeHtml($row['role_name']);
|
||||
$role_description = escapeHtml($row['role_description']);
|
||||
$role_admin = intval($row['role_is_admin']);
|
||||
|
||||
// Count number of users that have each role
|
||||
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(user_id) FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
|
||||
$role_user_count = mysqli_fetch_row($sql_role_user_count)[0];
|
||||
|
||||
$sql_users = mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
|
||||
// Initialize an empty array to hold user names
|
||||
$user_names = [];
|
||||
|
||||
// Fetch each row and store the user_name in the array
|
||||
while($row = mysqli_fetch_assoc($sql_users)) {
|
||||
$user_names[] = escapeHtml($row['user_name']);
|
||||
}
|
||||
|
||||
// Convert the array of user names to a comma-separated string
|
||||
$user_names_string = implode(",", $user_names) ;
|
||||
|
||||
if (empty($user_names_string)) {
|
||||
$user_names_string = "-";
|
||||
}
|
||||
$role = roleRow(intval($_GET['id'] ?? 0));
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
if (!$role) { ?>
|
||||
<div class="modal-body text-center text-secondary py-5">That role is gone - it may have been deleted.</div>
|
||||
<?php } else {
|
||||
$role_id = intval($role['role_id']);
|
||||
$role_admin_blocker = roleAdminBlocker($role);
|
||||
$agent_count = intval($role['agents']); ?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-user-shield me-2"></i>Editing role:
|
||||
<strong><?= $role_name ?></strong></h5>
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-user-shield me-2"></i>Role: <strong><?= escapeHtml($role['role_name']) ?></strong></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="role_id" value="<?= $role_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<ul class="nav nav-pills nav-justified mb-3">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" data-bs-toggle="pill" href="#pills-role-details">Details</a>
|
||||
</li>
|
||||
<?php if (!$role_admin) { ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" data-bs-toggle="pill" href="#pills-role-permissions">Permissions</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
||||
<div class="tab-content">
|
||||
|
||||
<div class="tab-pane fade show active" id="pills-role-details">
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-user-shield"></i></span>
|
||||
<input type="text" class="form-control" name="role_name" placeholder="Role Name" maxlength="200" value="<?= $role_name ?>" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Description <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-chevron-right"></i></span>
|
||||
<input type="text" class="form-control" name="role_description" placeholder="Role Description" maxlength="200" value="<?= $role_description ?>" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Admin Access <strong class="text-danger">*</strong></label>
|
||||
<div class="form-check mb-2">
|
||||
<input type="radio" class="form-check-input" id="admin_yes" name="role_is_admin" value="1"
|
||||
<?php if ($role_admin) { echo 'checked'; } ?> required>
|
||||
<label class="form-check-label" for="admin_yes">
|
||||
Yes - this role should have full admin access
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="form-check">
|
||||
<input type="radio" class="form-check-input" id="admin_no" name="role_is_admin" value="0"
|
||||
<?php if (!$role_admin) { echo 'checked'; } ?> required>
|
||||
<label class="form-check-label" for="admin_no">
|
||||
No - use permissions on the next tab
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<?php if (!$role_admin) { ?>
|
||||
<div class="tab-pane fade" id="pills-role-permissions">
|
||||
|
||||
<?php if ($role_admin) { ?>
|
||||
<div class="alert alert-warning"><strong>Module permissions do not apply to Admins.</strong></div>
|
||||
<?php } ?>
|
||||
|
||||
<?php
|
||||
|
||||
// Enumerate modules
|
||||
$sql_modules = mysqli_query($mysqli, "SELECT module_description, module_id, module_name FROM modules");
|
||||
while ($row_modules = mysqli_fetch_assoc($sql_modules)) {
|
||||
$module_id = intval($row_modules['module_id']);
|
||||
$module_name = escapeHtml($row_modules['module_name']);
|
||||
$module_name_display = ucfirst(str_replace("module_","",$module_name));
|
||||
$module_description = escapeHtml($row_modules['module_description']);
|
||||
|
||||
// Get permission level for module
|
||||
$module_permission_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT user_role_permission_level FROM user_role_permissions WHERE module_id = $module_id AND user_role_id = $role_id LIMIT 1"));
|
||||
$module_permission = 0;
|
||||
if ($module_permission_row) {
|
||||
$module_permission = $module_permission_row['user_role_permission_level'];
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label> <?= $module_name_display ?> <strong class="text-danger">*</strong></label>
|
||||
<?php
|
||||
$field_name = "$module_id##$module_name";
|
||||
$group_id = "perm_group_$module_id";
|
||||
?>
|
||||
|
||||
<div class="btn-group w-100" role="group" aria-label="Permissions for <?= $module_name_display ?>">
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_0"
|
||||
value="0"
|
||||
autocomplete="off"
|
||||
<?php if ($module_permission == 0) { echo 'checked'; } ?>
|
||||
required
|
||||
>
|
||||
<label class="btn btn-outline-secondary btn-sm" title="No Access" for="<?= $group_id ?>_0">None</label>
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_1"
|
||||
value="1"
|
||||
autocomplete="off"
|
||||
<?php if ($module_permission == 1) { echo 'checked'; } ?>
|
||||
>
|
||||
<label class="btn btn-outline-primary btn-sm" title="Viewing Only" for="<?= $group_id ?>_1"><i class="fas fa-fw fa-eye me-1"></i>Read</label>
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_2"
|
||||
value="2"
|
||||
autocomplete="off"
|
||||
<?php if ($module_permission == 2) { echo 'checked'; } ?>
|
||||
>
|
||||
<label class="btn btn-outline-warning btn-sm" title="Read, Edit, Archive" for="<?= $group_id ?>_2"><i class="fas fa-fw fa-edit me-1"></i>Modify</label>
|
||||
|
||||
<input class="btn-check"
|
||||
type="radio"
|
||||
name="<?= $field_name ?>"
|
||||
id="<?= $group_id ?>_3"
|
||||
value="3"
|
||||
autocomplete="off"
|
||||
<?php if ($module_permission == 3) { echo 'checked'; } ?>
|
||||
>
|
||||
<label class="btn btn-outline-danger btn-sm" title="Read, Edit, Archive, Delete" for="<?= $group_id ?>_3"><i class="fas fa-fw fa-trash me-1"></i>Full</label>
|
||||
|
||||
</div>
|
||||
|
||||
<small class="form-text text-muted mt-2"><?= $module_description ?></small>
|
||||
|
||||
</div>
|
||||
|
||||
<?php } // End while ?>
|
||||
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
|
||||
<?php if ($role['role_archived_at']) { ?>
|
||||
<div class="alert alert-secondary small py-2"><i class="fas fa-fw fa-archive me-1"></i>Archived <?= date('M j, Y', strtotime($role['role_archived_at'])) ?> - kept for the agents archived with it, but not given to anyone new.</div>
|
||||
<?php } elseif ($agent_count) { ?>
|
||||
<div class="small text-secondary mb-3"><i class="fas fa-fw fa-users me-1"></i>A change applies to its <?= $agent_count ?> agent<?= $agent_count === 1 ? '' : 's' ?> from the next page they open.</div>
|
||||
<?php } ?>
|
||||
<?php require __DIR__ . '/role_form.php'; ?>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="edit_role" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Save</button>
|
||||
<button type="submit" name="edit_role" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Save changes</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
<?php }
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
|
||||
87
admin/modals/role/role_form.php
Normal file
87
admin/modals/role/role_form.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* A role's fields - for role_add.php (new, or a copy) and role_edit.php,
|
||||
* which set $role (a roleRow() to fill from, or null) and $role_admin_blocker
|
||||
* (why it can't stop being an admin role, '' when it can). An admin role
|
||||
* opens everything; any other has a level of each module, each showing what
|
||||
* it lets them do. js/role_form.js hides the levels for an admin role and
|
||||
* ticks off what a level allows as it is picked.
|
||||
*/
|
||||
|
||||
isset($mysqli) || die("Direct file access is not allowed");
|
||||
|
||||
$r = $role ?? null;
|
||||
$r_admin = $r ? intval($r['role_is_admin']) : 0;
|
||||
$r_levels = $r['levels'] ?? [];
|
||||
$r_admin_locked = ($role_admin_blocker ?? '') !== '';
|
||||
|
||||
?>
|
||||
|
||||
<div data-role-form>
|
||||
<div class="mb-3">
|
||||
<label>Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-user-tag"></i></span>
|
||||
<input type="text" class="form-control" name="name" maxlength="200" placeholder="Help Desk, Billing, Field Tech" value="<?= escapeHtml($r['role_name'] ?? '') ?>" required autofocus>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Description</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-align-left"></i></span>
|
||||
<input type="text" class="form-control" name="description" maxlength="200" placeholder="Who it is for" value="<?= escapeHtml($r['role_description'] ?? '') ?>">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="form-check form-switch">
|
||||
<input class="form-check-input" type="checkbox" role="switch" name="admin" value="1" id="roleAdmin" data-role-admin<?= $r_admin ? ' checked' : '' ?><?= $r_admin_locked ? ' disabled' : '' ?>>
|
||||
<label class="form-check-label fw-semibold" for="roleAdmin"><i class="fas fa-fw fa-user-shield me-1"></i>Admin</label>
|
||||
<div class="form-text mt-0">Every part of ITFlow, every client, and Admin - users, roles and settings.</div>
|
||||
<?php if ($r_admin_locked) { ?>
|
||||
<input type="hidden" name="admin" value="1">
|
||||
<div class="form-text mt-0 text-warning-emphasis"><i class="fas fa-fw fa-lock me-1"></i>It stays an admin role: <?= escapeHtml($role_admin_blocker) ?></div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div data-role-levels<?= $r_admin ? ' hidden' : '' ?>>
|
||||
<label class="mb-2">What its agents can open</label>
|
||||
<?php foreach (roleModules() as $module_id => $module) {
|
||||
$level = min(intval($r_levels[$module_id] ?? 0), $module['max']);
|
||||
$label = escapeHtml($module['label']); ?>
|
||||
<div class="border rounded p-2 mb-2" data-role-module>
|
||||
<div class="d-flex flex-wrap align-items-center gap-2">
|
||||
<div class="d-flex align-items-center gap-2 flex-grow-1 min-w-0">
|
||||
<span class="itflow-asset-icon itflow-asset-icon-sm itflow-asset-tone-other" aria-hidden="true"><i class="fas fa-fw <?= $module['icon'] ?>"></i></span>
|
||||
<div class="min-w-0">
|
||||
<div class="fw-semibold"><?= $label ?></div>
|
||||
<div class="small text-secondary"><?= escapeHtml($module['about']) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="btn-group btn-group-sm" role="group" aria-label="<?= $label ?>">
|
||||
<?php foreach (ROLE_LEVELS as $value => $level_label) {
|
||||
if ($value > $module['max']) {
|
||||
break;
|
||||
}
|
||||
$id = "roleLevel{$module_id}_$value"; ?>
|
||||
<input class="btn-check" type="radio" name="levels[<?= $module_id ?>]" id="<?= $id ?>" value="<?= $value ?>" autocomplete="off"<?= $level === $value ? ' checked' : '' ?>>
|
||||
<label class="btn btn-outline-<?= ROLE_LEVEL_TONES[$value] ?>" for="<?= $id ?>"><?= $level_label ?></label>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="list-unstyled small mb-0 mt-2 itflow-role-level-list">
|
||||
<?php foreach ($module['levels'] as $value => $text) {
|
||||
$on = $value <= $level; ?>
|
||||
<li data-role-level="<?= $value ?>"<?= $on ? '' : ' class="is-off"' ?>><i class="fas fa-fw <?= $on ? 'fa-check text-success' : 'fa-times' ?> me-1" aria-hidden="true"></i><?= escapeHtml($text) ?></li>
|
||||
<?php } ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="small text-secondary" data-role-admin-note<?= $r_admin ? '' : ' hidden' ?>><i class="fas fa-fw fa-info-circle me-1"></i>An admin role opens every part of ITFlow, so there are no levels to set. Its levels are kept, for if it stops being one.</div>
|
||||
</div>
|
||||
|
||||
<script src="/js/role_form.js"></script>
|
||||
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - GET/POST request handler for roles
|
||||
* Roles (Admin > Roles) - making, changing, copying, archiving, restoring
|
||||
* and deleting them. The work is in functions/roles.php.
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
@@ -10,31 +11,21 @@ if (isset($_POST['add_role'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$name = escapeSql($_POST['role_name']);
|
||||
$description = escapeSql($_POST['role_description']);
|
||||
$admin = intval($_POST['role_is_admin']);
|
||||
|
||||
mysqli_query($mysqli, "INSERT INTO user_roles SET role_name = '$name', role_description = '$description', role_is_admin = $admin");
|
||||
|
||||
$role_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// Insert role permissions (only if not admin)
|
||||
if ($admin == 0) {
|
||||
foreach ($_POST as $key => $value) {
|
||||
if (str_contains($key, '##module_')) {
|
||||
$module_id = intval(explode('##', $key)[0]);
|
||||
$access_level = intval($value);
|
||||
|
||||
if ($access_level > 0) {
|
||||
mysqli_query($mysqli, "INSERT INTO user_role_permissions SET user_role_id = $role_id, module_id = $module_id, user_role_permission_level = $access_level");
|
||||
}
|
||||
}
|
||||
}
|
||||
[$fields, $error] = roleFromInput($_POST);
|
||||
if ($error !== '') {
|
||||
flashAlert(escapeHtml($error), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
logAudit("User Role", "Create", "$session_name created user role $name", 0, $role_id);
|
||||
$role_id = roleSave($fields);
|
||||
|
||||
flashAlert("User Role <strong>$name</strong> created");
|
||||
$access = $fields['admin'] ? 'an admin role' : (implode(', ', array_filter(array_map(function ($module_id) use ($fields) {
|
||||
$level = $fields['levels'][$module_id];
|
||||
return $level ? roleModules()[$module_id]['label'] . ' ' . ROLE_LEVELS[$level] : '';
|
||||
}, array_keys($fields['levels'])))) ?: 'no access');
|
||||
logAudit("User Role", "Create", "$session_name created role " . escapeSql($fields['name']) . " - " . escapeSql($access), 0, $role_id);
|
||||
|
||||
flashAlert("Role <strong>" . escapeHtml($fields['name']) . "</strong> created");
|
||||
|
||||
redirect();
|
||||
|
||||
@@ -44,30 +35,30 @@ if (isset($_POST['edit_role'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$role_id = intval($_POST['role_id']);
|
||||
$name = escapeSql($_POST['role_name']);
|
||||
$description = escapeSql($_POST['role_description']);
|
||||
$admin = intval($_POST['role_is_admin']);
|
||||
|
||||
mysqli_query($mysqli, "UPDATE user_roles SET role_name = '$name', role_description = '$description', role_is_admin = $admin WHERE role_id = $role_id");
|
||||
|
||||
// Update role access levels
|
||||
mysqli_query($mysqli, "DELETE FROM user_role_permissions WHERE user_role_id = $role_id");
|
||||
foreach ($_POST as $key => $value) {
|
||||
if (str_contains($key, '##module_')){
|
||||
$module_id = intval(explode('##', $key)[0]);
|
||||
$access_level = intval($value);
|
||||
|
||||
if ($access_level > 0) {
|
||||
mysqli_query($mysqli, "INSERT INTO user_role_permissions SET user_role_id = $role_id, module_id = $module_id, user_role_permission_level = $access_level");
|
||||
}
|
||||
}
|
||||
$row = roleRow(intval($_POST['role_id'] ?? 0));
|
||||
if (!$row) {
|
||||
flashAlert("That role is gone - it may have been deleted", 'error');
|
||||
redirect();
|
||||
}
|
||||
$role_id = intval($row['role_id']);
|
||||
|
||||
[$fields, $error] = roleFromInput($_POST, $role_id);
|
||||
if ($error === '' && intval($row['role_is_admin']) && !$fields['admin'] && ($blocker = roleAdminBlocker($row)) !== '') {
|
||||
$error = "$row[role_name] stays an admin role: $blocker";
|
||||
}
|
||||
if ($error !== '') {
|
||||
flashAlert(escapeHtml($error), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
logAudit("User Role", "Edit", "$session_name edited user role $name", 0, $role_id);
|
||||
$changes = roleChangeText($row, $fields);
|
||||
roleSave($fields, $role_id);
|
||||
|
||||
flashAlert("User Role <strong>$name</strong> edited");
|
||||
if ($changes !== '') {
|
||||
logAudit("User Role", "Edit", "$session_name edited role " . escapeSql($fields['name']) . " - " . escapeSql($changes), 0, $role_id);
|
||||
}
|
||||
|
||||
flashAlert("Role <strong>" . escapeHtml($fields['name']) . "</strong> " . ($changes !== '' ? 'saved' : 'unchanged'));
|
||||
|
||||
redirect();
|
||||
|
||||
@@ -77,24 +68,65 @@ if (isset($_GET['archive_role'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$role_id = intval($_GET['archive_role']);
|
||||
|
||||
// Check role isn't in use
|
||||
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(user_id) FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
|
||||
$role_user_count = mysqli_fetch_row($sql_role_user_count)[0];
|
||||
if ($role_user_count != 0) {
|
||||
flashAlert("Role must not in use to archive it", 'error');
|
||||
|
||||
$row = roleRow(intval($_GET['archive_role']));
|
||||
if (!$row) {
|
||||
flashAlert("That role is gone - it may have been deleted", 'error');
|
||||
redirect();
|
||||
}
|
||||
if (($blocker = roleArchiveBlocker($row)) !== '') {
|
||||
flashAlert("<strong>" . escapeHtml($row['role_name']) . "</strong> wasn't archived: " . escapeHtml($blocker), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli, "UPDATE user_roles SET role_archived_at = NOW() WHERE role_id = $role_id");
|
||||
roleSetArchived($row['role_id'], true);
|
||||
|
||||
$role_name = escapeSql(getFieldById('user_roles', $role_id, 'role_name'));
|
||||
logAudit("User Role", "Archive", "$session_name archived role " . escapeSql($row['role_name']), 0, $row['role_id']);
|
||||
|
||||
logAudit("User Role", "Archive", "$session_name archived user role $role_name", 0, $role_id);
|
||||
|
||||
flashAlert("User Role <strong>$role_name</strong> archived", 'error');
|
||||
flashAlert("Role <strong>" . escapeHtml($row['role_name']) . "</strong> archived");
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['restore_role'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$row = roleRow(intval($_GET['restore_role']));
|
||||
if (!$row) {
|
||||
flashAlert("That role is gone - it may have been deleted", 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
roleSetArchived($row['role_id'], false);
|
||||
|
||||
logAudit("User Role", "Restore", "$session_name restored role " . escapeSql($row['role_name']), 0, $row['role_id']);
|
||||
|
||||
flashAlert("Role <strong>" . escapeHtml($row['role_name']) . "</strong> restored");
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_role'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$row = roleRow(intval($_GET['delete_role']));
|
||||
if (!$row) {
|
||||
flashAlert("That role is gone - it may have been deleted", 'error');
|
||||
redirect();
|
||||
}
|
||||
if (($blocker = roleDeleteBlocker($row)) !== '') {
|
||||
flashAlert("<strong>" . escapeHtml($row['role_name']) . "</strong> wasn't deleted: " . escapeHtml($blocker), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
roleDelete($row['role_id']);
|
||||
|
||||
logAudit("User Role", "Delete", "$session_name deleted role " . escapeSql($row['role_name']), 0, $row['role_id']);
|
||||
|
||||
flashAlert("Role <strong>" . escapeHtml($row['role_name']) . "</strong> deleted", 'error');
|
||||
|
||||
redirect();
|
||||
|
||||
|
||||
291
admin/roles.php
291
admin/roles.php
@@ -1,154 +1,173 @@
|
||||
<?php
|
||||
|
||||
// Default Column Sortby Filter
|
||||
$sort = "role_is_admin";
|
||||
$order = "DESC";
|
||||
/*
|
||||
* Admin > Roles - what each role lets its agents open: an admin role opens
|
||||
* everything, any other a level of each module. Each shows its access at a
|
||||
* glance and its agents (opening Users filtered to them). A role can be
|
||||
* changed, copied as the start of another, archived - kept for the agents
|
||||
* archived with it, not offered for new ones - or, while nobody has it,
|
||||
* deleted. The work is in functions/roles.php; the handlers are
|
||||
* admin/post/roles.php.
|
||||
*/
|
||||
|
||||
require_once "includes/inc_all_admin.php";
|
||||
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT SQL_CALC_FOUND_ROWS * FROM user_roles
|
||||
WHERE (role_name LIKE '%$q%' OR role_description LIKE '%$q%')
|
||||
AND role_archived_at IS NULL
|
||||
ORDER BY $sort $order LIMIT $record_from, $record_to"
|
||||
);
|
||||
$show_archived = isset($_GET['archived']);
|
||||
$search = trim((string) ($_GET['q'] ?? ''));
|
||||
|
||||
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
$all_roles = roleRows();
|
||||
$roles = [];
|
||||
$archived_count = 0;
|
||||
foreach ($all_roles as $role_id => $row) {
|
||||
$archived_count += $row['role_archived_at'] ? 1 : 0;
|
||||
if ((bool) $row['role_archived_at'] !== $show_archived) {
|
||||
continue;
|
||||
}
|
||||
if ($search !== '' && mb_stripos($row['role_name'], $search) === false && mb_stripos((string) $row['role_description'], $search) === false) {
|
||||
continue;
|
||||
}
|
||||
$roles[$role_id] = $row;
|
||||
}
|
||||
$agents = roleAgents(array_keys($roles), 5);
|
||||
|
||||
// This page with some of its query changed - null drops a key
|
||||
$page_url = function (array $change = []) use ($show_archived, $search) {
|
||||
$query = array_merge(['q' => $search !== '' ? $search : null, 'archived' => $show_archived ? 1 : null], $change);
|
||||
return 'roles.php' . (($query = http_build_query(array_filter($query, function ($v) {
|
||||
return $v !== null;
|
||||
}))) ? "?$query" : '');
|
||||
};
|
||||
|
||||
$csrf = $_SESSION['csrf_token'];
|
||||
|
||||
?>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-user-shield me-2"></i>Roles</h3>
|
||||
<div class="card-tools">
|
||||
<div class="btn-group">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/role/role_add.php">
|
||||
<i class="fas fa-fw fa-user-plus me-2"></i>New Role
|
||||
</button>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-user-shield me-2"></i>Roles</h3>
|
||||
<div class="card-tools">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-size="lg" data-modal-url="modals/role/role_add.php"><i class="fas fa-plus me-2"></i>New Role</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="p-2 border-bottom">
|
||||
<p class="small text-secondary mb-2">A role sets what its agents can open - an admin role everything, including Admin; any other a level of each part of ITFlow. Which clients an agent sees is set on the agent, in <a href="users.php">Users</a>.</p>
|
||||
<form autocomplete="off" class="d-flex flex-wrap align-items-center gap-2">
|
||||
<?php if ($show_archived) { ?><input type="hidden" name="archived" value="1"><?php } ?>
|
||||
<div class="input-group input-group-sm w-auto">
|
||||
<input type="search" class="form-control" name="q" value="<?= escapeHtml($search) ?>" placeholder="Search roles">
|
||||
<button class="btn btn-primary" title="Search"><i class="fa fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-header py-3">
|
||||
<form autocomplete="off">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-md-4">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) {echo stripslashes(escapeHtml($q));} ?>" placeholder="Search Roles">
|
||||
<button class="btn btn-primary" title="Search"><i class="fa fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="table-responsive-sm">
|
||||
<table class="table table-striped table-borderless table-hover mb-0">
|
||||
<thead class="text-dark <?php if ($num_rows[0] == 0) { echo "d-none"; } ?> text-nowrap">
|
||||
<div class="btn-group btn-group-sm" role="group" aria-label="Current or archived">
|
||||
<a class="btn btn-outline-secondary<?= $show_archived ? '' : ' active' ?>" href="<?= escapeHtml($page_url(['archived' => null])) ?>">Current</a>
|
||||
<a class="btn btn-outline-secondary<?= $show_archived ? ' active' : '' ?>" href="<?= escapeHtml($page_url(['archived' => 1])) ?>">Archived <span class="badge text-bg-secondary ms-1"><?= $archived_count ?></span></a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="table-responsive-sm">
|
||||
<table class="table table-borderless table-hover align-middle mb-0">
|
||||
<?php if ($roles) { ?>
|
||||
<thead class="text-dark">
|
||||
<tr>
|
||||
<th class="ps-3">
|
||||
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=role_name&order=<?= $disp ?>">
|
||||
Role <?php if ($sort == 'role_name') { echo $order_icon; } ?>
|
||||
</a>
|
||||
</th>
|
||||
<th>Members</th>
|
||||
<th>
|
||||
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=role_is_admin&order=<?= $disp ?>">
|
||||
Admin <?php if ($sort == 'role_is_admin') { echo $order_icon; } ?>
|
||||
</a>
|
||||
</th>
|
||||
<th class="text-center">Action</th>
|
||||
<th class="ps-3">Role</th>
|
||||
<th class="d-none d-md-table-cell">Access</th>
|
||||
<th class="d-none d-sm-table-cell">Agents</th>
|
||||
<th class="text-center" style="width: 1%">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$role_id = intval($row['role_id']);
|
||||
$role_name = escapeHtml($row['role_name']);
|
||||
$role_description = escapeHtml($row['role_description']);
|
||||
$role_admin = intval($row['role_is_admin']);
|
||||
$role_archived_at = escapeHtml($row['role_archived_at']);
|
||||
|
||||
// Count number of users that have each role
|
||||
$sql_role_user_count = mysqli_query($mysqli, "SELECT COUNT(user_id) FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
|
||||
$role_user_count = mysqli_fetch_row($sql_role_user_count)[0];
|
||||
|
||||
$sql_users = mysqli_query($mysqli, "SELECT user_name FROM users WHERE user_role_id = $role_id AND user_archived_at IS NULL");
|
||||
// Initialize an empty array to hold user names
|
||||
$user_names = [];
|
||||
|
||||
// Fetch each row and store the user_name in the array
|
||||
while($row = mysqli_fetch_assoc($sql_users)) {
|
||||
$user_names[] = escapeHtml($row['user_name']);
|
||||
}
|
||||
|
||||
// Convert the array of user names to a comma-separated string
|
||||
$user_names_string = implode(",", $user_names);
|
||||
|
||||
if (empty($user_names_string)) {
|
||||
$user_names_string = "-";
|
||||
}
|
||||
|
||||
?>
|
||||
<tr>
|
||||
<td class="ps-3">
|
||||
<a class="<?php if ($role_id !== 3) { ?> ajax-modal" data-modal-url="modals/role/role_edit.php?id=<?= $role_id ?>" <?php } ?> href="#">
|
||||
<div class="d-flex">
|
||||
<i class="fas fa-fw fa-2x fa-user-shield text-dark me-2"></i>
|
||||
<div class="flex-grow-1">
|
||||
<div><?= $role_name ?></div>
|
||||
<div><small class="text-secondary"><?= $role_description ?></small></div>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</td>
|
||||
<td><?= $user_names_string ?></td>
|
||||
<td><?= $role_admin ? 'Yes' : 'No' ?></td>
|
||||
<td>
|
||||
<?php if ($role_id !== 3) { ?>
|
||||
<div class="dropdown dropstart text-center">
|
||||
<button class="btn btn-secondary btn-sm" title="More actions" type="button" data-bs-toggle="dropdown">
|
||||
<i class="fas fa-ellipsis-h"></i>
|
||||
</button>
|
||||
<div class="dropdown-menu">
|
||||
|
||||
<a class="dropdown-item ajax-modal" href="#"
|
||||
data-modal-url="modals/role/role_edit.php?id=<?= $role_id ?>">
|
||||
<i class="fas fa-fw fa-user-edit me-2"></i>Edit
|
||||
</a>
|
||||
|
||||
<?php if (empty($role_archived_at) && $role_user_count == 0) { ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item text-danger confirm-link" href="post.php?archive_role=<?= $role_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>">
|
||||
<i class="fas fa-fw fa-archive me-2"></i>Archive
|
||||
</a>
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
<?php } ?>
|
||||
<tbody>
|
||||
<?php foreach ($roles as $role_id => $row) {
|
||||
$name = escapeHtml($row['role_name']);
|
||||
$is_admin = intval($row['role_is_admin']) === 1;
|
||||
$is_mine = $role_id === intval($session_user_role);
|
||||
$edit_url = "modals/role/role_edit.php?id=$role_id";
|
||||
$agent_count = intval($row['agents']);
|
||||
$archive_blocker = roleArchiveBlocker($row);
|
||||
$delete_blocker = roleDeleteBlocker($row);
|
||||
// The agents with it - a few faces and how many, opening Users filtered to them
|
||||
ob_start();
|
||||
if ($agent_count) { ?>
|
||||
<a class="d-inline-flex align-items-center gap-2 text-reset text-decoration-none" href="users.php?role=<?= $role_id ?>" title="Their agents in Users">
|
||||
<span class="itflow-role-agents">
|
||||
<?php foreach ($agents[$role_id] ?? [] as $agent) { ?><?= userAvatar($agent['user_id'], $agent['user_name'], $agent['user_avatar'], 'sm') ?><?php } ?>
|
||||
</span>
|
||||
<span class="small text-nowrap"><?= $agent_count ?> agent<?= $agent_count === 1 ? '' : 's' ?></span>
|
||||
</a>
|
||||
<?php } else { ?>
|
||||
<span class="small text-secondary">Nobody</span>
|
||||
<?php }
|
||||
if (intval($row['agents_archived'])) { ?>
|
||||
<div class="small text-secondary"><?= intval($row['agents_archived']) ?> archived</div>
|
||||
<?php }
|
||||
$agents_html = ob_get_clean();
|
||||
?>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
'thing' => 'roles',
|
||||
'icon' => 'fa-user-shield',
|
||||
'actions' => [['label' => 'New Role', 'modal' => 'modals/role/role_add.php', 'icon' => 'fa-user-plus']],
|
||||
];
|
||||
?>
|
||||
<?php require_once "../includes/filter_footer.php";
|
||||
?>
|
||||
<tr data-role-id="<?= $role_id ?>">
|
||||
<td class="ps-3">
|
||||
<div class="d-flex align-items-center gap-3">
|
||||
<span class="itflow-asset-icon itflow-asset-tone-other d-none d-sm-inline-flex" aria-hidden="true"><i class="fas fa-fw <?= $is_admin ? 'fa-user-shield' : 'fa-user-tag' ?>"></i></span>
|
||||
<div class="min-w-0">
|
||||
<a class="ajax-modal fw-semibold text-body" href="#" data-modal-size="lg" data-modal-url="<?= $edit_url ?>" title="Edit <?= $name ?>"><?= $name ?></a>
|
||||
<?php if ($is_mine) { ?><span class="badge rounded-pill bg-primary-subtle text-primary-emphasis fw-normal ms-1">Yours</span><?php } ?>
|
||||
<?php if (trim((string) $row['role_description']) !== '') { ?>
|
||||
<div class="small text-secondary"><?= escapeHtml($row['role_description']) ?></div>
|
||||
<?php } ?>
|
||||
<?php if ($row['role_archived_at']) { ?>
|
||||
<div class="small text-secondary"><i class="fas fa-fw fa-archive me-1"></i>Archived <?= date('M j, Y', strtotime($row['role_archived_at'])) ?></div>
|
||||
<?php } ?>
|
||||
<!-- On a phone, access and agents come along under the name -->
|
||||
<div class="d-md-none mt-1"><?= roleAccessHtml($row) ?></div>
|
||||
<div class="d-sm-none mt-1"><?= $agents_html ?></div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell"><?= roleAccessHtml($row) ?></td>
|
||||
<td class="d-none d-sm-table-cell"><?= $agents_html ?></td>
|
||||
<td>
|
||||
<div class="dropdown dropstart text-center">
|
||||
<button class="btn btn-secondary btn-sm" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-ellipsis-h"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="<?= $edit_url ?>"><i class="fas fa-fw fa-edit me-2"></i>Edit</a>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/role/role_add.php?copy=<?= $role_id ?>" title="A new role starting from this one"><i class="fas fa-fw fa-copy me-2"></i>Copy</a>
|
||||
<?php if ($agent_count) { ?>
|
||||
<a class="dropdown-item" href="users.php?role=<?= $role_id ?>"><i class="fas fa-fw fa-users me-2"></i>Its agents</a>
|
||||
<?php } ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<?php if ($row['role_archived_at']) { ?>
|
||||
<a class="dropdown-item" href="post.php?restore_role=<?= $role_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-redo me-2"></i>Restore</a>
|
||||
<?php } elseif ($archive_blocker !== '') { ?>
|
||||
<span class="dropdown-item disabled" title="<?= escapeHtml($archive_blocker) ?>"><i class="fas fa-fw fa-archive me-2"></i>Archive</span>
|
||||
<?php } else { ?>
|
||||
<a class="dropdown-item" href="post.php?archive_role=<?= $role_id ?>&csrf_token=<?= $csrf ?>" title="Kept for agents archived with it, but not given to anyone new"><i class="fas fa-fw fa-archive me-2"></i>Archive</a>
|
||||
<?php } ?>
|
||||
<?php if ($delete_blocker !== '') { ?>
|
||||
<span class="dropdown-item disabled" title="<?= escapeHtml($delete_blocker) ?>"><i class="fas fa-fw fa-trash me-2"></i>Delete</span>
|
||||
<?php } else { ?>
|
||||
<a class="dropdown-item text-danger text-bold confirm-link" href="post.php?delete_role=<?= $role_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Delete <?= $name ?>?" data-confirm-text="Nobody has it, so no agent changes." data-confirm-button="Delete"><i class="fas fa-fw fa-trash me-2"></i>Delete</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<?php if (!$roles) { ?>
|
||||
<?= emptyStateHtml([
|
||||
'thing' => 'roles',
|
||||
'title' => $show_archived ? 'No archived roles' : 'No roles yet',
|
||||
'text' => $show_archived ? 'An archived role stays with the agents archived with it, but isn\'t given to anyone new.' : 'A role sets what its agents can open.',
|
||||
'icon' => 'fa-user-shield',
|
||||
'filtered' => $search !== '',
|
||||
'clear_url' => $page_url(['q' => null]),
|
||||
'actions' => $show_archived ? [] : [['label' => 'New Role', 'modal' => 'modals/role/role_add.php', 'size' => 'lg']],
|
||||
]) ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
|
||||
Reference in New Issue
Block a user