This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (58 loc) · 1.74 KB
/
index.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
const app = require('express')();
const config = require('ez-config').get();
const ldap = require('ldapjs').createClient(config.ldap.server);
const cors = require('cors')();
var payload = {
rows: null,
corpus: null,
init: function(corpus, item) {
this.rows = [];
if (!item) {
this.rows.push({key:[corpus], value:{name:corpus}});
}
this.corpus = corpus;
},
push: function(entry) {
var key = [this.corpus, entry[config.ldap.id]];
for (var attribute in entry) {
if (!(attribute in config.reserved.attributes || entry[attribute] in config.reserved.values)) {
var value = {};
value[config.hypertopic[attribute]||attribute] = entry[attribute];
this.rows.push({
key: key,
value: value
});
}
}
},
send: function(response) {
response.json({rows: this.rows});
}
};
function sendItems(request, response) {
var filter = '(' + config.ldap.class + '=' + request.params.corpus + ')';
if (request.params.item) {
filter = '(&' + filter + '('+ config.ldap.id + '=' + request.params.item + '))';
}
var options = {
scope: 'sub',
filter: filter,
attributes: config.ldap.attributes
};
payload.init(request.params.corpus, request.params.item);
ldap.search(config.ldap.base, options, function(err, ldap_response) {
ldap_response.on('searchEntry', function(entry) {
payload.push(entry.object);
});
ldap_response.on('error', function(err) {
console.error('error: ' + err.message);
});
ldap_response.on('end', function() {
payload.send(response);
});
});
}
app.use(cors)
.get(['/corpus/:corpus', '/item/:corpus/:item'], sendItems);
app.listen(config.port);
console.log('Server running on port ' + config.port);