95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
|
session_start();
|
|
require 'db_con.php';
|
|
|
|
// Vérifie si l'utilisateur est admin
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
// Ajout d'un utilisateur
|
|
if (isset($_POST['add_user'])) {
|
|
$username = $_POST['username'];
|
|
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
|
|
$role = $_POST['role'];
|
|
$status = $_POST['status'];
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, password, role, status) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$username, $password, $role, $status]);
|
|
}
|
|
|
|
// Suppression d'un utilisateur
|
|
if (isset($_POST['delete_user'])) {
|
|
$user_id = $_POST['user_id'];
|
|
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
|
|
$stmt->execute([$user_id]);
|
|
}
|
|
|
|
// Récupération des utilisateurs pour affichage
|
|
$users = $pdo->query("SELECT * FROM users")->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>User Management</title>
|
|
</head>
|
|
<body>
|
|
<h2>User Management</h2>
|
|
|
|
<!-- Formulaire d'ajout d'un nouvel utilisateur -->
|
|
<h3>Add New User</h3>
|
|
<form action="user_management.php" method="post">
|
|
<label for="username">Username:</label>
|
|
<input type="text" id="username" name="username" required>
|
|
<br>
|
|
<label for="password">Password:</label>
|
|
<input type="password" id="password" name="password" required>
|
|
<br>
|
|
<label for="role">Role:</label>
|
|
<select id="role" name="role" required>
|
|
<option value="admin">Admin</option>
|
|
<option value="chef">Chef</option>
|
|
<option value="cook">Cook</option>
|
|
<option value="waiter">Waiter</option>
|
|
</select>
|
|
<br>
|
|
<label for="status">Status:</label>
|
|
<select id="status" name="status" required>
|
|
<option value="active">Active</option>
|
|
<option value="inactive">Inactive</option>
|
|
</select>
|
|
<br>
|
|
<button type="submit" name="add_user">Add User</button>
|
|
</form>
|
|
|
|
<!-- Affichage des utilisateurs existants -->
|
|
<h3>Existing Users</h3>
|
|
<table border="1">
|
|
<tr>
|
|
<th>Username</th>
|
|
<th>Role</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
<?php foreach ($users as $user): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($user['username']) ?></td>
|
|
<td><?= htmlspecialchars($user['role']) ?></td>
|
|
<td><?= htmlspecialchars($user['status']) ?></td>
|
|
<td>
|
|
<form action="user_management.php" method="post" style="display:inline;">
|
|
<input type="hidden" name="user_id" value="<?= $user['id'] ?>">
|
|
<button type="submit" name="delete_user" onclick="return confirm('Are you sure you want to delete this user?')">Delete</button>
|
|
</form>
|
|
<a href="edit_user.php?id=<?= $user['id'] ?>">Edit</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</table>
|
|
<a href="main.php">Retour à la page principale</a>
|
|
</body>
|
|
</html>
|