phpactive-directoryldapdistinguishedname

Active Directory search using DN (distinguished name)


I am working on a PHP function to get the manager of an active directory user(using the users email address). After getting the manager, I want to get the managers e-mail address.

I use this code to get the manager:

    //Search using a filter.
    $result = ldap_search($connect,$ldaptree, "(mail=useremail@domain.de)") or die ("Error in search query: ".ldap_error($connect));
    $data = ldap_get_entries($connect, $result);


    // iterate over array and print data for each entry
    echo '<h1>Show me the users</h1>';
    for ($i=0; $i<$data["count"]; $i++) {

        echo "Manager: " . print_r($data[$i]["manager"]) . "<br />";

The code is working and I am getting correct values when searching for users email or other attributes. But when I am searching for the manager echo "Manager: " . print_r($data[$i]["manager"]) . "<br />"; then I get the DN (distinguished name) of the manager. For example: "Array ( [count] => 1 [0] => CN=LASTNAME\, FIRSTNAME,OU=01_User,DC=int,DC=domain,DC=de ) Manager: 1"

Now the problem is, when I try to search for the managers email address, using the DN as filter

$result = ldap_search($connect,$ldaptree, "(DN=".$data[$i]["manager"]."") or die ("Error in search query: ".ldap_error($connect));

Then I get an "Array to string convertion error". If I use print_r($data[$i]) to convert to string, then I get "Error in search query: Bad search filter".

So my question is, how can I use the DN to get the attributes of the user behind the DN? Is it possible to filter for a DN? Do I have to process the DN string?

Hope someone can help me. Thank you!


Solution

  • You should be able to retrieve the Manager by calling ldap_search directly with the DN of the manager as BaseDN and a filter of (objectclass=*)

    $result = ldap_search($connect, $data[$i]['manager'][0], '(objectclass=*)');
    

    I updated the answer with the feedback from the OP.