This commit is contained in:
2025-03-27 21:00:25 +01:00
parent 513b7563b6
commit 9e21bdc135
3 changed files with 54 additions and 18 deletions

View File

@@ -1,14 +1,18 @@
<?php
$table_name = "authentification_attempts";
/**
* Connexion à la base de données
*/
function GetDbConnection(): ?PDO
{
$host = 'localhost'; // ou l'adresse IP du serveur MariaDB
$dbname = 'ldap'; // nom de votre base de donn<6E>es
$username = 'root'; // nom d'utilisateur MariaDB
$password = '4321'; // mot de passe pour l'utilisateur
$host = 'localhost';
$dbname = 'ldap';
$username = 'root';
$password = '4321';
try {
// Cr<EFBFBD>ation d'une instance PDO pour la connexion <EFBFBD> la base de donn<EFBFBD>es
// Création d'une instance PDO pour la connexion à la base de données
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// Configuration du mode d'erreur de PDO pour les exceptions
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@@ -39,9 +43,12 @@ class AuthAttempt
}
}
/**
* Ajoute une entrée dans la table des tentatives de connexion
*/
function InsertLine(AuthAttempt $attempt)
{
$table_name = "authentification_attempts";
global $table_name;
$pdo = GetDbConnection();
$query = $pdo->prepare("INSERT INTO $table_name(`username`, `status`, `ip_address`) VALUES (:user, :status, :ip);");
@@ -53,8 +60,11 @@ function InsertLine(AuthAttempt $attempt)
$query->execute();
}
/**
* Récupère toutes les lignes de tentative de connexion
*/
function GetLines() : array {
$table_name = "authentification_attempts";
global $table_name;
$pdo = GetDbConnection();
$query = $pdo->prepare("SELECT * FROM $table_name;");

View File

@@ -23,6 +23,9 @@ class UserInfo
}
}
/**
* Connexion avec des identifiants
*/
function LdapConnect(string $domain, string $username, string $password): LDAP\Result|false
{
global $handle;
@@ -30,7 +33,10 @@ function LdapConnect(string $domain, string $username, string $password): LDAP\R
return $bind;
}
function LdapIsConnected(string $domain, string $username, string $password) {
/**
* Se connecte à l'AD et vérifie la validité des identifiants
*/
function LdapIsConnected(string $domain, string $username, string $password) : bool {
global $handle;
$result = LdapConnect($domain, $username, $password);
ldap_parse_result($handle, $result, $error_code, $matched_dn, $error_message, $referrals, $controls);
@@ -39,20 +45,16 @@ function LdapIsConnected(string $domain, string $username, string $password) {
return $success;
}
/**
* Enregistre la tentative de connexion dans la base de données
*/
function LogConnection(string $username, bool $success) {
InsertLine(new AuthAttempt($username, $success ? "success" : "failure", $_SERVER['REMOTE_ADDR']));
}
function LdapConnectAndBind()
{
global $ldap_domain_name;
$ldap_instance = ldap_connect("ldap://$ldap_domain_name.local");
ldap_set_option($ldap_instance, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_instance, LDAP_OPT_REFERRALS, 0);
ldap_bind($ldap_instance, "Administrateur@woodywood", "3AFISE+25");
return $ldap_instance;
}
/**
* Retourne les informations d'un utilisateur
*/
function LdapGetUserInfo(string $user): ?UserInfo
{
global $handle;
@@ -71,12 +73,18 @@ function LdapGetUserInfo(string $user): ?UserInfo
return null;
}
/**
* Permet de retourner les valeur dans une chaîne de caractère de type dn
*/
function GetValue(string $dnStr, string $key): array
{
preg_match_all("/$key=([^,]+)/", $dnStr, $matches);
return isset($matches[1]) ? $matches[1] : [];
}
/**
* Retourne tous les objets du type spécifié dans l'ou souhaitée et filtre le résultat
*/
function LdapGetObjectsInOU(string $ou, string $objectType, string $field): array
{
global $ldap_domain_name;
@@ -110,11 +118,17 @@ function LdapGetObjectsInOU(string $ou, string $objectType, string $field): arra
return $object_values;
}
/**
* Retourne la liste des utilisateurs d'une OU
*/
function LdapGetUsersInOU(string $ou): array
{
return LdapGetObjectsInOU($ou, "user", "CN");
}
/**
* Retourne la liste des groupes d'une OU
*/
function LdapGetGroupsInOU(string $ou): array
{
return LdapGetObjectsInOU($ou, "group", "CN");

View File

@@ -2,6 +2,9 @@
$admin_account = "Administrateur";
/**
* Affiche les éléments d'une liste
*/
function PrintListFirsts(string $title, array $liste): string
{
$result = '<div class="list-group-item"><h5>' . $title . '</h5>';
@@ -13,6 +16,9 @@ function PrintListFirsts(string $title, array $liste): string
return $result;
}
/**
* Affiche les informations de l'utilisateur
*/
function PrintLoginInfo($info)
{
global $admin_account;
@@ -33,10 +39,16 @@ function PrintLoginInfo($info)
return $body;
}
/**
* Retourne la chaîne traduite de l'état d'une tentative de connexion
*/
function translateSuccess(string $success) {
return $success == "success" ? "Succès" : "Échec";
}
/**
* Affiche l'interface administrateur
*/
function PrintAdminInterface(): string
{
$auth_attempts = GetLines();