87 lines
3.2 KiB
PHP
87 lines
3.2 KiB
PHP
<?php
|
|
// users.php
|
|
$page_title = "User Management";
|
|
require_once 'includes/header.php';
|
|
|
|
// Role check
|
|
if ($user_role !== 'Admin') {
|
|
$_SESSION['error'] = "You do not have permission to access this page.";
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
// Fetch users
|
|
$users = db()->query("SELECT * FROM users WHERE deleted_at IS NULL ORDER BY role, full_name")->fetchAll();
|
|
|
|
// Handle deletion
|
|
if (isset($_GET['delete'])) {
|
|
$id = (int)$_GET['delete'];
|
|
if ($id === (int)$_SESSION['user_id']) {
|
|
$_SESSION['error'] = "You cannot delete your own account.";
|
|
} else {
|
|
$stmt = db()->prepare("UPDATE users SET deleted_at = NOW() WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$_SESSION['success'] = "User deleted successfully.";
|
|
}
|
|
header("Location: users.php");
|
|
exit;
|
|
}
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h3 fw-bold mb-0">User Management</h1>
|
|
<a href="user_form.php" class="btn btn-primary">
|
|
<i class="bi bi-person-plus me-2"></i>Add New User
|
|
</a>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Full Name</th>
|
|
<th>Username</th>
|
|
<th>Role</th>
|
|
<th>Created At</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($users as $u): ?>
|
|
<tr>
|
|
<td>
|
|
<div class="fw-bold"><?= e($u['full_name']) ?></div>
|
|
</td>
|
|
<td><?= e($u['username']) ?></td>
|
|
<td>
|
|
<?php
|
|
$role_badge = 'bg-secondary';
|
|
if ($u['role'] === 'Admin') $role_badge = 'bg-dark';
|
|
if ($u['role'] === 'Sales') $role_badge = 'bg-primary';
|
|
if ($u['role'] === 'Finance') $role_badge = 'bg-info';
|
|
?>
|
|
<span class="badge <?= $role_badge ?>"><?= e($u['role']) ?></span>
|
|
</td>
|
|
<td><?= date('M d, Y', strtotime($u['created_at'])) ?></td>
|
|
<td class="text-end">
|
|
<div class="btn-group">
|
|
<a href="user_form.php?id=<?= $u['id'] ?>" class="btn btn-sm btn-outline-primary" title="Edit">
|
|
<i class="bi bi-pencil"></i>
|
|
</a>
|
|
<?php if ($u['id'] !== (int)$_SESSION['user_id']): ?>
|
|
<a href="users.php?delete=<?= $u['id'] ?>" class="btn btn-sm btn-outline-danger" title="Delete" onclick="return confirm('Are you sure you want to delete this user?')">
|
|
<i class="bi bi-trash"></i>
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|