160 lines
6.7 KiB
PHP
160 lines
6.7 KiB
PHP
<?php
|
|
require_once 'auth-check.php';
|
|
if ($_SESSION['user_role'] !== 'Admin') {
|
|
header("Location: index.php?error=access_denied");
|
|
exit;
|
|
}
|
|
require_once 'db/config.php';
|
|
|
|
// Function to execute query and return results
|
|
function get_users() {
|
|
try {
|
|
$pdo = db();
|
|
// Check if table exists, if not, run migration
|
|
$result = $pdo->query("SHOW TABLES LIKE 'users'");
|
|
if ($result->rowCount() == 0) {
|
|
$sql = file_get_contents('db/migrations/002_create_users_table.sql');
|
|
$pdo->exec($sql);
|
|
}
|
|
|
|
$stmt = $pdo->query('SELECT id, name, email, role, created_at FROM users ORDER BY created_at DESC');
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
return ['error' => 'Database error: ' . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
$users = get_users();
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>User Management - IC-Inventory</title>
|
|
<meta name="description" content="Manage users of the IC-Inventory system.">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
|
<script src="https://unpkg.com/feather-icons"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="wrapper">
|
|
<nav id="sidebar" class="d-flex flex-column flex-shrink-0 p-3">
|
|
<a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-decoration-none">
|
|
<span class="fs-4">IC-Inventory</span>
|
|
</a>
|
|
<hr>
|
|
<ul class="nav nav-pills flex-column mb-auto">
|
|
<li class="nav-item">
|
|
<a href="index.php" class="nav-link" aria-current="page">
|
|
<i data-feather="home" class="me-2"></i>
|
|
Dashboard
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a href="index.php" class="nav-link">
|
|
<i data-feather="box" class="me-2"></i>
|
|
Assets
|
|
</a>
|
|
</li>
|
|
<?php if ($_SESSION['user_role'] === 'Admin'): ?>
|
|
<li>
|
|
<a href="users.php" class="nav-link active">
|
|
<i data-feather="users" class="me-2"></i>
|
|
Users
|
|
</a>
|
|
</li>
|
|
<?php endif; ?>
|
|
<li>
|
|
<a href="#" class="nav-link">
|
|
<i data-feather="settings" class="me-2"></i>
|
|
Settings
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
<hr>
|
|
<div>
|
|
<span class="d-flex align-items-center text-decoration-none"><strong><?php echo htmlspecialchars($_SESSION['user_name']); ?></strong></span>
|
|
<a href="logout.php" class="d-flex align-items-center text-decoration-none">
|
|
<i data-feather="log-out" class="me-2"></i>
|
|
<strong>Logout</strong>
|
|
</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<main id="content">
|
|
<div class="header">
|
|
<h1>User Management</h1>
|
|
<div>
|
|
<a href="add-user.php" class="btn btn-primary">Add New User</a>
|
|
<div class="theme-switcher" id="theme-switcher">
|
|
<i data-feather="moon"></i>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="surface p-4">
|
|
<?php if (isset($_GET['success']) && $_GET['success'] === 'user_added'): ?>
|
|
<div class="alert alert-success">User successfully added!</div>
|
|
<?php elseif (isset($_GET['success']) && $_GET['success'] === 'user_updated'): ?>
|
|
<div class="alert alert-success">User successfully updated!</div>
|
|
<?php elseif (isset($_GET['success']) && $_GET['success'] === 'user_deleted'): ?>
|
|
<div class="alert alert-success">User successfully deleted!</div>
|
|
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'cannot_delete_admin'): ?>
|
|
<div class="alert alert-danger">The default admin user cannot be deleted.</div>
|
|
<?php endif; ?>
|
|
<?php if (isset($users['error'])): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo htmlspecialchars($users['error']); ?>
|
|
</div>
|
|
<?php elseif (empty($users)): ?>
|
|
<div class="text-center p-5">
|
|
<h4>No users found.</h4>
|
|
<p>Get started by adding your first user.</p>
|
|
<a href="add-user.php" class="btn btn-primary">Add User</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table user-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Email</th>
|
|
<th>Role</th>
|
|
<th>Created At</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($users as $user): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($user['name']); ?></td>
|
|
<td><?php echo htmlspecialchars($user['email']); ?></td>
|
|
<td><?php echo htmlspecialchars($user['role']); ?></td>
|
|
<td><?php echo htmlspecialchars($user['created_at']); ?></td>
|
|
<td>
|
|
<a href="edit-user.php?id=<?php echo $user['id']; ?>" class="btn btn-sm btn-outline-primary">Edit</a>
|
|
<a href="delete-user.php?id=<?php echo $user['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this user?');">Delete</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
<script>
|
|
feather.replace();
|
|
</script>
|
|
</body>
|
|
</html>
|