-
Notifications
You must be signed in to change notification settings - Fork 1
/
userman.js
129 lines (124 loc) · 3.37 KB
/
userman.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var mongodb = require('./lib/db.js');
var async = require('async');
exports.getUser = function(query, callback) {
async.waterfall([
function(callback) {
mongodb.open(callback);
},
function(db, callback) {
db.collection('users', callback);
},
function(collection, callback) {
collection.findOne(query, callback);
},
], function(err, doc) {
mongodb.close();
callback(err, doc);
});
};
exports.updateNet9 = function(net9_user_info, callback) {
var uid = parseInt(net9_user_info['uid']);
var username = net9_user_info['name'];
delete net9_user_info['uid'];
delete net9_user_info['name'];
async.waterfall([
function(callback) {
mongodb.open(callback);
},
function(db, callback) {
db.collection('users', callback);
},
function(collection, callback) {
async.waterfall([
function(callback) {
async.parallel([
function(callback) {
collection.ensureIndex('uid', {unique: true}, callback);
},
function(callback) {
collection.ensureIndex('username', {unique: true}, callback);
},
], function(err, result) {
if (err) {
callback(err);
} else {
collection.findOne({uid: uid}, callback);
}
});
},
function(doc, callback) {
if (doc) {
doc.net9 = net9_user_info;
} else {
doc = {
uid: uid,
username: username,
net9: net9_user_info,
};
}
collection.update({uid: uid}, doc, {safe: true, upsert: true}, callback);
},
], function(err, result) {
callback(err);
});
},
], function(err, result) {
mongodb.close();
callback(err);
});
};
exports.regDomain = function regDomain(domain, callback) {
async.waterfall([
function(callback) {
mongodb.open(callback);
},
function(db, callback) {
db.collection('domains', callback);
},
function(collection, callback) {
async.waterfall([
function(callback) {
collection.ensureIndex('name', {unique: true}, callback);
},
function(indexName, callback) {
collection.ensureIndex('username', callback);
},
function(indexName, callback) {
collection.findOne({name: domain.name}, callback);
},
function(doc, callback) {
if (doc && doc.username != domain.username) {
//The existing domain record not belong to current user
callback('domain-exist');
} else {
doc = domain;
}
collection.update({name: domain.name}, doc, {safe: true, upsert: true}, callback);
},
], function(err, doc) {
callback(err, doc);
});
},
], function(err) {
//Do modification on DNS configuration
mongodb.close();
callback(err);
});
};
exports.getDomains = function getDomains(cond, callback) {
async.waterfall([
function(callback) {
mongodb.open(callback);
},
function(db, callback) {
db.collection('domains', callback);
},
function(collection, callback) {
var cursor = collection.find(cond);
cursor.toArray(callback);
},
], function(err, docs) {
mongodb.close();
callback(err, docs);
});
};