Pregunta

I need a way to retrive a list of all the users from Ldap. All i need from them is their names to display them in a page.

this is my ldap connection page that retrieve information regarding one user. I need this + the name of all the users.

code:

session_start();

include 'includes/connect.php';

// Set the ldap server
$ldapurl = "bpt.kme.intern";
// Set the debug flag
$debug = true;

// Set debugging
if ($debug) {
  ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
}

// connect to ldap server
$ldapconn = ldap_connect($ldapurl) or die ("Couldn't connect"); 


$ldapuser= $_POST['email'];
$ldappass= $_POST['pass'];
$ldaptree    = "OU=BPT_Users,DC=bpt,DC=kme,DC=intern";

echo $ldapuser;
echo $ldappass;


// binding to ldap server
//echo "Trying to bind with $ldapuser - $ldappass\n";
$ldapbind = @ldap_bind($ldapconn, $ldapuser, $ldappass);

if (!$ldapbind) {
echo "Unable to bind to server $ldapurl\n";
echo "OpenLdap error message: " . ldap_error($ldapconn) . "\n";
$erro = "Dados de Login Inválidos";
$_SESSION['erro']=$erro;
header('location:index.php');


}



if ($ldapbind) {


     $result = ldap_search($ldapconn,$ldaptree, "(mail=".$ldapuser.")") or die ("Error in search query: ".ldap_error($ldapconn));

     //$data = ldap_get_entries($ldapconn, $result);
      // SHOW ALL DATA
     //echo '<h1>Dump all data</h1><pre>';
    //print_r($data);   
     //echo '</pre>';

    $entry = ldap_first_entry($ldapconn, $result);
    //Title
    $title = ldap_get_values($ldapconn, $entry, "title");
    //Manager
    $manager = ldap_get_values($ldapconn, $entry, "manager");
    //ACC name
    $samaccountname = ldap_get_values($ldapconn, $entry, "samaccountname");
    //Name
    $nome = ldap_get_values($ldapconn, $entry, "name");


    $_SESSION['logged']=true;
    $_SESSION['email'] = $ldapuser;
    $_SESSION['pass'] = $ldappass;
    $_SESSION['title']=$title;
    $_SESSION['manager']=$manager;
    $_SESSION['colab']=$samaccountname;
    $_SESSION['nome']=$nome;


}

Thanks in advance

¿Fue útil?

Solución

What you are looking for is the right LDAP filter to use to retrieve all users. This depends on what the entries look like - try to figure out what they have in common. For now, I suppose that all users have a mail attribute and share the objectClass organizationalPerson. In that case, retrieve all the entries like this:

$result = ldap_search($ldapconn,$ldaptree, "(&(mail=*)(objectClass=organizationalPerson))") or die ("Error in search query: ".ldap_error($ldapconn));

Then go ahead, either as you tried in your example, or using the ldap_next_entry method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top