PHP LDAP Get User Attributes, Including Associated Groups

后端 未结 1 566
心在旅途
心在旅途 2020-12-31 11:57

What is the best way to run a search on the current user to retrieve all attributes, including associated groups in Active Directory using LDAP / PHP?

For attributes

相关标签:
1条回答
  • 2020-12-31 12:55

    Here's a script we have for dumping AD information, maybe it will help you:

    <?php
    $ldap_columns = NULL;
    $ldap_connection = NULL;
    $ldap_password = 'top_secret_password';
    $ldap_username = 'top_secret_username@'.LDAP_DOMAIN;
    
    //------------------------------------------------------------------------------
    // Connect to the LDAP server.
    //------------------------------------------------------------------------------
    $ldap_connection = ldap_connect(LDAP_HOSTNAME);
    if (FALSE === $ldap_connection){
        die("<p>Failed to connect to the LDAP server: ". LDAP_HOSTNAME ."</p>");
    }
    
    ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
    ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
    
    if (TRUE !== ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
        die('<p>Failed to bind to LDAP server.</p>');
    }
    
    //------------------------------------------------------------------------------
    // Get a list of all Active Directory users.
    //------------------------------------------------------------------------------
    $ldap_base_dn = 'DC=xyz,DC=local';
    $search_filter = "(&(objectCategory=person))";
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter);
    if (FALSE !== $result){
        $entries = ldap_get_entries($ldap_connection, $result);
        if ($entries['count'] > 0){
            $odd = 0;
            foreach ($entries[0] AS $key => $value){
                if (0 === $odd%2){
                    $ldap_columns[] = $key;
                }
                $odd++;
            }
            echo '<table class="data">';
            echo '<tr>';
            $header_count = 0;
            foreach ($ldap_columns AS $col_name){
                if (0 === $header_count++){
                    echo '<th class="ul">';
                }else if (count($ldap_columns) === $header_count){
                    echo '<th class="ur">';
                }else{
                    echo '<th class="u">';
                }
                echo $col_name .'</th>';
            }
            echo '</tr>';
            for ($i = 0; $i < $entries['count']; $i++){
                echo '<tr>';
                $td_count = 0;
                foreach ($ldap_columns AS $col_name){
                    if (0 === $td_count++){
                        echo '<td class="l">';
                    }else{
                        echo '<td>';
                    }
                    if (isset($entries[$i][$col_name])){
                        $output = NULL;
                        if ('lastlogon' === $col_name || 'lastlogontimestamp' === $col_name){
                            $output = date('D M d, Y @ H:i:s', ($entries[$i][$col_name][0] / 10000000) - 11676009600); // See note below
                        }else{
                            $output = $entries[$i][$col_name][0];
                        }
                        echo $output .'</td>';
                    }
                }
                echo '</tr>';
            }
            echo '</table>';
        }
    }
    ldap_unbind($ldap_connection); // Clean up after ourselves.
    ?>
    

    User inventor96 has suggested using 11644473600 instead of 11676009600. I can confirm 11644473600 is correct in a Linux environment - my guess is that inventor96 is in a Windows environment.

    0 讨论(0)
提交回复
热议问题