You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.3 KiB

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/default.php';
require_once __DIR__ . '/auth.php';
requireAuthentication();
requireAdmin();
$users = loadUsers();
$errors = [];
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'create') {
$username = trim((string) ($_POST['username'] ?? ''));
$password = (string) ($_POST['password'] ?? '');
$email = trim((string) ($_POST['email'] ?? ''));
$role = ($_POST['role'] ?? 'user') === 'admin' ? 'admin' : 'user';
if ($username === '' || $password === '') {
$errors[] = 'Username and password are required.';
} elseif (!createUser($username, $password, $role, $email)) {
$errors[] = 'User already exists or could not be created.';
} else {
$users = loadUsers();
$success = 'User created successfully.';
}
} elseif ($action === 'update') {
$index = (int) ($_POST['index'] ?? -1);
$username = trim((string) ($_POST['username'] ?? ''));
$email = trim((string) ($_POST['email'] ?? ''));
$password = (string) ($_POST['password'] ?? '');
$role = ($_POST['role'] ?? 'user') === 'admin' ? 'admin' : 'user';
if ($index >= 0 && isset($users[$index])) {
if ($username !== '') {
$users[$index]['username'] = $username;
}
if ($email !== '') {
$users[$index]['email'] = $email;
}
if ($password !== '') {
$users[$index]['passwordHash'] = hashPassword($password);
}
$users[$index]['role'] = $role;
if (saveUsers($users)) {
$success = 'User updated successfully.';
} else {
$errors[] = 'Unable to update user.';
}
} else {
$errors[] = 'Invalid user selected.';
}
} elseif ($action === 'delete') {
$index = (int) ($_POST['index'] ?? -1);
if ($index >= 0 && isset($users[$index])) {
unset($users[$index]);
$users = array_values($users);
if (saveUsers($users)) {
$success = 'User deleted successfully.';
} else {
$errors[] = 'Unable to delete user.';
}
} else {
$errors[] = 'Invalid user selected.';
}
}
}
$users = loadUsers();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Management - KaI</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<div class="wrap">
<div class="topbar">
<h1>User Management</h1>
<a href="index.php">Back to chat</a>
</div>
<?php if ($success !== ''): ?><div class="success"><?php echo htmlspecialchars($success); ?></div><?php endif; ?>
<?php if (!empty($errors)): ?><div class="error"><?php echo htmlspecialchars(implode('<br>', $errors)); ?></div><?php endif; ?>
<div class="card">
<h2>Create user</h2>
<form method="post">
<input type="hidden" name="action" value="create">
<div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap:10px;">
<div><label>Username</label><br><input name="username" required></div>
<div><label>Email</label><br><input name="email" type="email"></div>
<div><label>Password</label><br><input name="password" type="password" required></div>
<div><label>Role</label><br><select name="role"><option value="user">User</option><option value="admin">Admin</option></select></div>
<div><label>&nbsp;</label><button type="submit">Create</button></div>
</div>
</form>
</div>
<div class="card">
<h2>Existing users</h2>
<table>
<thead>
<tr><th>Username</th><th>Email</th><th>Role</th><th>Actions</th></tr>
</thead>
<tbody>
<?php foreach ($users as $index => $user): ?>
<tr>
<td>
<form method="post" style="display:flex; gap:8px; align-items:center; flex-wrap:wrap;">
<input type="hidden" name="action" value="update">
<input type="hidden" name="index" value="<?php echo (int) $index; ?>">
<input name="username" value="<?php echo htmlspecialchars($user['username'] ?? ''); ?>" style="width: 150px"; required>
</td>
<td>
<input name="email" type="email" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>">
</td>
<td>
<select name="role">
<option value="user" <?php if (($user['role'] ?? 'user') === 'user') echo 'selected'; ?>>User</option>
<option value="admin" <?php if (($user['role'] ?? 'user') === 'admin') echo 'selected'; ?>>Admin</option>
</select>
</td>
<td>
<input name="password" type="password" placeholder="New password" style="width: 150px">
<button type="submit">Save</button>
</form>
<form method="post" style="display:inline-block; margin-left:8px;">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="index" value="<?php echo (int) $index; ?>">
<button class="danger" type="submit">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</body>
</html>