feat: implement user creation, editing, and deletion functionality ALL features are working
This commit is contained in:
@@ -359,4 +359,91 @@ class LDAPAuth
|
||||
}
|
||||
return $clean;
|
||||
}
|
||||
|
||||
public function getUserDetailsByDn($dn)
|
||||
{
|
||||
$this->connect();
|
||||
$this->bindServiceAccount();
|
||||
|
||||
$filter = "(objectClass=user)";
|
||||
$attributes = ["givenName", "sn", "mail", "sAMAccountName"];
|
||||
$result = ldap_read($this->ad, $dn, $filter, $attributes);
|
||||
$entries = ldap_get_entries($this->ad, $result);
|
||||
|
||||
return $this->cleanLdapEntries($entries[0]);
|
||||
}
|
||||
|
||||
public function updateUser($dn, $attributes)
|
||||
{
|
||||
$this->connect();
|
||||
$this->bindServiceAccount();
|
||||
|
||||
return ldap_modify($this->ad, $dn, $attributes);
|
||||
}
|
||||
|
||||
public function getUserOUFromDN($dn)
|
||||
{
|
||||
// Extraire l'OU ou le conteneur parent
|
||||
$parts = explode(',', $dn);
|
||||
foreach ($parts as $part) {
|
||||
if (strpos($part, 'OU=') === 0) {
|
||||
return $part . ",DC=epul3a,DC=local";
|
||||
}
|
||||
}
|
||||
// Si l'utilisateur est dans CN=Users, retourner CN=Users
|
||||
if (strpos($dn, 'CN=Users,DC=epul3a,DC=local') !== false) {
|
||||
return "CN=Users,DC=epul3a,DC=local";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getAdConnection()
|
||||
{
|
||||
return $this->ad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprime un utilisateur en utilisant les identifiants de l'admin connecté
|
||||
*/
|
||||
public function deleteUserWithAdminAuth($user_dn, $admin_username, $admin_password)
|
||||
{
|
||||
$this->connect();
|
||||
$this->bindWithCredentials($admin_username, $admin_password);
|
||||
|
||||
if (!@ldap_delete($this->ad, $user_dn)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Met à jour un utilisateur avec les identifiants de l'admin
|
||||
*/
|
||||
public function updateUserWithAdminAuth($user_dn, $attributes, $admin_username, $admin_password)
|
||||
{
|
||||
$this->connect();
|
||||
$this->bindWithCredentials($admin_username, $admin_password);
|
||||
return ldap_modify($this->ad, $user_dn, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute un nouvel utilisateur avec les droits de l'admin
|
||||
*/
|
||||
public function addUserWithAdminAuth($dn, $entry, $admin_username, $admin_password)
|
||||
{
|
||||
$this->connect();
|
||||
$this->bindWithCredentials($admin_username, $admin_password);
|
||||
return ldap_add($this->ad, $dn, $entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Méthode générique pour lier la connexion avec des identifiants
|
||||
*/
|
||||
private function bindWithCredentials($username, $password)
|
||||
{
|
||||
$admin_dn = $this->getUserDN($username); // Récupérer le DN de l'admin
|
||||
if (!@ldap_bind($this->ad, $admin_dn, $password)) {
|
||||
throw new Exception("Erreur d'authentification : " . ldap_error($this->ad));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user