2011-06-03 11 views
25

के लिए ओयू नहीं पता है, तो मेरे पास एक सक्रिय-निर्देशिका संरचना है जहां उपयोगकर्ता ऑब्जेक्ट्स ओयू में रहते हैं उदाहरण के लिए, आईटी, तकनीकी, एचआर, लेखा आदि .. मैं एक PHP स्क्रिप्ट लिखना चाहता हूं जो उपयोगकर्ता को एडी के साथ प्रमाणित करता है और अपरिपक्व वेब सेवाओं को प्रदान करने के लिए उनके समूह के आधार पर।उपयोगकर्ता ओयू प्राप्त करने के लिए PHP ldap_search() को कैसे प्राप्त करें यदि मुझे बेस डीएन

ldap_search() आधार डीएन की आवश्यकता है। मैं

ldap_search($ldap, "dc=country,dc=company,dc=co,dc=uk", "(samaccountname=$username)", array("memberof")); 

साथ खोज करने के लिए कोशिश की, लेकिन पीएचपी "ऑपरेशन त्रुटि" देता है। यदि इसके बजाय मैं ओयू

ldap_search($ldap, "ou=sales,dc=country,dc=company,dc=co,dc=uk", "(samaccountname=jake)", array("memberof")); 

निर्दिष्ट करता है तो खोज ठीक है।

क्या कोई वाइल्डकार्ड मैं उपयोग कर सकता हूं?

एक तरफ नोट पर, क्या उपयोगकर्ता ऑब्जेक्ट्स ओयू में होना चाहिए? क्योंकि मैं नोब हूं जो उन्हें पहले स्थान पर ले गया!

संपादित करें: मुझे सही दिशा में मार्गदर्शन और http://blog.redbranch.net/?p=76

समाधान के लिए JPBlanc के लिए क्रेडिट के साथ जुड़ने और बाँध के बीच 2 लाइनों को जोड़ने के लिए है।

ldap_connect(..) 
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0); 
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 
ldap_bind(..) 

धन्यवाद =)

संपादित करें 2 - Fullcode:

<?php 

namespace ldap; 

abstract class AuthStatus 
{ 
    const FAIL = "Authentication failed"; 
    const OK = "Authentication OK"; 
    const SERVER_FAIL = "Unable to connect to LDAP server"; 
    const ANONYMOUS = "Anonymous log on"; 
} 

// The LDAP server 
class LDAP 
{ 
    private $server = "127.0.0.1"; 
    private $domain = "localhost"; 
    private $admin = "admin"; 
    private $password = ""; 

    public function __construct($server, $domain, $admin = "", $password = "") 
    { 
     $this->server = $server; 
     $this->domain = $domain; 
     $this->admin = $admin; 
     $this->password = $password; 
    } 

    // Authenticate the against server the domain\username and password combination. 
    public function authenticate($user) 
    { 
     $user->auth_status = AuthStatus::FAIL; 

     $ldap = ldap_connect($this->server) or $user->auth_status = AuthStatus::SERVER_FAIL; 
     ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); 
     ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 
     $ldapbind = ldap_bind($ldap, $user->username."@".$this->domain, $user->password); 

     if($ldapbind) 
     { 
      if(empty($user->password)) 
      { 
       $user->auth_status = AuthStatus::ANONYMOUS; 
      } 
      else 
      { 
       $result = $user->auth_status = AuthStatus::OK; 

       $this->_get_user_info($ldap, $user); 
      } 
     } 
     else 
     { 
      $result = $user->auth_status = AuthStatus::FAIL; 
     } 

     ldap_close($ldap); 
    } 

    // Get an array of users or return false on error 
    public function get_users() 
    {  
     if(!($ldap = ldap_connect($this->server))) return false; 

     ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); 
     ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 
     $ldapbind = ldap_bind($ldap, $this->admin."@".$this->domain, $this->password); 

     $dc = explode(".", $this->domain); 
     $base_dn = ""; 
     foreach($dc as $_dc) $base_dn .= "dc=".$_dc.","; 
     $base_dn = substr($base_dn, 0, -1); 
     $sr=ldap_search($ldap, $base_dn, "(&(objectClass=user)(objectCategory=person)(|(mail=*)(telephonenumber=*))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))", array("cn", "dn", "memberof", "mail", "telephonenumber", "othertelephone", "mobile", "ipphone", "department", "title")); 
     $info = ldap_get_entries($ldap, $sr); 

     for($i = 0; $i < $info["count"]; $i++) 
     { 
      $users[$i]["name"] = $info[$i]["cn"][0]; 
      $users[$i]["mail"] = $info[$i]["mail"][0]; 
      $users[$i]["mobile"] = $info[$i]["mobile"][0]; 
      $users[$i]["skype"] = $info[$i]["ipphone"][0]; 
      $users[$i]["telephone"] = $info[$i]["telephonenumber"][0]; 
      $users[$i]["department"] = $info[$i]["department"][0]; 
      $users[$i]["title"] = $info[$i]["title"][0]; 

      for($t = 0; $t < $info[$i]["othertelephone"]["count"]; $t++) 
       $users[$i]["othertelephone"][$t] = $info[$i]["othertelephone"][$t]; 

      // set to empty array 
      if(!is_array($users[$i]["othertelephone"])) $users[$i]["othertelephone"] = Array(); 
     } 

     return $users; 
    } 

    private function _get_user_info($ldap, $user) 
    { 
     $dc = explode(".", $this->domain); 

     $base_dn = ""; 
     foreach($dc as $_dc) $base_dn .= "dc=".$_dc.","; 

     $base_dn = substr($base_dn, 0, -1); 

     $sr=ldap_search($ldap, $base_dn, "(&(objectClass=user)(objectCategory=person)(samaccountname=".$user->username."))", array("cn", "dn", "memberof", "mail", "telephonenumber", "othertelephone", "mobile", "ipphone", "department", "title")); 
     $info = ldap_get_entries($ldap, $sr); 

     $user->groups = Array(); 
     for($i = 0; $i < $info[0]["memberof"]["count"]; $i++) 
      array_push($user->groups, $info[0]["memberof"][$i]); 

     $user->name = $info[0]["cn"][0]; 
     $user->dn = $info[0]["dn"]; 
     $user->mail = $info[0]["mail"][0]; 
     $user->telephone = $info[0]["telephonenumber"][0]; 
     $user->mobile = $info[0]["mobile"][0]; 
     $user->skype = $info[0]["ipphone"][0]; 
     $user->department = $info[0]["department"][0]; 
     $user->title = $info[0]["title"][0]; 

     for($t = 0; $t < $info[$i]["othertelephone"]["count"]; $t++) 
       $user->other_telephone[$t] = $info[$i]["othertelephone"][$t]; 

     if(!is_array($user->other_telephone[$t])) $user->other_telephone[$t] = Array(); 
    } 
} 

class User 
{ 
    var $auth_status = AuthStatus::FAIL; 
    var $username = "Anonymous"; 
    var $password = ""; 

    var $groups = Array(); 
    var $dn = ""; 
    var $name = ""; 
    var $mail = ""; 
    var $telephone = ""; 
    var $other_telephone = Array(); 
    var $mobile = ""; 
    var $skype = ""; 
    var $department = ""; 
    var $title = ""; 

    public function __construct($username, $password) 
    {  
     $this->auth_status = AuthStatus::FAIL; 
     $this->username = $username; 
     $this->password = $password; 
    } 

    public function get_auth_status() 
    { 
     return $this->auth_status; 
    } 
} 
?> 

उपयोग:

$ldap = new ldap\LDAP("192.168.1.123", "company.com", "admin", "mypassword"); 
$users = $ldap->get_users(); 
+2

@ LarryG.Wapnitsky मैं अपने कोड है कि PHP 5.3 पर मेरे लिए काम करता पोस्ट किया है। एक्स। आशा है कि यह आपके लिए है। – Jake

+0

ने पोस्ट करने के लगभग 2 घंटे बाद काम किया। धन्यवाद! –

उत्तर

22

आप 2003 Server सक्रिय निर्देशिका या इसके बाद के संस्करण विंडोज पर खोज करने का प्रयास करें , ऐसा लगता है कि आपको LDAP_OPT_REFERRALS विकल्प 0:

पर सेट करना होगा
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); 
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 

इसके बिना, यदि आप पूरे एडी को खोजने का प्रयास करते हैं तो आपको "ऑपरेशंस त्रुटि" मिल जाएगी (डोमेन की रूट को $ base_dn के रूप में उपयोग करके)।


LDAP निर्देशिका में सामान्य किसी भी नोड में किसी भी नोड के अंतर्गत हो सकता है (यदि कोई उपयोगकर्ता एक नोड है, एक कहां एक नोड है)।

लेकिन सक्रिय-निर्देशिका एक अलग तरीके से व्यवहार करती है, SCHEMA परिभाषित करता है कि किस कंटेनर में कोई ऑब्जेक्ट मौजूद हो सकता है। इसलिए, यदि आप एक उपयोगकर्ता के लिए लग रही है, की अनुमति वरिष्ठ अधिकारियों हैं: builtinDomain, domainDNS और organizationalUnit के रूप में आप यहां के तहत देख सकते हैं:

enter image description here

+1

धन्यवाद, लेकिन मुझे एक अलग चेतावनी मिलती है: ldap_search(): खोज: आंशिक परिणाम और रेफ़रल – Jake

+0

प्राप्त हुआ मैं जवाब संपादित कर सकता हूं कृपया आप कोशिश कर सकते हैं। – JPBlanc

+0

मैं समस्या को हल करने में सक्षम था इसलिए मैंने आपकी पोस्ट को पहले से ही उत्तर के रूप में चिह्नित किया। इसके अतिरिक्त मैंने भी अपना प्रश्न संपादित किया। आपकी सहायताके लिए धन्यवाद! – Jake

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