-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_admin_cli.php
More file actions
100 lines (82 loc) · 2.9 KB
/
create_admin_cli.php
File metadata and controls
100 lines (82 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env php
<?php
// Configuration BD
$host = 'localhost';
$dbname = 'kadodo_db';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Erreur de connexion: " . $e->getMessage() . "\n");
}
// Fonctions helpers
function prompt($message)
{
echo $message;
return trim(fgets(STDIN));
}
function validateEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
// Interface CLI
echo "=== Création d'un nouvel administrateur ===\n\n";
$nom = prompt("Nom: ");
while (empty($nom)) {
echo "Le nom est requis!\n";
$nom = prompt("Nom: ");
}
$prenom = prompt("Prénom: ");
while (empty($prenom)) {
echo "Le prénom est requis!\n";
$prenom = prompt("Prénom: ");
}
$email = prompt("Email: ");
while (!validateEmail($email)) {
echo "Email invalide!\n";
$email = prompt("Email: ");
}
$password = prompt("Mot de passe: ");
while (strlen($password) < 6) {
echo "Le mot de passe doit contenir au moins 6 caractères!\n";
$password = prompt("Mot de passe: ");
}
echo "\nDépartements disponibles:\n";
echo "1) SYSTEM\n";
echo "2) SCOLARITE\n";
echo "3) COMPTABILITE\n";
echo "4) DIRECTION\n";
$dept_choice = prompt("Choisir le département (1-4): ");
$departments = ['SYSTEM', 'SCOLARITE', 'COMPTABILITE', 'DIRECTION'];
while (!isset($departments[$dept_choice - 1])) {
echo "Choix invalide!\n";
$dept_choice = prompt("Choisir le département (1-4): ");
}
$departement = $departments[$dept_choice - 1];
try {
$pdo->beginTransaction();
// Générer matricule unique
$stmt = $pdo->query("SELECT MAX(CAST(SUBSTRING(matricule, 5) AS SIGNED)) as max_num FROM utilisateur WHERE matricule LIKE 'ADM-%'");
$result = $stmt->fetch();
$next_num = ($result['max_num'] ?? 0) + 1;
$matricule = "ADM-" . $next_num;
// Insérer utilisateur
$stmt = $pdo->prepare("INSERT INTO utilisateur (nom, prenom, email, mot_de_passe, id_role, matricule, date_creation, date_mise_a_jour) VALUES (?, ?, ?, ?, 3, ?, NOW(), NOW())");
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt->execute([$nom, $prenom, $email, $hashed_password, $matricule]);
$id_utilisateur = $pdo->lastInsertId();
// Insérer administrateur
$stmt = $pdo->prepare("INSERT INTO administrateur (id_utilisateur, departement_admin) VALUES (?, ?)");
$stmt->execute([$id_utilisateur, $departement]);
$pdo->commit();
echo "\n=== Admin créé avec succès! ===\n";
echo "Matricule: $matricule\n";
echo "Nom: $nom $prenom\n";
echo "Email: $email\n";
echo "Département: $departement\n";
} catch (Exception $e) {
$pdo->rollBack();
echo "\nErreur: " . $e->getMessage() . "\n";
}