-
Notifications
You must be signed in to change notification settings - Fork 6
/
users.js
85 lines (77 loc) · 2.45 KB
/
users.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
var express = require('express');
var router = express.Router();
var userManagement = require('./userManagement');
var common = require('./common');
var config = require('./config');
var redis = require('redis'),
client = redis.createClient();
router.post('/changePassword', common.authInRequest, function(req, res){
if(req.body.newPassword != null && req.body.newPassword.length > 4){
userManagement.setPassword(req.user.idNumber, req.body.newPassword);
res.send('0').end();
}else{
res.end();
}
});
router.post('/login', common.authInRequest, function(req, res){
req.session.idNumber = req.user.idNumber;
req.session.save(function(err){
res.send('0').end();
});
});
router.post('/logout', common.loggedIn, function(req, res){
req.session.idNumber = null;
res.send('0').end();
});
router.post('/register', function(req, res){
var name = req.body.name;
var username = req.body.username;
var idNumber = req.body.newId;
var approverId = req.body.approverId;
var passphrase = req.body.passphrase || '';
var password = req.body.password || '';
if(!(common.isValidId(idNumber) && common.isValidUsername(username) &&
common.isValidName(name) && password.length > 4)){
res.end();
return;
}
if(passphrase != '' && common.passphraseIsValid(passphrase)){
userManagement.createUser(idNumber, username, name, password,
passphrase == config.labMonitorPassphrase,
passphrase == config.execPassphrase,
passphrase == config.adminPassphrase,
function(){
res.send('0').end();
return;
}, function(error){
if(error.message == "Username taken"){
res.send('3').end();
}else{
res.send('1').end();
}
});
return;
}else if(passphrase != '' && !common.passphraseIsValid(passphrase)){
res.send('4').end();
return;
}
if(common.isValidId(approverId)){
userManagement.getUser(approverId, function(approver){
if(approver.labMonitor == 'true'){
userManagement.createUser(idNumber, username, name, password, false, false, false,
function(){
res.send('0').end();
}, function(error){
if(error.message == "Username taken"){
res.send('3').end();
}else{
res.send('1').end();
}
});
}
}, function(){
res.send('2').end();
});
}
});
module.exports = router;