38682-vm/admin/user_groups.php
2026-02-23 12:56:09 +00:00

129 lines
5.9 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../includes/functions.php';
$pdo = db();
require_permission('user_groups_view');
$message = '';
// Handle Delete
if (isset($_GET['delete'])) {
if (!has_permission('user_groups_del')) {
$message = '<div class="alert alert-danger border-0 shadow-sm rounded-3">Access Denied: You do not have permission to delete groups.</div>';
} else {
$id = $_GET['delete'];
// Don't delete admin group
if ($id == 1) {
$message = '<div class="alert alert-danger border-0 shadow-sm rounded-3">Cannot delete the Administrator group.</div>';
} else {
$pdo->prepare("DELETE FROM user_groups WHERE id = ?")->execute([$id]);
header("Location: user_groups.php");
exit;
}
}
}
// Fetch Groups
$groups = $pdo->query("SELECT g.*, (SELECT COUNT(*) FROM users u WHERE u.group_id = g.id) as user_count
FROM user_groups g
ORDER BY g.id ASC")->fetchAll(PDO::FETCH_ASSOC);
include 'includes/header.php';
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h2 class="fw-bold mb-1">User Groups / Roles</h2>
<p class="text-muted mb-0">Define permissions and access levels</p>
</div>
<?php if (has_permission('user_groups_add')): ?>
<button class="btn btn-primary btn-lg shadow-sm" data-bs-toggle="modal" data-bs-target="#addGroupModal" style="border-radius: 10px;">
<i class="bi bi-shield-plus me-1"></i> Add Group
</button>
<?php endif; ?>
</div>
<?= $message ?>
<div class="row g-4">
<?php foreach ($groups as $group): ?>
<div class="col-md-6 col-lg-4">
<div class="card border-0 shadow-sm rounded-4 h-100 position-relative overflow-hidden">
<div class="card-body p-4">
<div class="d-flex justify-content-between align-items-start mb-3">
<div class="bg-primary bg-opacity-10 text-primary p-3 rounded-3 shadow-sm">
<i class="bi bi-shield-lock-fill fs-4"></i>
</div>
<?php if (has_permission('user_groups_add')): ?>
<a href="user_group_edit.php?id=<?= $group['id'] ?>" class="btn-icon-soft edit" title="Edit Permissions">
<i class="bi bi-pencil-fill"></i>
</a>
<?php endif; ?>
</div>
<h5 class="fw-bold mb-1"><?= htmlspecialchars($group['name']) ?></h5>
<p class="text-muted small mb-3"><?= $group['user_count'] ?> users assigned</p>
<div class="d-flex flex-wrap gap-1 mb-4">
<?php
$perms = explode(',', $group['permissions']);
$display_perms = array_slice($perms, 0, 3);
foreach ($display_perms as $p): if (empty($p)) continue; ?>
<span class="badge bg-light text-muted border px-2 py-1" style="font-size: 0.65rem;"><?= htmlspecialchars($p) ?></span>
<?php endforeach; ?>
<?php if (count($perms) > 3): ?>
<span class="badge bg-light text-muted border px-2 py-1" style="font-size: 0.65rem;">+<?= count($perms) - 3 ?> more</span>
<?php endif; ?>
</div>
<div class="d-flex gap-2">
<?php if (has_permission('user_groups_add')): ?>
<a href="user_group_edit.php?id=<?= $group['id'] ?>" class="btn btn-primary w-100 rounded-pill">Manage Permissions</a>
<?php endif; ?>
<?php if (has_permission('user_groups_del') && $group['id'] != 1): ?>
<a href="?delete=<?= $group['id'] ?>" class="btn btn-light text-danger w-100 rounded-pill" onclick="return confirm('Are you sure?')">Delete</a>
<?php endif; ?>
</div>
</div>
<?php if ($group['name'] === 'Administrator' || $group['permissions'] === 'all'): ?>
<div class="position-absolute top-0 end-0 m-3">
<span class="badge bg-warning text-dark shadow-sm">Super Admin</span>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<!-- Add Group Modal -->
<?php if (has_permission('user_groups_add')): ?>
<div class="modal fade" id="addGroupModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content border-0 shadow-lg rounded-4">
<div class="modal-header border-0 pb-0 ps-4 pt-4">
<h5 class="modal-title fw-bold">Create New User Group</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form action="user_group_edit.php" method="POST">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label small fw-bold text-muted">GROUP NAME</label>
<input type="text" name="name" class="form-control form-control-lg border-0 bg-light rounded-3" placeholder="e.g. Supervisor" required>
<p class="form-text small text-muted mt-2">After creating the group, you will be redirected to define its specific permissions.</p>
</div>
</div>
<div class="modal-footer border-0 p-4 pt-0">
<button type="button" class="btn btn-light rounded-pill px-4" data-bs-toggle="modal">Cancel</button>
<button type="submit" class="btn btn-primary rounded-pill px-4 fw-bold">Create & Configure</button>
</div>
</form>
</div>
</div>
</div>
<?php endif; ?>
<?php include 'includes/footer.php'; ?>