This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
userman.js
87 lines (77 loc) · 3.25 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
/* vim: set sw=2 ts=2 nocin si: */
var userbase = require('./userbase-ldap');
exports.checkUser = function (username, callback) {
userbase.checkUser(username, function (occupied) {
callback({ username: username, occupied: occupied });
});
};
exports.register = function (userinfo, callback) {
// Let's do some verification first! TODO: Better verification.
if (!userinfo.password || !userinfo.email) {
callback({ success: false, userinfo: userinfo, error: 'fields-required' });
}
// Now make sure that the user doesn't exist.
userbase.checkUser(userinfo.username, function (occupied) {
if (occupied) {
callback({ success: false, userinfo: userinfo, error: 'Already exists' });
return;
}
// When everything looks good, insert the record.
userbase.create(userinfo, function (success, userOrErr) {
if (success) callback({ success: true, userinfo: userOrErr });
else callback({ success: false, userinfo: userinfo, error: userOrErr });
});
});
};
exports.getByName = function (username, callback) {
userbase.getByName(username, function (success, userOrErr) {
if (success) callback({ success: true, userinfo: userOrErr });
else callback({ success: false, username: username, error: userOrErr });
});
};
exports.authenticate = function (userinfo, callback) {
userbase.authenticate(userinfo.username, userinfo.password, function (success, userOrErr) {
if (success) callback({ success: true, userinfo: userOrErr });
else callback({ success: false, userinfo: userinfo, error: userOrErr });
});
};
var updateInfo = function (newinfo, callback) {
userbase.update(newinfo, function (success, userOrErr) {
if (success) callback({ success: true, userinfo: userOrErr });
else callback({ success: false, error: userOrErr });
});
};
exports.editInfo = function (newinfo, callback) {
// Authenticate only if the user wants to change the password (ie. newinfo.newpass != '')
if (newinfo.newpass) {
userbase.authenticate(newinfo.username, newinfo.oldpass, function (success, userOrErr) {
if (success) {
newinfo.password = newinfo.newpass;
updateInfo(newinfo, callback);
} else {
callback({ success: false, error: 'wrong-old-pass' });
}
});
} else {
updateInfo(newinfo, callback);
}
};
exports.rename = function (nameChange, callback) {
// First authenticate. We don't want strange things to happen...
userbase.authenticate(nameChange.oldname, nameChange.password, function (success, userOrErr) {
if (!success) callback({ success: false, error: userOrErr });
// Now double-check: Are you *really* allowed to rename yourself?
else if (userOrErr.nextNameChangeDate > Date.now()) {
callback({ success: false, error: 'change-name-later|' + new Date(userOrErr.nextNameChangeDate) });
} else {
// Is the target name occupied?
userbase.checkUser(nameChange.newname, function (occupied) {
if (occupied) callback({ success: false, error: 'Already exists' });
else userbase.rename(nameChange.oldname, nameChange.newname, function (success, userOrErr) {
if (success) callback({ success: true, userinfo: userOrErr });
else callback({ success: false, error: userOrErr });
});
});
}
});
};