Front + gestion des utilisateurs
This commit is contained in:
@@ -0,0 +1,328 @@
|
||||
<?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();
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* CONFIGURATION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/database.php';
|
||||
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* LECTURE JSON
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
$data = json_decode(
|
||||
file_get_contents('php://input'),
|
||||
true
|
||||
);
|
||||
|
||||
$action = $data['action'] ?? '';
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* REPONSE JSON
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
function jsonResponse(
|
||||
string $status,
|
||||
string $message = '',
|
||||
array $extra = []
|
||||
): never {
|
||||
|
||||
echo json_encode(
|
||||
array_merge(
|
||||
[
|
||||
'status' => $status,
|
||||
'message' => $message
|
||||
],
|
||||
$extra
|
||||
),
|
||||
JSON_UNESCAPED_UNICODE
|
||||
);
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* VERIFICATION SESSION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if ($action === 'check') {
|
||||
|
||||
if (
|
||||
isset($_SESSION['user_id']) &&
|
||||
isset($_SESSION['username'])
|
||||
) {
|
||||
|
||||
jsonResponse(
|
||||
'success',
|
||||
'',
|
||||
[
|
||||
'authenticated' => true,
|
||||
'user_id' => $_SESSION['user_id'],
|
||||
'nom' => $_SESSION['username']
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Session inactive.',
|
||||
[
|
||||
'authenticated' => false
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* DECONNEXION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if ($action === 'logout') {
|
||||
|
||||
/*
|
||||
* Détruire complètement la session
|
||||
*/
|
||||
|
||||
$_SESSION = [];
|
||||
|
||||
|
||||
/*
|
||||
* Supprimer le cookie de session
|
||||
*/
|
||||
|
||||
if (
|
||||
ini_get('session.use_cookies')
|
||||
) {
|
||||
|
||||
$params = session_get_cookie_params();
|
||||
|
||||
setcookie(
|
||||
session_name(),
|
||||
'',
|
||||
time() - 42000,
|
||||
$params['path'],
|
||||
$params['domain'] ?? '',
|
||||
$params['secure'],
|
||||
$params['httponly']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
session_destroy();
|
||||
|
||||
|
||||
jsonResponse(
|
||||
'success',
|
||||
'Déconnexion effectuée.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* CONNEXION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if ($action === 'login') {
|
||||
|
||||
$nom =
|
||||
trim(
|
||||
(string)($data['nom'] ?? '')
|
||||
);
|
||||
|
||||
$pass =
|
||||
(string)($data['pass'] ?? '');
|
||||
|
||||
|
||||
if (
|
||||
$nom === '' ||
|
||||
$pass === ''
|
||||
) {
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Identifiant et mot de passe obligatoires.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Recherche utilisateur
|
||||
*/
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
id,
|
||||
nom,
|
||||
pass,
|
||||
actif
|
||||
FROM login
|
||||
WHERE nom = :nom
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':nom' => $nom
|
||||
]);
|
||||
|
||||
|
||||
$user =
|
||||
$stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
|
||||
/*
|
||||
* Utilisateur inexistant
|
||||
*/
|
||||
|
||||
if (!$user) {
|
||||
|
||||
/*
|
||||
* Même traitement que mauvais mot de passe
|
||||
* afin de ne pas révéler si le compte existe.
|
||||
*/
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Identifiant ou mot de passe incorrect.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Compte désactivé
|
||||
*/
|
||||
|
||||
if ((int)$user['actif'] !== 1) {
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Ce compte est désactivé.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Vérification du mot de passe
|
||||
*/
|
||||
|
||||
if (
|
||||
!password_verify(
|
||||
$pass,
|
||||
$user['pass']
|
||||
)
|
||||
) {
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Identifiant ou mot de passe incorrect.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Nouveau numéro de session
|
||||
* après authentification
|
||||
*/
|
||||
|
||||
session_regenerate_id(true);
|
||||
|
||||
|
||||
/*
|
||||
* Variables de session
|
||||
*/
|
||||
|
||||
$_SESSION['user_id'] =
|
||||
(int)$user['id'];
|
||||
|
||||
$_SESSION['username'] =
|
||||
$user['nom'];
|
||||
|
||||
$_SESSION['authenticated'] =
|
||||
true;
|
||||
|
||||
|
||||
/*
|
||||
* Dernière connexion
|
||||
*/
|
||||
|
||||
$sql = "
|
||||
UPDATE login
|
||||
SET dernier_login = NOW()
|
||||
WHERE id = :id
|
||||
";
|
||||
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':id' => $user['id']
|
||||
]);
|
||||
|
||||
|
||||
/*
|
||||
* Réponse
|
||||
*/
|
||||
|
||||
jsonResponse(
|
||||
'success',
|
||||
'Connexion réussie.',
|
||||
[
|
||||
'authenticated' => true,
|
||||
'user_id' => (int)$user['id'],
|
||||
'nom' => $user['nom']
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* ACTION INCONNUE
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Action inconnue.'
|
||||
);
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* CONNEXION MYSQL / PDO
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
$host = 'localhost';
|
||||
$db = 'GF';
|
||||
$user = 'root';
|
||||
$pass = 'sysadm-1963';
|
||||
|
||||
|
||||
try {
|
||||
|
||||
$pdo = new PDO(
|
||||
"mysql:host={$host};dbname={$db};charset=utf8mb4",
|
||||
$user,
|
||||
$pass,
|
||||
[
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false
|
||||
]
|
||||
);
|
||||
} catch (PDOException $e) {
|
||||
|
||||
http_response_code(500);
|
||||
|
||||
header(
|
||||
'Content-Type: application/json; charset=utf-8'
|
||||
);
|
||||
|
||||
echo json_encode(
|
||||
[
|
||||
'status' => 'error',
|
||||
'message' => 'Erreur de connexion à la base de données.'
|
||||
],
|
||||
JSON_UNESCAPED_UNICODE
|
||||
);
|
||||
|
||||
exit;
|
||||
}
|
||||
@@ -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