x

主要章節

  1. Get started
  2. Workspace
  3. Editor
  4. Question types
  5. Share
  6. Results
  7. Configuration
  8. Installation and updates
  9. Profile and personal settings
  10. Account and billing
  11. FAQs
  12. Development
 Actions

LDAP 設定

From LimeSurvey Manual
This page applies to the LimeSurvey Community Edition only. Show the manual for the Community Edition


  注意 : 此功能允許 LimeSurvey 問卷管理員透過 LDAP 匯入參與者。如果您需要為管理員啟用 LDAP 驗證,請參閱 AuthLDAP 外掛程式。


一般=

您必須在 config.php 中啟用 LDAP 支援,並在 config/ldap.php 中設定 LDAP 參數,才能使用此功能。

  若要使用 LDAP,您必須確認 PHP 已安裝 LDAP 模組。有關如何安裝此擴充功能,請參閱 PHP LDAP 模組文件。


在 config.php 中啟用 LDAP

  • $enableLdap: 如果您想在 LimeSurvey 中使用 LDAP 功能,必須將此參數設為 true(預設為 false):
'config'=>array(
		'debug'=>0,
		'debugsql'=>0,
		'enableLdap'=>true,
	)

定義 LDAP 伺服器

首先,請在 "application/config/ldap.php" 中定義 LDAP 伺服器連線選項。每個伺服器都有以下選項可用:

  • $serverId: 用於識別此 LDAP 伺服器的整數。在查詢定義中用於將伺服器綁定到特定查詢;
  • $ldap_server[$serverId]['server']: LDAP 伺服器的 IP 位址或 DNS 名稱。如果您使用 SSL 安全連線(LDAPs 或 LDAP+Start-TLS),此名稱必須與伺服器憑證的 CN(或 SubjectAlternativeName)相符;
  • $ldap_server[$serverId]['port']: LDAP 伺服器監聽的 TCP 連接埠。LDAP 和 LDAP+Start-TLS 通常為 389,LDAPs 則為 636;
  • $ldap_server[$serverId]['protoversion']: 可設為 'ldapv2' 或 'ldapv3',取決於您的伺服器所支援的通訊協定。建議使用 'ldapv3'。不過,如果您要使用加密連線,請注意 LDAPs 在 'ldapv2' 模式下受支援,而 Start-TLS 則是 'ldapv3' 的加密方式;
  • $ldap_server[$serverId]['encrypt']: 定義所使用的加密方式。'ldapv2' 伺服器支援 'ldaps','ldapv3' 伺服器支援 'start-tls'。'none' 關鍵字用於明文 LDAP 通訊;
    • 請別忘了,使用 'ldaps' 或 'start-tls' 加密時,網頁伺服器必須能夠驗證 LDAP 伺服器的憑證。因此,您需要在 openldap 函式庫中定義您的憑證授權單位(在 Linux 下通常是在 /etc/openldap/ldap.conf 檔案中設定)。
  • $ldap_server[$serverId]['referrals']: 這是一個布林參數,用於定義是否要跟隨參照(referrals)(ActiveDirectory 請使用 false);
  • $ldap_server[$serverId]['encoding']: 這是一個選用參數,用於指定 LDAP 目錄儲存字串所使用的編碼。通常您不需要設定此參數,因為預設的編碼 'utf-8' 即為 LDAP 目錄的標準編碼。不過,如果您使用 Active Directory 並且在匯入帶有重音符號的字串時遇到問題,請嘗試將此參數設定為您所在地區使用的編碼(例如西歐使用 'cp850')。您可以參考 從 CSV 檔案匯入權杖 介面中的「檔案的字元編碼」下拉式選單,以取得支援的完整編碼清單。

接下來,您需要定義存取目錄所需的驗證方式。如果允許「匿名」存取,請不要設定以下兩個參數,否則請相應地進行設定:

  • $ldap_server[$serverId]['binddn']: 允許讀取目錄的「LDAP」使用者之 DN;
  • $ldap_server[$serverId]['bindpw']: 上述 LDAP 使用者的密碼。

如果您需要定義其他 LDAP 伺服器,請新增以下這一行以遞增 serverID,然後定義新的參數:

  • $serverId++。

在 config/ldap.php 中定義查詢

注意:當這些參數中需要填入 ldap 屬性名稱 時,請僅使用小寫名稱:例如使用 displayname,而不是 displayName。

請參閱 config/ldap.php 檔案,其中包含設定範例。

簡單查詢

讓我們從簡單查詢開始。這些查詢僅依據 LDAP 項目本身的屬性和位置進行篩選,通常足以用於查詢 ActiveDirectory。

  • $query_id: 是 LDAP 查詢的 ID;
  • $ldap_queries[$query_id]['ldapServerId']: 將查詢綁定到特定伺服器;
  • $ldap_queries[$query_id]['name']: 描述查詢的字串,將顯示在圖形介面中;
  • $ldap_queries[$query_id]['userbase']: 用於使用者搜尋的根 DN;
  • $ldap_queries[$query_id]['userfilter']: 用於選取潛在使用者項目的篩選條件,必須以括號括起來;
  • $ldap_queries[$query_id]['userscope']: 使用者 LDAP 搜尋的範圍('base'、'one' 或 'sub');
  • $ldap_queries[$query_id]['firstname_attr']: 將對應到權杖項目名字欄位的 Ldap 屬性;
  • $ldap_queries[$query_id]['lastname_attr']: 將對應到權杖項目姓氏欄位的 Ldap 屬性;
  • $ldap_queries[$query_id]['email_attr']: 將對應到權杖項目電子郵件地址欄位的 Ldap 屬性。

您也可以選擇從目錄中擷取更多資訊:

  • $ldap_queries[$query_id]['token_attr']: 將對應到權杖代碼的 Ldap 屬性;
  • $ldap_queries[$query_id]['language']: 將對應到使用者語言編碼的 Ldap 屬性;
  • $ldap_queries[$query_id]['attr1']: 將對應到 attribute_1 欄位的 Ldap 屬性;
  • $ldap_queries[$query_id]['attr2']: 將對應到 attribute_2 欄位的 Ldap 屬性。

以 DN 成員進行的組合群組查詢

現在讓我們來看如何定義較複雜的查詢。

以下查詢會先進行一次 LDAP 搜尋,在 LDAP 群組中查找。LDAP 群組是包含使用者項目參照的 LDAP 項目,其形式為:

  • 使用者 ID(例如 posixGroups) ==> 請參閱下一節
  • 或使用者 DN(例如 groupofnames 和 groupofuniquenames)==> 請見下文

這裡我們處理的是包含使用者 DN 的群組:

  • 如上所述,定義 $query_id、$ldap_queries[$query_id]['ldapServerId']、$ldap_queries[$query_id]['name']。

然後定義群組篩選參數:

  • $ldap_queries[$query_id]['groupbase']: 開始搜尋群組項目的根 DN;
  • $ldap_queries[$query_id]['groupfilter']: 用於選取潛在群組項目的 LDAP 篩選條件;
  • $ldap_queries[$query_id]['groupscope']: 群組 LDAP 搜尋的範圍('one'、'base' 或 'sub');
  • $ldap_queries[$query_id]['groupmemberattr']: 群組項目中包含使用者項目參照的 LDAP 屬性名稱;
  • $ldap_queries[$query_id]['groupmemberisdn']: TRUE。

至此,一切都已設定完成,第一次 LDAP 搜尋即可找到與所選群組相對應的使用者。不過,您可以對這些「候選使用者」再套用另一個篩選條件,以限制最終選取的對象。當然,這是選用的:

  • $ldap_queries[$query_id]['userbase']: 使用者 LDAP 搜尋的基底 DN(只會選取符合此基底的候選使用者);
  • $ldap_queries[$query_id]['userscope']: 使用者 LDAP 搜尋的範圍(只會選取符合 userbase+scope 的候選使用者);
  • $ldap_queries[$query_id]['userfilter']: 套用於每個候選使用者項目(依其屬性)的篩選條件,用以增加另一層選取。

以 UID 成員進行的組合群組查詢

現在讓我們來看,當群組成員是使用者 UID 而不是使用者 DN 時,如何定義組合群組查詢。

與以 DN 成員進行的群組查詢相同,這些查詢會先進行一次 LDAP 搜尋,查找 LDAP 群組項目並取得其成員。這些成員值接著會用於使用者搜尋篩選條件中,以搜尋對應的項目。因此,還必須設定另一個參數,用來定義使用者項目中應與群組內成員 UID 相符的使用者屬性。

讓我們來檢視必要的參數:

  • 如上所述,定義 $query_id、$ldap_queries[$query_id]['ldapServerId']、$ldap_queries[$query_id]['name']

然後定義群組篩選參數:

  • $ldap_queries[$query_id]['groupbase']: 開始搜尋群組項目的根 DN;
  • $ldap_queries[$query_id]['groupfilter']: 用於選取潛在群組項目的 LDAP 篩選條件;
  • $ldap_queries[$query_id]['groupscope']: 群組 LDAP 搜尋的範圍('one'、'base' 或 'sub');
  • $ldap_queries[$query_id]['groupmemberattr']: 群組項目中包含使用者項目參照的 LDAP 屬性名稱;
  • $ldap_queries[$query_id]['groupmemberisdn']: FALSE;
  • $ldap_queries[$query_id]['useridattr']: 必須與群組成員中找到的 UID 相符的使用者屬性名稱。

至此,一切都已設定完成,第一次 LDAP 搜尋即可找到與所選群組相對應的使用者 UID,並且會自動填入使用者搜尋篩選條件。

不過,您可以透過補充由成員 UID 自動產生的使用者篩選條件,來限制最終選取哪些「候選使用者」。當然,這是選用的:

  • $ldap_queries[$query_id]['userbase']: 使用者 LDAP 搜尋的基底 DN(只會選取符合此基底的候選使用者);
  • $ldap_queries[$query_id]['userscope']: 使用者 LDAP 搜尋的範圍(只會選取符合 userbase+scope 的候選使用者);
  • $ldap_queries[$query_id]['userfilter']: 套用於每個候選使用者項目(依其屬性)的篩選條件,用以增加另一層選取。

Active Directory 呢?

Active Directory (AD) 是 Microsoft 的登錄目錄,可透過 LDAP 通訊協定進行查詢。

因此可以將其內容用於 LimeSurvey 的 token 查詢,但這需要了解 AD 的組織方式。

  • LDAP 根基底為 dc=my_windows_domain_name,dc=dns_suffix2,dc=dns_suffix1

==> 例如,如果貴公司擁有 DNS 網域 'my-company.com',而您的 Windows 網域為 'employees',則根基底為 dc=employees,dc=my-company,dc=com

  • 使用者和使用者群組儲存在 cn=Users,dc=my_windows_domain_name,dc=dns_suffix2,dc=dns_suffix1 之下(請注意,這不是 ou=users);
  • Active Directory 群組:
    • 群組物件在其 'member' 屬性中包含成員的 DN;
    • 群組成員資格也儲存在每個使用者項目的 memberOf 屬性中。此屬性包含該使用者所屬群組的 DN;
    • 部分群組位於 CN=Builtin,dc=my_windows_domain_name,dc=dns_suffix2,dc=dns_suffix1:
      • 例如:cn=Administrator,CN=Builtin,dc=my_windows_domain_name,dc=dns_suffix2,dc=dns_suffix1;

在某些情況下,查詢 Active Directory 並不容易,以下是從 Active Directory 取得部分資訊的範例設定:

//Connection to the active directory Server:
$serverId=0;
$ldap_server[$serverId]['server'] = "10.10.10.10";
$ldap_server[$serverId]['port'] = "389";
$ldap_server[$serverId]['protoversion'] = "ldapv2";
$ldap_server[$serverId]['encrypt'] = "none"; // Most AD LDAP servers will not have encryption set by default
$ldap_server[$serverId]['referrals'] = false;
$ldap_server[$serverId]['binddn'] = "domain\\user";
$ldap_server[$serverId]['bindpw'] = "userpassword";
//$ldap_server[$serverId]['binddn'] = "CN=user,OU=user_group,DC=xxx,DC=yyy"; this one will not work with active directory, that´s why you need to use "domain\\user"
//Here is a sample query for getting all active users of an active directory:
$query_id=0;
$ldap_queries[$query_id]['ldapServerId'] = 0;
$ldap_queries[$query_id]['name'] = 'Staff with an enabled account';
$ldap_queries[$query_id]['userbase'] = 'OU=USER_GROUP,DC=xxx,DC=yyy';
$ldap_queries[$query_id]['userfilter'] = '(&(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))';
//AD has no "account is enabled" attribute, so the filter excludes accounts that have the "disabled" flag (bit 2) set in userAccountControl
$ldap_queries[$query_id]['userscope'] = 'sub';
$ldap_queries[$query_id]['firstname_attr'] = 'givenname';
$ldap_queries[$query_id]['lastname_attr'] = 'sn';
$ldap_queries[$query_id]['email_attr'] = 'mail';
$ldap_queries[$query_id]['token_attr'] = ''; // Leave empty for Auto Token generation by phpsv
$ldap_queries[$query_id]['language'] = '';
$ldap_queries[$query_id]['attr1'] = '';
$ldap_queries[$query_id]['attr2'] = '';
//You can also filter on group membership through the memberOf attribute of the user. Here is a sample query for getting all active users that are member of the group "samplegroup" in active directory:
$query_id++;
$ldap_queries[$query_id]['ldapServerId'] = 0;
$ldap_queries[$query_id]['name'] = 'All members of samplegroup';
$ldap_queries[$query_id]['userbase'] = 'OU=USER_GROUP,DC=xxx,DC=yyy';
$ldap_queries[$query_id]['userfilter'] = '(&(objectClass=user)(memberOf=CN=samplegroup,OU=Group Global,OU=USER_GROUP,DC=xxx,DC=yyy)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))';
$ldap_queries[$query_id]['userscope'] = 'sub';
$ldap_queries[$query_id]['firstname_attr'] = 'givenname';
$ldap_queries[$query_id]['lastname_attr'] = 'sn';
$ldap_queries[$query_id]['email_attr'] = 'mail';
$ldap_queries[$query_id]['token_attr'] = ''; // Leave empty for Auto Token generation by phpsv
$ldap_queries[$query_id]['language'] = '';
$ldap_queries[$query_id]['attr1'] = '';
$ldap_queries[$query_id]['attr2'] = '';

另一個使用者查詢範例:

$ldap_queries[$query_id]['userfilter'] = '(&(objectCategory=Person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))'; // AD doesn't recognise enabled accounts in the normal way, so instead, we check users are not disabled
  • 建議在您的使用者篩選條件中新增 (mail=*),以略過沒有電子郵件地址的使用者。

群組查詢範例:

$ldap_queries[$query_id]['groupfilter'] = '(&(objectClass=group)(cn=Domain Admins))'; // AD doesn't use the standard attribute name for groups, so use this example instead.

自動為每個 Active Directory 群組建立一個查詢

如果您將問卷參與者的群組保存在同一個組織單位 (OU) 中,就不必為每個群組各自撰寫查詢。以下程式碼會列出該 OU 中的所有群組,並為每個群組建立一個查詢。該查詢包含群組中所有已啟用的成員,包括巢狀群組的成員。

請將程式碼新增到 config/ldap.php 中您的伺服器和查詢定義之後、//DO NOT CHANGE BELOW HERE 這一行之前。然後設定最上方的四個變數:

  • $groupServerId: 要使用的伺服器的 $serverId;
  • $groupContainer: 存放群組的 OU 的 DN。僅列出直接位於此 OU 中的群組,不包括子 OU 中的群組;
  • $groupUserBase: 搜尋群組成員時所用的根 DN;
  • $groupNameAttr: 在 GUI 中用作查詢名稱的群組屬性(小寫),例如 'info'(即「Active Directory 使用者和電腦」中的「備註」欄位)或 'description'。如果群組在此屬性中沒有值,則改用其 cn。
$groupServerId  = 0;
$groupContainer = 'OU=Survey groups,DC=xxx,DC=yyy';
$groupUserBase  = 'DC=xxx,DC=yyy';
$groupNameAttr  = 'info';

$srv = $ldap_server[$groupServerId];
$ds = ldap_connect(($srv['encrypt'] == 'ldaps' ? 'ldaps://' : 'ldap://') . $srv['server'] . ':' . $srv['port']);
if ($ds) {
    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $srv['protoversion'] == 'ldapv3' ? 3 : 2);
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
    // Never send the bind password if Start-TLS was requested but failed
    $secure = $srv['encrypt'] != 'start-tls' || @ldap_start_tls($ds);
    $bound = $secure && (empty($srv['binddn']) ? @ldap_bind($ds) : @ldap_bind($ds, $srv['binddn'], $srv['bindpw']));
    $result = $bound ? @ldap_list($ds, $groupContainer, '(objectCategory=group)', array('cn', $groupNameAttr)) : false;
    $groups = $result ? ldap_get_entries($ds, $result) : array('count' => 0);
    for ($i = 0; $i < $groups['count']; $i++) {
        $query_id++;
        $ldap_queries[$query_id]['ldapServerId'] = $groupServerId;
        $ldap_queries[$query_id]['name'] = isset($groups[$i][$groupNameAttr][0]) ? $groups[$i][$groupNameAttr][0] : $groups[$i]['cn'][0];
        $ldap_queries[$query_id]['userbase'] = $groupUserBase;
        // Enabled users with an email address that are members of the group, directly or through nested groups
        $ldap_queries[$query_id]['userfilter'] = '(&(objectCategory=person)(objectClass=user)(mail=*)'
            . '(!(userAccountControl:1.2.840.113556.1.4.803:=2))'
            . '(memberOf:1.2.840.113556.1.4.1941:=' . ldap_escape($groups[$i]['dn'], '', LDAP_ESCAPE_FILTER) . '))';
        $ldap_queries[$query_id]['userscope'] = 'sub';
        $ldap_queries[$query_id]['firstname_attr'] = 'givenname';
        $ldap_queries[$query_id]['lastname_attr'] = 'sn';
        $ldap_queries[$query_id]['email_attr'] = 'mail';
        $ldap_queries[$query_id]['token_attr'] = '';
        $ldap_queries[$query_id]['language'] = '';
        $ldap_queries[$query_id]['attr1'] = '';
        $ldap_queries[$query_id]['attr2'] = '';
    }
    ldap_unbind($ds);
}

注意事項:

  • 每次開啟或提交「由下列位置匯入參與者:LDAP」頁面時,程式碼都會連線到目錄,因此新增的群組無需變更設定即會出現。如果連線或繫結失敗,頁面只會顯示您手動定義的查詢。
  • 比對規則 1.2.840.113556.1.4.1941 (LDAP_MATCHING_RULE_IN_CHAIN) 可解析巢狀群組。它僅存在於 Active Directory 中,因此此範例不適用於其他 LDAP 伺服器。在那些伺服器上,請改為對每個群組使用組合群組查詢。
  • 若也要列出 $groupContainer 子 OU 中的群組,請將 ldap_list 替換為 ldap_search。
  • 若要列出多個 OU 中的群組,請使用四個變數的其他值重複此程式碼。

如需有關 Active Directory LDAP 結構的更多資訊,請參閱 Active Directory 架構 和 Active Directory 技術規格。

本頁面可能為機器翻譯,可能包含錯誤。 檢視英文原文 · 改善此翻譯