php - ldap搜索需要大约30-35秒才能搜索

时间:2017-04-25 12:57:35

标签: php ldap

我正在使用ldap搜索来获取用户信息的用户信息。共有500条记录,并且从LDAP获取用户信息的功能被称为500次。

inside while loop
$userInfo = getUserInfo($booking_info['user_id']);

以下是getUserInfo函数。

function getUserInfo($passedUserId){
    global $ldapConnection;
    global $bind;
    global $baseDn;
    if($bind){
        $ldapFilter = "(sAMAccountName=" . $passedUserId . ")";
        $ldapResult = ldap_search($ldapConnection, $baseDn, $ldapFilter);
        $ldapInfo   = ldap_get_entries($ldapConnection, $ldapResult);
        if(isset($ldapInfo[0])){
            if($ldapInfo[0]["givenname"][0]){
                $firstName      = $ldapInfo[0]["givenname"][0];
            }
            if($ldapInfo[0]["sn"][0]){
                $lastName       = $ldapInfo[0]["sn"][0];
            }
            if($ldapInfo[0]["mail"][0]){
                $emailAddress   = $ldapInfo[0]["mail"][0];
            }
            if ($ldapInfo[0]["extensionattribute1"][0]) {
                $costCentre = $ldapInfo[0]["extensionattribute1"][0];
            }
            if ($ldapInfo[0]["department"][0]) {
                $organizationalUnit = $ldapInfo[0]["department"][0];
            }
            if ($ldapInfo[0]["manager"][0]) {
                $lineManager = split("=", split(",", $ldapInfo[0]["manager"][0])[0])[1];
            }
            return Array($firstName, $lastName, $emailAddress, $costCentre, $organizationalUnit, $lineManager);
        }
    }
    return Array('None', 'None', 'None', 'None', 'None', 'None');
}

完成请求大约需要30-35秒。

如何优化此功能以加快搜索速度?

1 个答案:

答案 0 :(得分:0)

我已将getUserInfo函数更改为接受user_id数组,而ldap_search过滤器具有在单个请求中传递多个ID的功能。

传递500个user_id的数组,因此只调用一次getMultipleUserInfo。

filterString将是“(|(sAMAccountName = user_id0)(sAMAccountName = user_id1)(sAMAccountName = user_id2).......(sAMAccountName = user_id499))”

function getMultipleUserInfo($passedUsersArray){
    global $ldapConnection;
    global $bind;
    global $baseDn;
    $userInfoArray = Array();
    $filterString = '';
    $count = 0;
    if(is_array($passedUsersArray)){
        foreach ($passedUsersArrayas $key => $value) {
            $count++;
            $filterString .= "(sAMAccountName=" . $key . ")";                
        }
    }
    if($bind){
        $ldapFilter = "(|" . $filterString . ")";
        $justthese  = array("ou", "sn", "givenname", "mail");
        $ldapResult = ldap_search($ldapConnection, $baseDn, $ldapFilter, Array(), 0, 0);
        $ldapInfo   = ldap_get_entries($ldapConnection, $ldapResult);
        for($info=0; $info<count($ldapInfo); $info++){
            if(isset($ldapInfo[$info])){
                if($ldapInfo[$info]["givenname"][0]){
                    $firstName      = $ldapInfo[$info]["givenname"][0];
                }
                if($ldapInfo[$info]["sn"][0]){
                    $lastName       = $ldapInfo[$info]["sn"][0];
                }
                if($ldapInfo[$info]["mail"][0]){
                    $emailAddress   = $ldapInfo[$info]["mail"][0];
                }
                if ($ldapInfo[$info]["extensionattribute1"][0]) {
                    $costCentre = $ldapInfo[$info]["extensionattribute1"][0];
                }
                if ($ldapInfo[$info]["department"][0]) {
                    $organizationalUnit = $ldapInfo[$info]["department"][0];
                }
                if ($ldapInfo[$info]["manager"][0]) {
                    $lineManager = split("=", split(",", $ldapInfo[$info]["manager"][0])[0])[1];
                }
                if ($ldapInfo[$info]["extensionattribute7"][0]) {
                    $usr= strtolower($ldapInfo[$info]["extensionattribute7"][0]);
                }
                // return Array($firstName, $lastName, $emailAddress, $costCentre, $organizationalUnit, $lineManager);
                if($usr=== ''){
                    $usr= 'invalid';
                }
                $userInfoArray [$usr] = Array($firstName, $lastName, $emailAddress, $costCentre, $organizationalUnit, $lineManager);
            }
        }
        return $userInfoArray ;
    }
    return Array('None', 'None', 'None', 'None', 'None', 'None');
}

现在需要3-4秒才能完成搜索。