-
Notifications
You must be signed in to change notification settings - Fork 2
/
ldap.js
96 lines (71 loc) · 1.92 KB
/
ldap.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
84
85
86
87
88
89
90
91
92
93
94
95
var async = require('async');
var ldap = require('ldapjs');
var Firebase = require('firebase');
var firebaseDB = new Firebase('https://berkeleydir.firebaseio.com/users');
var server = 'ldap://ldap.berkeley.edu';
var searchBase = 'ou=people,dc=berkeley,dc=edu';
var client = ldap.createClient({
url: server
});
var exists = function(attribute) {
return (attribute && attribute[0]);
};
var onSearch = function(err, res) {
res.on('searchEntry', function (entry) {
//var log = JSON.stringify(entry);
var user = {
name: '',
email: [],
id: 0
};
for (var i = 0; i < entry.json.attributes.length; i++) {
var item = entry.json.attributes[i];
if (item.type === 'displayName' && exists(item.vals)) {
user.name = item.vals[0];
}
if (item.type === 'uid' && exists(item.vals)) {
user.id = parseInt(item.vals[0], 10);
}
if (item.type === 'mail' && exists(item.vals)) {
user.email = item.vals;
}
}
//console.log(user);
firebaseDB.child(user.id).set(user);
});
res.on('end', function(result) {
console.log(result.messageID);
if (result.messageID === currentBatchNumber) {
console.log('next!!!');
nextBatch(currentBatchNumber);
}
});
res.on('error', function(err) {
console.error('error: ' + err.message);
});
};
var search = function(arr) {
async.each(arr, function(i, callback){
process.nextTick(function () {
var opts = {
filter: '(&(objectclass=*)(uid=' + i + '))',
scope: 'sub'
};
client.search(searchBase, opts, function(err, res){
onSearch(err, res, i);
});
callback();
});
});
};
var currentBatchNumber = -1;
var nextBatch = function(arrstart) {
var arr = [];
var arrlength = arrstart + 1000;
currentBatchNumber = arrlength;
for (var i = arrstart; i < arrlength; i++) {
arr[i] = i;
}
search(arr);
};
nextBatch(0);