Actualiser api/users.php
This commit is contained in:
+1
-26
@@ -1,11 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once 'config.php';
|
require_once 'config.php';
|
||||||
|
|
||||||
$method = $_SERVER['REQUEST_METHOD'];
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
$action = $_GET['action'] ?? '';
|
$action = $_GET['action'] ?? '';
|
||||||
$id = $_GET['id'] ?? null;
|
$id = $_GET['id'] ?? null;
|
||||||
|
|
||||||
// Vérifier les droits admin
|
|
||||||
$user = requireAdmin();
|
$user = requireAdmin();
|
||||||
|
|
||||||
switch ($method) {
|
switch ($method) {
|
||||||
@@ -16,22 +13,18 @@ switch ($method) {
|
|||||||
getAllUsers();
|
getAllUsers();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'PUT':
|
case 'PUT':
|
||||||
updateUser($id);
|
updateUser($id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'DELETE':
|
case 'DELETE':
|
||||||
deleteUser($id);
|
deleteUser($id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllUsers() {
|
function getAllUsers() {
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
$stmt = $db->query("
|
$stmt = $db->query("
|
||||||
SELECT u.id, u.username, u.email, u.role, u.points, u.created_at,
|
SELECT u.id, u.username, u.email, u.role, u.points, u.created_at,
|
||||||
COUNT(p.id) as total_predictions,
|
COUNT(p.id) as total_predictions,
|
||||||
@@ -41,7 +34,6 @@ function getAllUsers() {
|
|||||||
GROUP BY u.id
|
GROUP BY u.id
|
||||||
ORDER BY u.created_at DESC
|
ORDER BY u.created_at DESC
|
||||||
");
|
");
|
||||||
|
|
||||||
$users = [];
|
$users = [];
|
||||||
while ($row = $stmt->fetch()) {
|
while ($row = $stmt->fetch()) {
|
||||||
$users[] = [
|
$users[] = [
|
||||||
@@ -55,13 +47,11 @@ function getAllUsers() {
|
|||||||
'correct_predictions' => (int)$row['correct_predictions']
|
'correct_predictions' => (int)$row['correct_predictions']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(['success' => true, 'users' => $users]);
|
jsonResponse(['success' => true, 'users' => $users]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUser($id) {
|
function getUser($id) {
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
SELECT u.id, u.username, u.email, u.role, u.points, u.created_at,
|
SELECT u.id, u.username, u.email, u.role, u.points, u.created_at,
|
||||||
COUNT(p.id) as total_predictions,
|
COUNT(p.id) as total_predictions,
|
||||||
@@ -73,65 +63,50 @@ function getUser($id) {
|
|||||||
");
|
");
|
||||||
$stmt->execute([$id]);
|
$stmt->execute([$id]);
|
||||||
$user = $stmt->fetch();
|
$user = $stmt->fetch();
|
||||||
|
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
jsonResponse(['error' => 'Utilisateur non trouvé'], 404);
|
jsonResponse(['error' => 'Utilisateur non trouvé'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(['success' => true, 'user' => $user]);
|
jsonResponse(['success' => true, 'user' => $user]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateUser($id) {
|
function updateUser($id) {
|
||||||
$data = getJsonInput();
|
$data = getJsonInput();
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
$stmt = $db->prepare("SELECT id, role FROM users WHERE id = ?");
|
$stmt = $db->prepare("SELECT id, role FROM users WHERE id = ?");
|
||||||
$stmt->execute([$id]);
|
$stmt->execute([$id]);
|
||||||
$targetUser = $stmt->fetch();
|
$targetUser = $stmt->fetch();
|
||||||
|
|
||||||
if (!$targetUser) {
|
if (!$targetUser) {
|
||||||
jsonResponse(['error' => 'Utilisateur non trouvé'], 404);
|
jsonResponse(['error' => 'Utilisateur non trouvé'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$fields = [];
|
$fields = [];
|
||||||
$values = [];
|
$values = [];
|
||||||
|
|
||||||
$allowedFields = ['role', 'points'];
|
$allowedFields = ['role', 'points'];
|
||||||
|
|
||||||
foreach ($allowedFields as $field) {
|
foreach ($allowedFields as $field) {
|
||||||
if (isset($data[$field])) {
|
if (isset($data[$field])) {
|
||||||
$fields[] = "$field = ?";
|
$fields[] = "$field = ?";
|
||||||
$values[] = $data[$field];
|
$values[] = $data[$field];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($fields)) {
|
if (empty($fields)) {
|
||||||
jsonResponse(['error' => 'Aucune donnée à mettre à jour'], 400);
|
jsonResponse(['error' => 'Aucune donnée à mettre à jour'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$values[] = $id;
|
$values[] = $id;
|
||||||
$stmt = $db->prepare("UPDATE users SET " . implode(', ', $fields) . " WHERE id = ?");
|
$stmt = $db->prepare("UPDATE users SET " . implode(', ', $fields) . " WHERE id = ?");
|
||||||
$stmt->execute($values);
|
$stmt->execute($values);
|
||||||
|
|
||||||
jsonResponse(['success' => true, 'message' => 'Utilisateur mis à jour avec succès']);
|
jsonResponse(['success' => true, 'message' => 'Utilisateur mis à jour avec succès']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteUser($id) {
|
function deleteUser($id) {
|
||||||
$db = getDB();
|
|
||||||
|
|
||||||
// Empêcher la suppression de soi-même
|
|
||||||
global $user;
|
global $user;
|
||||||
if ($user['id'] == $id) {
|
if ($user['id'] == $id) {
|
||||||
jsonResponse(['error' => 'Vous ne pouvez pas supprimer votre propre compte'], 400);
|
jsonResponse(['error' => 'Vous ne pouvez pas supprimer votre propre compte'], 400);
|
||||||
}
|
}
|
||||||
|
$db = getDB();
|
||||||
$stmt = $db->prepare("DELETE FROM users WHERE id = ?");
|
$stmt = $db->prepare("DELETE FROM users WHERE id = ?");
|
||||||
$stmt->execute([$id]);
|
$stmt->execute([$id]);
|
||||||
|
|
||||||
if ($stmt->rowCount() === 0) {
|
if ($stmt->rowCount() === 0) {
|
||||||
jsonResponse(['error' => 'Utilisateur non trouvé'], 404);
|
jsonResponse(['error' => 'Utilisateur non trouvé'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(['success' => true, 'message' => 'Utilisateur supprimé avec succès']);
|
jsonResponse(['success' => true, 'message' => 'Utilisateur supprimé avec succès']);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user