带有Node JS的LDAP JS查找用户传递字符串模式

时间:2015-04-14 01:17:02

标签: javascript angularjs node.js ldap ldap-query

我目前在Angular JS应用程序中使用LDAP JS进行身份验证,一切正常。

我现在正在构建一个新视图,我的要求是:

  • 我有一个文本框,其中admin将写入可能是LDAP中存在的用户ID的几个字母。
  • 我想在预先输入/建议中显示LDAP中存在的应用匹配ID。我知道typeahead是如何工作的,所以这不是问题。问题是如何在uid方法中为search()传递rejex或模式匹配项。

我的示例代码在这里:

function GetAllLDAPUser(dnFilter, res) {

    client.search('uid=**WHAT-PATTERN-CAN-I-PASS-HERE**' + dnFilter, opts, function(err, result) {

            result.on('searchEntry', function(entry) {

                // I usually read entry.object or entry.raw here , that works

            });
            result.on('end', function(result) {

                .......
            });
        }
    }
}

所以问题是我应该通过什么代替 WHAT-PATTERN-CAN-I-PASS-HERE

结果: 假设我输入an。预先输入将显示以an开头的所有用户ID,例如anaanamonanalisa等。

1 个答案:

答案 0 :(得分:-1)

我已写完最终解决方案并关闭the issue on the project's repository

  

对于模式匹配,我们需要使用'过滤器'我们传递给搜索方法的选项对象中的字段。所以我最终做了类似下面的事情:

var dnFilter = 'ou=People,o=Intra,dc=YOURCOMPANY,dc=com'; //depends on your LDAP settings.
var query;
var matchedUsers = [];
query.LDAPName = "dummy"; //some name which resides in LDAP 
//You can even have one simple variable rather than having this query object.
opts = {
    scope: 'sub',
    filter: (shcDisplayName = '+ query.LDAPName + ')
    '
};

//Do not use 'shcDisplayName' , this will be any variable stored in your LDAP object. You need get 
//the structure of LDAP end point you are working on. For me, I had one variable 'shcDisplayName'
//on which I wanted to play so I am using this variable in my filter.
client.search(dnFilter, opts, function(err, result) {

        result.on('searchEntry', function(entry) {
                matchedUsers.push({
                    'Name': entry.object.shcDisplayName,
                    'Id': entry.object.uid
                });
            }

            result.on('end', function(result) {

                    if (matchedUsers.length) { //if any match was found.

                        //send the json result back
                        res.json(matchedUsers);

                        //if you want to send json back, do not use res.send() otherwise you will end up getting
                        //circular reference error. 
                    }
                }

                result.on('error', function(ex) {
                    //Handle errors here if any
                });

            });
    }
}