我想使用ldap和nodejs搜索并返回某个组的所有用户。结果应使用带有pug扩展名的express在网页上显示。
我的函数的调用如下:
app.get('/singleProjectSelection/:id', (req, res) => {
var url = req.url.toString('utf-8');
var prjName = url.replace('/singleProjectSelection/', '');
ldap.getUserFromProject(prjName, users => {
res.render('users', { users: getUsers() });
});
});
ldap.js文件如下:
module.exports.getUserFromProject = function (searchDN, callback) {
const firstClient = getFirstClient();
firstClient .bind(USER, PW, (err) => {
let users = [];
firstClient .search(searchDN, getUserSearchQuery(), (err, res) => {
res.on('searchEntry', function (entry) {
const members = entry.attributes[1];
for (var i = 0; i < members.buffers.length; i++) {
let json = JSON.stringify(members.buffers[i]);
let bufferOriginal = Buffer.from(JSON.parse(json).data);
let output = bufferOriginal.toString('utf8');
firstClient .search(`${output}`, opts, (err, res) => {
res.on('searchEntry', (entry) => {
const secondClient = getSecondClient();
secondClient .bind(SEC_USER, SEC_PW, (err) => {
const splitRes = output.split(",");
const foreignName = splitRes[0].replace("CN=", "");
const searchString = `(&(objectClass=user)(objectSid=${foreignName}))`;
var searchFilter = {
filter: searchString,
scope: 'one',
attributes: ['sAMAccountName', 'givenName', 'sn', 'mail']
};
secondClient .search('ou=Usr,dc=company,dc=domain,dc=de', searchFilter, (err, res) => {
res.on('searchEntry', (entry) => {
try {
users.push({ userID: entry.json.attributes[2].vals[0], first: entry.json.attributes[1].vals[0], last: entry.json.attributes[0].vals[0]});
}
catch (error) {
console.log(error);
}
});
});
});
});
});
}
});
res.on('searchReference', function (referral) {
console.log('referral: ' + referral.uris.join());
});
res.on('error', function (err) {
console.error('error: ' + err.message);
});
res.on('end', function (result) {
console.log('status: ' + result.status);
return callback(users);
});
})
我希望输出是一个用户数组[{userID:'123',first:'firstName,last:'lastName'}],但是我得到的是一个空数组,该网站未显示任何用户。
我如何“等待” ldap搜索的结果并用正确的数据呈现网页?
谢谢。