Front + gestion des utilisateurs
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* SESSION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
session_set_cookie_params([
|
||||
'lifetime' => 0,
|
||||
'path' => '/',
|
||||
'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
|
||||
'httponly' => true,
|
||||
'samesite' => 'Strict'
|
||||
]);
|
||||
|
||||
session_start();
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* VERIFICATION AUTHENTIFICATION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if (
|
||||
empty($_SESSION['user_id']) ||
|
||||
empty($_SESSION['authenticated'])
|
||||
) {
|
||||
|
||||
http_response_code(401);
|
||||
|
||||
header(
|
||||
'Content-Type: application/json; charset=utf-8'
|
||||
);
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Authentification requise.'
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
exit;
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/auth.php';
|
||||
require_once __DIR__ . '/../database.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
|
||||
$data = json_decode(
|
||||
file_get_contents('php://input'),
|
||||
true
|
||||
);
|
||||
|
||||
$nom = trim($data['nom'] ?? '');
|
||||
$pass = $data['pass'] ?? '';
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* VERIFICATION NOM UTILISATEUR
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
if ($nom === '') {
|
||||
|
||||
throw new Exception(
|
||||
'Le nom utilisateur est obligatoire.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* VERIFICATION MOT DE PASSE
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
if (strlen($pass) < 4) {
|
||||
|
||||
throw new Exception(
|
||||
'Le mot de passe doit contenir au moins 4 caractères.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* HASH DU MOT DE PASSE
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
$hash = password_hash(
|
||||
$pass,
|
||||
PASSWORD_DEFAULT
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* CREATION UTILISATEUR
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
$sql = "
|
||||
INSERT INTO login
|
||||
(nom, pass)
|
||||
VALUES
|
||||
(:nom, :pass)
|
||||
";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':nom' => $nom,
|
||||
':pass' => $hash
|
||||
]);
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* REPONSE
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Utilisateur créé.'
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
} catch (PDOException $e) {
|
||||
|
||||
http_response_code(400);
|
||||
|
||||
|
||||
/*
|
||||
* Nom utilisateur déjà existant
|
||||
*/
|
||||
|
||||
if (($e->errorInfo[1] ?? null) == 1062) {
|
||||
|
||||
$message =
|
||||
'Ce nom utilisateur existe déjà.';
|
||||
} else {
|
||||
|
||||
$message =
|
||||
'Erreur lors de la création.';
|
||||
}
|
||||
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => $message
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
} catch (Exception $e) {
|
||||
|
||||
http_response_code(400);
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/auth.php';
|
||||
require_once __DIR__ . '/../database.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
|
||||
$data = json_decode(
|
||||
file_get_contents('php://input'),
|
||||
true
|
||||
);
|
||||
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
$pass = $data['pass'] ?? '';
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* VERIFICATION UTILISATEUR
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
if ($id <= 0) {
|
||||
|
||||
throw new Exception(
|
||||
'Utilisateur invalide.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* VERIFICATION MOT DE PASSE
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
if (strlen($pass) < 4) {
|
||||
|
||||
throw new Exception(
|
||||
'Le mot de passe doit contenir au moins 4 caractères.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* HASH DU MOT DE PASSE
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
$hash = password_hash(
|
||||
$pass,
|
||||
PASSWORD_DEFAULT
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* MODIFICATION
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
$sql = "
|
||||
UPDATE login
|
||||
SET pass = :pass
|
||||
WHERE id = :id
|
||||
";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':pass' => $hash,
|
||||
':id' => $id
|
||||
]);
|
||||
|
||||
|
||||
/*
|
||||
* =====================================================
|
||||
* REPONSE
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Mot de passe modifié.'
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
} catch (Exception $e) {
|
||||
|
||||
http_response_code(400);
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/auth.php';
|
||||
require_once __DIR__ . '/../database.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
|
||||
$data = json_decode(
|
||||
file_get_contents('php://input'),
|
||||
true
|
||||
);
|
||||
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
|
||||
if ($id <= 0) {
|
||||
throw new Exception(
|
||||
'Utilisateur invalide.'
|
||||
);
|
||||
}
|
||||
|
||||
$sql = "
|
||||
UPDATE login
|
||||
SET actif = IF(actif = 1, 0, 1)
|
||||
WHERE id = :id
|
||||
";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':id' => $id
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
} catch (Exception $e) {
|
||||
|
||||
http_response_code(400);
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/auth.php';
|
||||
require_once __DIR__ . '/../database.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
id,
|
||||
nom,
|
||||
actif,
|
||||
dernier_login,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM login
|
||||
ORDER BY nom ASC
|
||||
";
|
||||
|
||||
$stmt = $pdo->query($sql);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'users' => $stmt->fetchAll(PDO::FETCH_ASSOC)
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
} catch (PDOException $e) {
|
||||
|
||||
http_response_code(500);
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Erreur base de données.'
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
Reference in New Issue
Block a user