設定 PHP 以使用 LDAP

正確配置 LDAP 伺服器後,現在我們要連線。例如,通過使用 PHP。

  • DN =專有名稱。這意味著,你正在使用資料庫的哪個部分。可以是使用者或組(甚至是配置設定)。
  • 條目:實體,例如使用者。
  • 屬性:條目中的內容,例如姓名,電話號碼和電子郵件地址。

首先,我們定義以下內容:

$server = "server.example.com";  //this is the LDAP server you're connecting with
$ds = ldap_connect("ldaps://$server", 636); //always connect securely via LDAPS when possible

現在,我們相互聯絡。我們想要的下一件事是讓伺服器知道我們是一個值得信賴的人。最簡單的解決方案是使用根 DN 或具有適當許可權的現有使用者來檢視資料庫(可能預設情況下,資料庫中的每個使用者都可以執行此操作)。我們使用 bind 函式進行身份驗證。

設定選項

首先,我們宣告這些選項。根據你的伺服器配置,你可以將其保留。

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);

繫結

假設你使用的是 admin,密碼是 pass123notsafe

$dn = "uid=admin,cn=users,dc=server,dc=example,dc=com";
$pass = "pass123notsafe";
$ldapbind = ldap_bind($ds, $dn, $pass); //this is the point we are authenticating

那太好了。我們在。現在我們可以執行許多不同的操作。例如,我們可以搜尋,讀取,編寫和修改使用者甚至組。

搜尋

想象一下,我們想要顯示組使用者的所有成員。

$dn = "cn=users,dc=server,dc=example,dc=com"; //very important: in which part of your database are you looking
$filter = "uid=*"; //don't filter anyone out (every user has a uid)
$sr = ldap_search($ds, $dn, $filter) or die ("bummer"); //define your search scope

$results = ldap_get_entries($ds, $sr); //here we are pulling the actual entries from the search we just defined
var_dump($results); //will give you all results is array form. 

你可以使用 foreach 迴圈以一種很好的方式顯示資料。

好的,有這個嗎?現在繼續進行一些過濾。我們只想顯示已註冊電子郵件地址的自行車所有者組中的使用者。知道所有使用者都在 cn = users 中非常重要。除此之外,他們也可以是其他組的成員。

//did the connecting and binding

$dn = "cn=bikeowners,cn=groups,dc=server,dc=example,dc=com"; //note the extra "cn=groups" for looking in a group that is not "users"
$filter = "email=*"; //email address must be set but can be anything
$sr = ldap_search($ds, $dn, $filter) or die ("bummer"); //define your search scope

現在再次進行 ldap_get_entries

獲得參賽作品的效率

解決效率的一個例子,對於每個條目具有大量屬性的大型資料庫而言越來越重要。特別是當你在屬性 jpegphoto 中儲存圖片時,當你有選擇地拉動條目時,它可能會顯著減少你的載入時間。

想象一下,我們想要尋找自行車所有者組中的使用者。這些條目中儲存了大量資訊,假設它們具有以下屬性:cn,uid,name,displayname,mail,initials,mobile,telephonenumber,street,postaladdress,postalcode 和 jpegphoto。現在我們只需要他們的 uid,名字和首字母。

為此,我們使用 ldap_search 的可選第 4 個引數:

$dn = "cn=bikeowners,cn=groups,dc=server,dc=example,dc=com";
$filter = "uid=*"; //
$justhese = array("uid", "name", "initials");
$sr = ldap_search($ds, $dn, $filter, $justthese) or die ("bummer"); //define your search scope

瞧,執行 ldap_get_entries 只會給你這些資料。

高階過濾

當然,你可以使用 LDAP 提取整個資料庫,並在 PHP 中進一步處理。但是,就像 MySQL 一樣,處理伺服器端的效率要高得多。為了證明這一點,我們可以使用高階過濾器。

以上面的示例之一作為起點,但根據下面的行更改$ filter。在我們的示例中,我們希望顯示活動使用者的資料。通常,屬性 shadowexpire 用於儲存此資訊。但是,這可能因各種 LDAP 系統而異。我們不僅要顯示的是活躍使用者,但其名稱必須以開始,他們必須住在阿姆斯特丹。

基本上我們想做三件事:

  1. 最簡單:必須住在阿姆斯特丹。在此示例中,居住地儲存在屬性 postaladdress
$filter= "postaladdress=Amsterdam";
  1. 名稱必須以 a 開頭。在此示例中,UID 由名稱組成,因此:
$filter= "uid=a*";
  1. 使用者必須處於活動狀態該值儲存在預設屬性 shadowexpire 中,值為 -1。根據你的伺服器配置,shadowexpire 可以儲存無數的值,甚至可以使用日期。如果使用者處於非活動狀態,則 shadowexpire 將為 1。為了確保我們獲得所有使用者,除了那些非常不活躍的使用者,我們不會選擇過濾 shadowexpire = -1。相反,我們說我們希望它們不活躍。
$filter= "(!(shadowexpire=1))"; //NOT is represented with "!"

現在最有趣的部分:結合所有三個例子。我們可以使用括號,AND,OR 和 NOT 表示式來完成此操作

$filter= "(&(postaladdress=Amsterdam)(uid=a*)(!(shadowexpire=1)))";

最後,我們可以使用“|”構建 OR 語句,例如,如果我們希望所有使用者以 ab 開頭

$filter= "(|(uid=a*)(uid=n*))";

你可以無休止地結合並構建相當令人印象深刻的過濾器,享受嘗