35273-vm/users.php
Flatlogic Bot 734d16aa81 v4
2025-10-27 13:06:57 +00:00

159 lines
7.1 KiB
PHP

<?php
require_once 'config.php';
require_once 'db/config.php';
// Admin-only access
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
header('Location: dashboard.php');
exit;
}
$pdo = db();
$feedback = [];
if (isset($_GET['update']) && $_GET['update'] === 'success') {
$feedback = ['type' => 'success', 'message' => 'User updated successfully.'];
}
// Handle Create User
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create') {
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$password = $_POST['password'];
$role = $_POST['role'];
if (!$email || empty($password) || !in_array($role, ['user', 'admin'])) {
$feedback = ['type' => 'danger', 'message' => 'Invalid input. Please check all fields.'];
} else {
// Check if user already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$feedback = ['type' => 'danger', 'message' => 'User with this email already exists.'];
} else {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (email, password, role) VALUES (?, ?, ?)");
if ($stmt->execute([$email, $hashedPassword, $role])) {
$feedback = ['type' => 'success', 'message' => 'User created successfully.'];
} else {
$feedback = ['type' => 'danger', 'message' => 'Failed to create user.'];
}
}
}
}
// Handle Delete User
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') {
$userId = filter_input(INPUT_POST, 'user_id', FILTER_VALIDATE_INT);
// Prevent admin from deleting themselves
if ($userId && $userId != $_SESSION['user_id']) {
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
if ($stmt->execute([$userId])) {
$feedback = ['type' => 'success', 'message' => 'User deleted successfully.'];
} else {
$feedback = ['type' => 'danger', 'message' => 'Failed to delete user.'];
}
} else {
$feedback = ['type' => 'danger', 'message' => 'Invalid request or you cannot delete your own account.'];
}
}
$stmt = $pdo->query("SELECT id, email, role, created_at FROM users ORDER BY created_at DESC");
$users = $stmt->fetchAll();
include 'header.php';
?>
<div class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="text-primary">User Management</h1>
<a href="dashboard.php" class="btn btn-outline-secondary">Back to Dashboard</a>
</div>
<?php if (!empty($feedback)): ?>
<div class="alert alert-<?php echo htmlspecialchars($feedback['type']); ?> alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($feedback['message']); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<!-- Create User Form -->
<div class="card shadow-sm mb-4">
<div class="card-header bg-light">
<h5 class="mb-0">Create New User</h5>
</div>
<div class="card-body">
<form action="users.php" method="POST">
<input type="hidden" name="action" value="create">
<div class="row">
<div class="col-md-4 mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="col-md-3 mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="col-md-3 mb-3">
<label for="role" class="form-label">Role</label>
<select class="form-select" id="role" name="role">
<option value="user" selected>User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="col-md-2 d-flex align-items-end">
<button type="submit" class="btn btn-primary w-100">Create User</button>
</div>
</div>
</form>
</div>
</div>
<div class="card shadow-sm">
<div class="card-header bg-light">
<h5 class="mb-0">All Users</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-light">
<tr>
<th>ID</th>
<th>Email</th>
<th>Role</th>
<th>Registered At</th>
<th class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($users)):
?>
<tr>
<td colspan="5" class="text-center text-muted">No users found.</td>
</tr>
<?php else: ?>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo htmlspecialchars($user['id']); ?></td>
<td><?php echo htmlspecialchars($user['email']); ?></td>
<td><span class="badge bg-secondary"><?php echo htmlspecialchars($user['role']); ?></span></td>
<td><?php echo htmlspecialchars(date('Y-m-d H:i', strtotime($user['created_at']))); ?></td>
<td class="text-end">
<a href="edit_user.php?id=<?php echo $user['id']; ?>" class="btn btn-sm btn-outline-primary">Edit</a>
<form action="users.php" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this user?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
<button type="submit" class="btn btn-sm btn-outline-danger" <?php if($user['id'] == $_SESSION['user_id']) echo 'disabled'; ?>>Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>