带有 Node JS 的 LDAP JS 查找传入字符串模式的用户
LDAP JS with Node JS Finding users passing in patterns of string
我目前在 Angular JS 应用程序中使用 LDAP JS 进行身份验证,一切正常。
我现在正在构建一个新视图,我的要求是:
- 我有一个文本框,管理员将在其中写入可能是 LDAP 中存在的用户 ID 的几个字母。
- 我想在 typeahead/suggestions 上显示 LDAP 中存在的应用匹配 ID。我知道 typeahead 是如何工作的,所以这不是问题。问题是如何在
search()
方法中为 uid
传递 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
。 typeahead 将显示所有以 an
开头的用户 ID,例如 ana
、anamon
、analisa
等
我已经写出最终方案并关闭了the issue on the project's repository
For pattern matching, we need to play with the 'filter' field in option object which we pass to the search method. So I ended up doing something like below:
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
});
});
}
}
我目前在 Angular JS 应用程序中使用 LDAP JS 进行身份验证,一切正常。
我现在正在构建一个新视图,我的要求是:
- 我有一个文本框,管理员将在其中写入可能是 LDAP 中存在的用户 ID 的几个字母。
- 我想在 typeahead/suggestions 上显示 LDAP 中存在的应用匹配 ID。我知道 typeahead 是如何工作的,所以这不是问题。问题是如何在
search()
方法中为uid
传递 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
。 typeahead 将显示所有以 an
开头的用户 ID,例如 ana
、anamon
、analisa
等
我已经写出最终方案并关闭了the issue on the project's repository
For pattern matching, we need to play with the 'filter' field in option object which we pass to the search method. So I ended up doing something like below:
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
});
});
}
}