-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.js
83 lines (75 loc) · 3.02 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
module.exports = function ldapClient(context) {
let ldap = require('ldapjs');
let client = ldap.createClient({
url: 'ldap://ldap.epfl.ch',
timeLimit: 1,
sizeLimit: 10
});
function cacheQuery(ldapQuery, objectFactory, modelMapper, isResultUniq, next) {
let opts = {
filter: ldapQuery,
scope: 'sub'
};
client.search(context.options.searchBase, opts, function (err, ldapRes) {
let groupedObject = {};
ldapRes.on('searchEntry', function (entry) {
if (typeof entry.json != 'undefined') {
let objectIdentifier = entry.object.uniqueIdentifier;
if (groupedObject[objectIdentifier] === undefined) {
groupedObject[objectIdentifier] = Array();
}
groupedObject[objectIdentifier].push(entry.object);
} else {
next(null, groupedObject);
}
});
ldapRes.on('searchReference', function (referral) {
//console.log('referral: ' + referral.uris.join());
});
ldapRes.on('error', function (err) {
console.error('error: ' + err.message);
next(err, null);
});
ldapRes.on('timeout', function (err) {
console.error('error: ' + err.message);
next(err, null);
});
ldapRes.on('end', function () {
let objectsGroup = Array();
for (let userEntry in groupedObject) {
if (groupedObject.hasOwnProperty(userEntry)) {
if (isResultUniq) {
objectsGroup = modelMapper(objectFactory(groupedObject[userEntry]));
} else {
objectsGroup.push(modelMapper(objectFactory(groupedObject[userEntry])));
}
}
}
next(null, objectsGroup);
});
});
}
client.executeQuery = function(ldapQuery, objectFactory, modelMapper, isResultUniq, next) {
context.memoryCache.get(ldapQuery, function (err, data) {
if (!err) {
if (data == undefined) {
cacheQuery(ldapQuery, objectFactory, modelMapper, isResultUniq, function(err, data) {
context.memoryCache.set(ldapQuery, data, function (err, success) {
if (!err && success) {
next(null, data);
} else {
next({ Error: "aararrggghhh!" }, null);
}
});
});
} else {
next(null, data);
}
} else {
next({ Error: "aararrggghhh!" }, null);
}
});
};
return client;
};