LDAP: couldn't connect to LDAP server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lpro:ldap:code_php [2016/01/12 16:48]
jbaudry [Ajout]
lpro:ldap:code_php [2016/01/13 10:12] (current)
jbaudry
Line 27: Line 27:
  
 <code php> <code php>
-//del group +//del user 
-function ​delGroup($dn) { +function ​delUser($dn) { 
- $ds=authenticate("login", "pass");+ //​authentification 
 +        ​$ds=ldap_connect("localhost"​);​ 
 +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); 
 +        $r=@ldap_bind($ds,"​cn=admin,​dc=univ-lehavre,​dc=fr","​azerty");
  $res=ldap_delete($ds,​$dn);​  $res=ldap_delete($ds,​$dn);​
  ldap_close($ds);​  ldap_close($ds);​
Line 52: Line 55:
         $info["​gidnumber"​]="​2025";​         $info["​gidnumber"​]="​2025";​
         ​         ​
-        $dn=$info["​cn"​].","​.$group_path ;+        $dn="​cn="​.$info["​cn"​].","​.$group_path ;
    
  $res = ldap_add($ds,​ $dn, $info);  $res = ldap_add($ds,​ $dn, $info);
  $res = ldap_err2str( ldap_errno($ds) );  $res = ldap_err2str( ldap_errno($ds) );
         ​         ​
 +        //fermeture de la connexion
 + ldap_close($ds);​
 + return $res ;
 +}
 +</​code>​
 +
 +<code php>
 +function addUser() {
 +        //​authentification
 +        $ds=ldap_connect("​localhost"​);​
 +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3);
 +        $r=@ldap_bind($ds,"​cn=admin,​dc=univ-lehavre,​dc=fr","​azerty"​);​
 + ​ var_dump($r);​
 +        //on indique l'​endroit où ajouter l'​element
 + $user_path="​ou=users,​dc=univ-lehavre,​dc=fr";​
 +        //on stocke les donnees dans un tableau
 + 
 +        $info["​cn"​] = "​newuser"​ ;
 +        $info["​objectclass"​][0] = "​account"​ ;
 +        $info["​objectclass"​][1] = "​posixAccount"​ ;
 +        $info["​objectclass"​][2] = "​top"​ ;
 +        $info["​userpassword"​] = "​pass";​
 +        $info["​loginshell"​] = "/​bin/​bash"​ ;
 +        $info["​uidnumber"​] = "​123"​ ;
 +        $info["​gidnumber"​] = "​345"​ ;
 +        $info["​homedirectory"​] = "/​home/​newuser"​ ;
 + 
 +        $dn="​uid=newuser,"​.$user_path ;
 + 
 + $res = ldap_add($ds,​ $dn, $info);
 + $res = ldap_err2str( ldap_errno($ds) );
 + 
         //fermeture de la connexion         //fermeture de la connexion
  ldap_close($ds);​  ldap_close($ds);​
Line 117: Line 152:
 <code php> <code php>
 function show_a_person2($host,​ $uidPerson, $rootdn, $rootpasswd) { function show_a_person2($host,​ $uidPerson, $rootdn, $rootpasswd) {
- $ds=ldap_connect($host); +        ​$ds=ldap_connect("​localhost"​); 
- ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); 
- $r=@ldap_bind($ds,​$rootdn,$rootpw); +        $r=@ldap_bind($ds,​"​cn=admin,dc=univ-lehavre,​dc=fr","​azerty"​);
- if($ds && $r) {+
  $sr=ldap_search($ds,​ $uidPerson, "​objectclass=*"​);​  $sr=ldap_search($ds,​ $uidPerson, "​objectclass=*"​);​
  $info = ldap_get_entries($ds,​ $sr);  $info = ldap_get_entries($ds,​ $sr);
 + $i=0;
  if(count($info)>​0) {  if(count($info)>​0) {
  $res["​cn"​] = $info[$i]["​cn"​][0];​  $res["​cn"​] = $info[$i]["​cn"​][0];​
- $res["​gecos"​] = $info[$i]["​gecos"​][0];​ 
  $res["​gidnumber"​] = $info[$i]["​gidnumber"​][0];​  $res["​gidnumber"​] = $info[$i]["​gidnumber"​][0];​
  $res["​homedirectory"​] = $info[$i]["​homedirectory"​][0];​  $res["​homedirectory"​] = $info[$i]["​homedirectory"​][0];​
Line 134: Line 168:
  }  }
  return $res ;  return $res ;
- } 
- else { 
- return "​Erreur de connexion"​ ; 
- } 
 } }
 +
 +$profil = show_a_person2("​localhost",​ "​uid=gaetan,​ou=users,​dc=univ-lehavre,​dc=fr",​ "​cn=admin,​dc=univ-lehavre,​dc=fr",​ "​azerty"​) ;
 +
 +echo "<​table border=1>"​ ;
 +foreach($profil as $k=>$v) {
 + echo "<​tr><​td>"​.$k."</​td><​td>"​.$v."</​td></​tr>"​ ;
 +}
 +echo "</​table>";​
 </​code>​ </​code>​