diff --git a/controllers/create_user.php b/controllers/create_user.php index d03daca..996f0f7 100644 --- a/controllers/create_user.php +++ b/controllers/create_user.php @@ -14,6 +14,9 @@ if (!isset($_SESSION["login"]) || !$_SESSION["is_admin"]) { // Récupérer les données de la session et du formulaire $admin_username = $_SESSION['sAMAccountName']; // sAMAccountName de l'admin connecté $ou_dn = $_POST['ou_dn']; // OU cible pour la création +$role = $_POST['role']; // Rôle de l'utilisateur + +$crypted_password = iconv('UTF-8', 'UTF-16LE', '"' . $_POST['password'] . '"'); // Récupérer les attributs du nouvel utilisateur depuis le formulaire $entry = [ @@ -22,8 +25,11 @@ $entry = [ 'givenName' => $_POST['firstname'], 'sn' => $_POST['lastname'], 'mail' => $_POST['email'], - 'userPassword' => $_POST['password'], 'sAMAccountName' => $_POST['username'], + 'userAccountControl' => '512', // Activer le compte + 'displayName' => $_POST['firstname'] . ' ' . $_POST['lastname'], + 'userPrincipalName' => $_POST['username'] . '@epul3a.local', + 'unicodePwd' => $crypted_password ]; // Vérifier que l'admin a les droits sur l'OU cible @@ -36,6 +42,20 @@ if (!in_array($ou_dn, $_SESSION['admin_ous'])) { try { $user_dn = "CN=" . $_POST['username'] . "," . $ou_dn; if ($ldapAuth->addUserWithAdminAuth($user_dn, $entry, $admin_username, $_SESSION['password'])) { + // Ajouter l'utilisateur au groupe d'administration si nécessaire + if ($role === 'admin') { + $group_dn = ''; + if ($ou_dn === 'OU=3AFISA,DC=epul3a,DC=local') { + $group_dn = 'CN=Domain Admins,CN=Users,DC=epul3a,DC=local'; + } elseif ($ou_dn === 'CN=Users,DC=epul3a,DC=local') { + $group_dn = 'CN=Enterprise Admins,CN=Users,DC=epul3a,DC=local'; + } + + if (!empty($group_dn)) { + $ldapAuth->addUserToGroup($user_dn, $group_dn, $admin_username, $_SESSION['password']); + } + } + header("Location: ../views/list_users.php?ou=" . urlencode($ou_dn)); } else { die("Erreur lors de la création."); diff --git a/controllers/edit_user.php b/controllers/edit_user.php index f7e94f7..c2bd0a2 100644 --- a/controllers/edit_user.php +++ b/controllers/edit_user.php @@ -29,8 +29,10 @@ if (!empty($_POST['email'])) { $attributes['mail'] = [$_POST['email']]; } +$_crypted_new_password = iconv('UTF-8', 'UTF-16LE', '"' . $_POST['new_password'] . '"'); + if (!empty($_POST['new_password'])) { - $attributes['userPassword'] = [$_POST['new_password']]; + $attributes['unicodePwd'] = $_crypted_new_password; } $ldapAuth = new LDAPAuth(); diff --git a/models/LDAPAuth.php b/models/LDAPAuth.php index b8ff268..47c342f 100644 --- a/models/LDAPAuth.php +++ b/models/LDAPAuth.php @@ -5,16 +5,29 @@ class LDAPAuth private $service_dn; private $service_pwd; private $ad; + private $ca_cert_file; public function __construct() { - $this->ldap_server = 'ldap://intranet.epul3a.local'; + $this->ldap_server = 'ldaps://intranet.epul3a.local'; $this->service_dn = 'CN=Service LDAP Reader,CN=Users,DC=epul3a,DC=local'; $this->service_pwd = 'Test@123'; + $this->ca_cert_file = 'c:\\certs\\root.pem'; } public function connect() { + if (!file_exists($this->ca_cert_file)) { + die("❌ Le fichier de certificat n'existe pas ou n'est pas lisible : " . $this->ca_cert_file); + } + + if (!is_readable($this->ca_cert_file)) { + die("❌ Impossible de lire le fichier de certificat : " . $this->ca_cert_file); + } + + ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, $this->ca_cert_file); + ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_DEMAND); + $this->ad = ldap_connect($this->ldap_server) or die("❌ Impossible de se connecter au LDAP"); @@ -54,8 +67,12 @@ class LDAPAuth if (@ldap_bind($this->ad, $user_dn, $user_password)) { // Vérifier si l'utilisateur est un administrateur $is_admin = $this->isUserAdmin($user_dn); - // Récupérer les OUs sur lesquelles l'utilisateur a des droits d'administration - $admin_ous = $this->getAdminOUs($user_dn); + // Récupérer les OUs sur lesquelles l'utilisateur a des droits d'administration (si admin) + if ($is_admin) { + $admin_ous = $this->getAdminOUs($user_dn); + } else { + $admin_ous = []; + } return [ 'success' => true, @@ -331,6 +348,20 @@ class LDAPAuth return $users; } + public function addUserToGroup($user_dn, $group_dn, $admin_username, $admin_password) + { + $this->connect(); + $this->bindWithCredentials($admin_username, $admin_password); + + $mod = [ + 'member' => $user_dn + ]; + + if (!@ldap_mod_add($this->ad, $group_dn, $mod)) { + throw new Exception("Erreur lors de l'ajout de l'utilisateur au groupe : " . ldap_error($this->ad)); + } + } + public function getUserDetails($username) { $this->connect(); @@ -366,7 +397,7 @@ class LDAPAuth $this->bindServiceAccount(); $filter = "(objectClass=user)"; - $attributes = ["Name", "sn", "givenName","mail", "sAMAccountName"]; + $attributes = ["Name", "sn", "givenName", "mail", "sAMAccountName"]; $result = ldap_read($this->ad, $dn, $filter, $attributes); $entries = ldap_get_entries($this->ad, $result); diff --git a/views/create_user.php b/views/create_user.php index 81ecbe7..9fd11ad 100644 --- a/views/create_user.php +++ b/views/create_user.php @@ -40,6 +40,12 @@ if (!isset($_SESSION["login"]) || !$_SESSION["is_admin"]) {

+ +

+