From 3b1749292e1cc7deeb89d5785c17f84475db4d26 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 2 Feb 2025 10:22:31 -0800 Subject: [PATCH] feat: add MVC base, with auth to ldap --- controllers/auth.php | 42 ++++++++++ controllers/controllerAdmin.php | 60 +++++++++++++++ controllers/create_user.php | 33 ++++++++ divers/connexion.php | 19 +++++ divers/hello.aspx | 13 ++++ divers/ldap.php | 6 ++ index.php | 23 ++++++ info.php | 2 + models/LDAPAuth.php | 52 +++++++++++++ php_errors.log | 132 ++++++++++++++++++++++++++++++++ views/auth.php | 49 ++++++++++++ views/menu.php | 17 ++++ 12 files changed, 448 insertions(+) create mode 100644 controllers/auth.php create mode 100644 controllers/controllerAdmin.php create mode 100644 controllers/create_user.php create mode 100644 divers/connexion.php create mode 100644 divers/hello.aspx create mode 100644 divers/ldap.php create mode 100644 index.php create mode 100644 info.php create mode 100644 models/LDAPAuth.php create mode 100644 php_errors.log create mode 100644 views/auth.php create mode 100644 views/menu.php diff --git a/controllers/auth.php b/controllers/auth.php new file mode 100644 index 0000000..e91735a --- /dev/null +++ b/controllers/auth.php @@ -0,0 +1,42 @@ +auth_model = new LDAPAuth(); + } + + public function login() + { + if (isset($_POST['user_pseudo']) && isset($_POST['user_password'])) { + $result = $this->auth_model->authenticate( + $_POST['user_pseudo'], + $_POST['user_password'] + ); + + if ($result['success']) { + $_SESSION['login'] = true; + $_SESSION['user_pseudo'] = $_POST['user_pseudo']; + $_SESSION['is_admin'] = $result['is_admin']; + $_SESSION['ldap_token'] = base64_encode($_POST['user_pseudo'] . ':' . $_POST['user_password']); + header('Location: index.php'); + exit; + } else { + $error_message = 'Identifiants incorrects'; + require_once __DIR__ . '/views/auth.php'; + } + } else { + require_once __DIR__ . '/views/auth.php'; + } + } +} + +session_start(); // Assure-toi que la session est bien démarrée +$controller = new AuthController(); +$controller->login(); diff --git a/controllers/controllerAdmin.php b/controllers/controllerAdmin.php new file mode 100644 index 0000000..a4d54eb --- /dev/null +++ b/controllers/controllerAdmin.php @@ -0,0 +1,60 @@ +Liste des OUs:"; + if ($entries['count'] > 0) { + foreach ($entries as $key => $entry) { + if (is_numeric($key)) { + echo "OU: " . $entry['ou'][0] . "
"; + echo "DN: " . $entry['distinguishedname'][0] . "

"; + } + } + } else { + echo "Aucune OU trouvée."; + } + } else { + echo "Error: " . ldap_error($ldapconn); + } + + ldap_close($ldapconn); +} diff --git a/controllers/create_user.php b/controllers/create_user.php new file mode 100644 index 0000000..6c8b13f --- /dev/null +++ b/controllers/create_user.php @@ -0,0 +1,33 @@ +Création d'un utilisateur"; +echo "
"; +echo "Nom
"; +echo "Prénom
"; +echo "Nom d'utilisateur
"; +echo "E-Mail
"; +echo "Mot de passe
"; +echo "Rôle dans l'OU
"; +echo ""; +echo "
"; + +require_once 'controllerAdmin.php'; +listAllOU(); diff --git a/divers/connexion.php b/divers/connexion.php new file mode 100644 index 0000000..265f2b1 --- /dev/null +++ b/divers/connexion.php @@ -0,0 +1,19 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + echo "Connexion réussie à MariaDB avec PDO!"; +} catch (PDOException $e) { + echo "Échec de la connexion : " . $e->getMessage(); +} + +$stmt = $pdo->query("SELECT * FROM users"); +$users = $stmt->fetchAll(PDO::FETCH_ASSOC); +print_r($users); +?> \ No newline at end of file diff --git a/divers/hello.aspx b/divers/hello.aspx new file mode 100644 index 0000000..998f41f --- /dev/null +++ b/divers/hello.aspx @@ -0,0 +1,13 @@ +<%@ Page Language="VB" %> +<% +HelloWorld.Text = "Hello World" +%> + + +ASP.NET Hello World + + +

+ + \ No newline at end of file diff --git a/divers/ldap.php b/divers/ldap.php new file mode 100644 index 0000000..d45fb3a --- /dev/null +++ b/divers/ldap.php @@ -0,0 +1,6 @@ +ldap_server = 'ldap://intranet.epul3a.local'; + $this->domain = 'intranet.epul3a.local'; + $this->FISA = 'OU=3AFISA,DC=epul3a,DC=local'; + $this->admin = 'CN=Users,DC=epul3a,DC=local'; + } + + public function connect() + { + $this->ad = ldap_connect($this->ldap_server) + or die("Impossible de se connecter au LDAP"); + ldap_set_option($this->ad, LDAP_OPT_PROTOCOL_VERSION, 3); + ldap_set_option($this->ad, LDAP_OPT_REFERRALS, 0); + } + + public function authenticate($user_pseudo, $user_password) + { + $this->connect(); + + $ldap_user_3afisa = "CN=$user_pseudo,$this->FISA"; + $ldap_user_admin = "CN=$user_pseudo,$this->admin"; + + $bind_3afisa = @ldap_bind($this->ad, $ldap_user_3afisa, $user_password); + $bind_admin = false; + + if (!$bind_3afisa) { + $bind_admin = @ldap_bind($this->ad, $ldap_user_admin, $user_password); + } + + if ($bind_3afisa || $bind_admin) { + return ['success' => true, 'is_admin' => $bind_admin]; + } + return ['success' => false]; + } + + public function close() + { + if ($this->ad) { + ldap_close($this->ad); + } + } +} diff --git a/php_errors.log b/php_errors.log new file mode 100644 index 0000000..7dc43c2 --- /dev/null +++ b/php_errors.log @@ -0,0 +1,132 @@ +[31-Jan-2025 14:48:30 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.users' doesn't exist in C:\Site\connexion.php:16 +Stack trace: +#0 C:\Site\connexion.php(16): PDO->query('SELECT * FROM u...') +#1 {main} + thrown in C:\Site\connexion.php on line 16 +[31-Jan-2025 15:17:56 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[31-Jan-2025 15:17:57 UTC] PHP Warning: Undefined variable $user_pseudo in C:\Site\auth.php on line 29 +[31-Jan-2025 15:17:57 UTC] PHP Warning: Undefined variable $user_password in C:\Site\auth.php on line 30 +[31-Jan-2025 15:18:50 UTC] PHP Warning: Undefined variable $user_pseudo in C:\Site\auth.php on line 29 +[31-Jan-2025 15:18:50 UTC] PHP Warning: Undefined variable $user_password in C:\Site\auth.php on line 30 +[31-Jan-2025 15:29:03 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[31-Jan-2025 15:31:38 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:31:55 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:32:18 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:33:59 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:34:11 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:36:54 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:37:03 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:39:23 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:46:59 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:48:49 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:49:22 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 15:49:38 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 20 +[31-Jan-2025 16:05:43 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[31-Jan-2025 16:08:44 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[31-Jan-2025 16:12:08 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[02-Feb-2025 10:00:09 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[02-Feb-2025 10:03:11 UTC] PHP Warning: Undefined array key "login" in C:\Site\menu.php on line 3 +[02-Feb-2025 10:14:29 UTC] PHP Warning: Undefined array key "is_admin" in C:\Site\menu.php on line 10 +[02-Feb-2025 10:14:48 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 31 +[02-Feb-2025 10:16:23 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 31 +[02-Feb-2025 10:20:53 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 31 +[02-Feb-2025 10:24:33 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\auth.php on line 35 +[02-Feb-2025 10:46:39 UTC] PHP Warning: Undefined global variable $_SESSION in C:\Site\create_user.php on line 2 +[02-Feb-2025 10:46:39 UTC] PHP Warning: Trying to access array offset on value of type null in C:\Site\create_user.php on line 2 +[02-Feb-2025 10:50:51 UTC] PHP Warning: Undefined global variable $_SESSION in C:\Site\create_user.php on line 2 +[02-Feb-2025 10:50:51 UTC] PHP Warning: Trying to access array offset on value of type null in C:\Site\create_user.php on line 2 +[02-Feb-2025 15:44:25 UTC] PHP Warning: ldap_bind(): Unable to bind to server: Invalid credentials in C:\Site\create_user.php on line 41 +[02-Feb-2025 15:44:25 UTC] PHP Warning: ldap_search(): Search: Operations error in C:\Site\create_user.php on line 44 +[02-Feb-2025 15:44:25 UTC] PHP Fatal error: Uncaught TypeError: ldap_get_entries(): Argument #2 ($result) must be of type LDAP\Result, bool given in C:\Site\create_user.php:45 +Stack trace: +#0 C:\Site\create_user.php(45): ldap_get_entries(Object(LDAP\Connection), false) +#1 C:\Site\create_user.php(32): listAllOU() +#2 {main} + thrown in C:\Site\create_user.php on line 45 +[02-Feb-2025 15:47:31 UTC] PHP Warning: ldap_search(): Search: Operations error in C:\Site\create_user.php on line 53 +[02-Feb-2025 15:48:22 UTC] PHP Warning: Undefined array key "user_password" in C:\Site\create_user.php on line 44 +[02-Feb-2025 15:48:22 UTC] PHP Warning: ldap_search(): Search: Operations error in C:\Site\create_user.php on line 52 +[02-Feb-2025 15:49:05 UTC] PHP Warning: Undefined array key "user_password" in C:\Site\create_user.php on line 44 +[02-Feb-2025 15:49:05 UTC] PHP Warning: ldap_search(): Search: Operations error in C:\Site\create_user.php on line 52 +[02-Feb-2025 16:22:37 UTC] PHP Fatal error: Uncaught Error: Class "LDAPController" not found in C:\Site\auth.php:3 +Stack trace: +#0 {main} + thrown in C:\Site\auth.php on line 3 +[02-Feb-2025 16:22:39 UTC] PHP Fatal error: Uncaught Error: Class "LDAPController" not found in C:\Site\auth.php:3 +Stack trace: +#0 {main} + thrown in C:\Site\auth.php on line 3 +[02-Feb-2025 16:22:39 UTC] PHP Fatal error: Uncaught Error: Call to undefined function listAllOU() in C:\Site\create_user.php:32 +Stack trace: +#0 {main} + thrown in C:\Site\create_user.php on line 32 +[02-Feb-2025 16:23:07 UTC] PHP Fatal error: Uncaught Error: Class "LDAPController" not found in C:\Site\auth.php:3 +Stack trace: +#0 {main} + thrown in C:\Site\auth.php on line 3 +[02-Feb-2025 16:24:12 UTC] PHP Warning: require_once(LDAPController.php): Failed to open stream: No such file or directory in C:\Site\auth.php on line 3 +[02-Feb-2025 16:24:12 UTC] PHP Fatal error: Uncaught Error: Failed opening required 'LDAPController.php' (include_path='.;C:\php\pear') in C:\Site\auth.php:3 +Stack trace: +#0 {main} + thrown in C:\Site\auth.php on line 3 +[02-Feb-2025 16:24:36 UTC] PHP Warning: Undefined variable $user_pseudo in C:\Site\auth.php on line 6 +[02-Feb-2025 16:24:36 UTC] PHP Warning: Undefined variable $user_password in C:\Site\auth.php on line 6 +[02-Feb-2025 16:28:19 UTC] PHP Warning: require_once(classes/LDAPAuth.php): Failed to open stream: No such file or directory in C:\Site\auth.php on line 2 +[02-Feb-2025 16:28:19 UTC] PHP Fatal error: Uncaught Error: Failed opening required 'classes/LDAPAuth.php' (include_path='.;C:\php\pear') in C:\Site\auth.php:2 +Stack trace: +#0 {main} + thrown in C:\Site\auth.php on line 2 +[02-Feb-2025 16:32:46 UTC] PHP Warning: require_once(classes/LDAPAuth.php): Failed to open stream: No such file or directory in C:\Site\vues\auth.php on line 2 +[02-Feb-2025 16:32:46 UTC] PHP Fatal error: Uncaught Error: Failed opening required 'classes/LDAPAuth.php' (include_path='.;C:\php\pear') in C:\Site\vues\auth.php:2 +Stack trace: +#0 {main} + thrown in C:\Site\vues\auth.php on line 2 +[02-Feb-2025 16:32:47 UTC] PHP Warning: require_once(classes/LDAPAuth.php): Failed to open stream: No such file or directory in C:\Site\vues\auth.php on line 2 +[02-Feb-2025 16:32:47 UTC] PHP Fatal error: Uncaught Error: Failed opening required 'classes/LDAPAuth.php' (include_path='.;C:\php\pear') in C:\Site\vues\auth.php:2 +Stack trace: +#0 {main} + thrown in C:\Site\vues\auth.php on line 2 +[02-Feb-2025 17:36:03 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\views\menu.php on line 2 +[02-Feb-2025 17:36:03 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\views\menu.php on line 2 +[02-Feb-2025 17:37:42 UTC] PHP Warning: ldap_search(): Search: Operations error in C:\Site\controllers\controllerAdmin.php on line 25 +[02-Feb-2025 17:39:26 UTC] PHP Warning: ldap_search(): Search: Operations error in C:\Site\controllers\controllerAdmin.php on line 25 +[02-Feb-2025 17:45:20 UTC] PHP Warning: Undefined array key "ldap_token" in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:45:20 UTC] PHP Deprecated: base64_decode(): Passing null to parameter #1 ($string) of type string is deprecated in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:45:20 UTC] PHP Warning: Undefined array key 1 in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:28 UTC] PHP Warning: Undefined array key "ldap_token" in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:28 UTC] PHP Deprecated: base64_decode(): Passing null to parameter #1 ($string) of type string is deprecated in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:28 UTC] PHP Warning: Undefined array key 1 in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:29 UTC] PHP Warning: Undefined array key "ldap_token" in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:29 UTC] PHP Deprecated: base64_decode(): Passing null to parameter #1 ($string) of type string is deprecated in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:29 UTC] PHP Warning: Undefined array key 1 in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:30 UTC] PHP Warning: Undefined array key "ldap_token" in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:30 UTC] PHP Deprecated: base64_decode(): Passing null to parameter #1 ($string) of type string is deprecated in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:46:30 UTC] PHP Warning: Undefined array key 1 in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:48:24 UTC] PHP Warning: Undefined array key "ldap_token" in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:48:24 UTC] PHP Deprecated: base64_decode(): Passing null to parameter #1 ($string) of type string is deprecated in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:48:24 UTC] PHP Warning: Undefined array key 1 in C:\Site\controllers\controllerAdmin.php on line 15 +[02-Feb-2025 17:51:43 UTC] PHP Warning: require_once(../models/LDAPAuth.php): Failed to open stream: No such file or directory in C:\Site\controllers\auth.php on line 2 +[02-Feb-2025 17:51:43 UTC] PHP Fatal error: Uncaught Error: Failed opening required '../models/LDAPAuth.php' (include_path='.;C:\php\pear') in C:\Site\controllers\auth.php:2 +Stack trace: +#0 C:\Site\index.php(12): require_once() +#1 {main} + thrown in C:\Site\controllers\auth.php on line 2 +[02-Feb-2025 17:59:14 UTC] PHP Warning: require(/views/auth.php): Failed to open stream: No such file or directory in C:\Site\index.php on line 6 +[02-Feb-2025 17:59:14 UTC] PHP Fatal error: Uncaught Error: Failed opening required '/views/auth.php' (include_path='.;C:\php\pear') in C:\Site\index.php:6 +Stack trace: +#0 {main} + thrown in C:\Site\index.php on line 6 +[02-Feb-2025 17:59:15 UTC] PHP Warning: require(/views/auth.php): Failed to open stream: No such file or directory in C:\Site\index.php on line 6 +[02-Feb-2025 17:59:15 UTC] PHP Fatal error: Uncaught Error: Failed opening required '/views/auth.php' (include_path='.;C:\php\pear') in C:\Site\index.php:6 +Stack trace: +#0 {main} + thrown in C:\Site\index.php on line 6 +[02-Feb-2025 18:02:55 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 38 +[02-Feb-2025 18:03:27 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 38 +[02-Feb-2025 18:04:48 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 38 +[02-Feb-2025 18:09:49 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 2 +[02-Feb-2025 18:09:49 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 40 +[02-Feb-2025 18:11:56 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 2 +[02-Feb-2025 18:11:56 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 40 +[02-Feb-2025 18:14:05 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 2 +[02-Feb-2025 18:14:05 UTC] PHP Notice: session_start(): Ignoring session_start() because a session is already active in C:\Site\controllers\auth.php on line 40 diff --git a/views/auth.php b/views/auth.php new file mode 100644 index 0000000..9f64516 --- /dev/null +++ b/views/auth.php @@ -0,0 +1,49 @@ + + + + + + + Authentification + + + +
+ +
+ + +
+ + +
+ +
+ + + + + \ No newline at end of file diff --git a/views/menu.php b/views/menu.php new file mode 100644 index 0000000..b88ff82 --- /dev/null +++ b/views/menu.php @@ -0,0 +1,17 @@ +Admin Dashboard"; + echo "Créer un utilisateur
"; + echo "Modifier un utilisateur
"; + echo "Supprimer un utilisateur
"; +} else { + echo "

Utilisateur Standard

"; + echo "

Vous pouvez consulter votre profil.

"; +}