2013-01-16 18 views
6

एलडीएपी/PHP का उपयोग कर सक्रिय निर्देशिका में संबंधित समूहों समेत सभी विशेषताओं को पुनर्प्राप्त करने के लिए वर्तमान उपयोगकर्ता पर खोज चलाने का सबसे अच्छा तरीका क्या है?PHP एलडीएपी एसोसिएटेड समूह समेत उपयोगकर्ता विशेषताओं को प्राप्त करें

विशेषताओं के लिए, मुख्य रूप से केवल पहला नाम, अंतिम नाम, और प्रदर्शन नाम।

संबंधित समूहों के लिए, केवल सदस्य जो सदस्य हैं, जैसे सदस्य सदस्य हैं।

मैंने कुछ विकल्प आज़माए हैं, लेकिन सही फ़िल्टर/खोज संयोजन नहीं लग रहा है और अधिकांश उदाहरण उन उपयोगकर्ताओं की सूचियों को पुनर्प्राप्त करने के लिए कवर करते हैं जहां एक ज्ञात समूह है।

मैं एक सफल बाँध के बाद यह चल रहा है की कोशिश की है:

$attributes = array("displayname"); 
$filter = "(&(sAMAccountName=$username))"; 
$result = ldap_search($ds, $ldapconfig['basedn'], $filter, $attributes); 
$entries = ldap_get_entries($ds, $result); 
if($entries["count"] > 0){ 
    echo "displayName: ".$entries[0]['displayname'][0]."<br/>"; 
} else { 
echo("msg:'".ldap_error($ds)."'</br>"); 
} 

निम्नलिखित में से कौन त्रुटि देता है: "कोई ऐसी वस्तु"।

अद्यतन:

यह नवीनतम ब्लॉक मैंने कोशिश की और परिणाम प्राप्त करने के लिए जब मैं $ जानकारी चर print_r, हालांकि खंड के लिए अभी भी कहीं न कहीं बाहर स्खलित है कर रहा हूँ गया है। मैं सिर्फ डीसी को BaseDN बदल जिम्मेदार बताते हैं:

$filter="($SearchField=$SearchFor)"; 
$sr=ldap_search($ds, $basedn, $filter, $LDAPFieldsToFind); 
$info = ldap_get_entries($ds, $sr); 

if($info["count"] > 0) { 
    for ($x=0; $x<$info["count"]; $x++) { 
     $sam=$info[$x]['samaccountname'][0]; 
     $giv=$info[$x]['givenname'][0]; 
     $tel=$info[$x]['telephonenumber'][0]; 
     $email=$info[$x]['mail'][0]; 
     $nam=$info[$x]['cn'][0]; 
     $dir=$info[$x]['homedirectory'][0]; 
     $dir=strtolower($dir); 
     $pos=strpos($dir,"home"); 
     $pos=$pos+5; 
      if (stristr($sam, $SearchFor) && (strlen($dir) > 8)) { 
       print "\nActive Directory says that:\n"; 
       print "CN is: ".$nam." \n"; 
       print "SAMAccountName is: ".$sam." \n"; 
       print "Given Name is: ".$giv." \n"; 
       print "Telephone is: ".$tel." \n"; 
       print "Home Directory is: ".$dir." \n"; 
      } 
    } 
    } 

परिणामों की print_r हैं:

([count] => 1 [0] => Array ([cn] => Array ([count] => 1 [0] => George) [0] => cn [givenname] => Array ([count] => 1 [0] => George) [1] => givenname [memberof] => Array ([count] => 4 [0] => CN=EQCStaff,CN=Users,DC=EQC,DC=local [1] => CN=RDS Users,OU=Security Groups,OU=Service,DC=EQC,DC=local [2] => CN=SFTP Client Folders,OU=Security Groups,OU=Service,DC=EQC,DC=local [3] => CN=EQC Staff,OU=Security Groups,OU=Service,DC=EQC,DC=local) [2] => memberof [samaccountname] => Array ([count] => 1 [0] => gortiz) [3] => samaccountname [mail] => Array ([count] => 1 [0] => [email protected]) [4] => mail [count] => 5 [dn] => CN=George,OU=Users,OU=Accounts,DC=EQC,DC=local)) 
+0

[आपने क्या प्रयास किया है?] (Http://whathaveyoutried.com) – Phil

+0

टिप्पणी के लिए धन्यवाद। मैंने मूल प्रश्न को एक समाधान के साथ संपादित किया, जिसने कोशिश की, जो प्रदर्शन नाम विशेषता के लिए एक खोज चलाती है जहां sAMAccountName वर्तमान उपयोगकर्ता के बराबर है। यह एक "ऐसी कोई वस्तु" त्रुटि लौटा दी। –

उत्तर

5

यहाँ एक स्क्रिप्ट हम ई जानकारी डंपिंग के लिए है है, शायद यह तुम्हारी मदद करेगा:

<?php 
$ldap_columns = NULL; 
$ldap_connection = NULL; 
$ldap_password = 'top_secret_password'; 
$ldap_username = '[email protected]'.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); 
        }else{ 
         $output = $entries[$i][$col_name][0]; 
        } 
        echo $output .'</td>'; 
       } 
      } 
      echo '</tr>'; 
     } 
     echo '</table>'; 
    } 
} 
ldap_unbind($ldap_connection); // Clean up after ourselves. 
?> 
+2

अनिवार्य रूप से आपका काम ldap_search के चौथे पैरामीटर ($ विशेषताएँ) को छोड़ रहा है, इसलिए सभी विशेषताएं लौटा दी गई हैं। आप यह समझाने के लिए अपने उत्तर में यह जोड़ना चाह सकते हैं कि आप डंप/डीबग के लिए इच्छित डेटा कैसे प्राप्त कर रहे हैं –

संबंधित मुद्दे