-
Notifications
You must be signed in to change notification settings - Fork 1
/
userctl.js
140 lines (129 loc) · 3.47 KB
/
userctl.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
130
131
132
133
134
135
136
137
138
139
140
var messages = require('./messages');
var utils = require('./lib/utils');
var accounts9 = require('./lib/accounts9');
var userman = require('./userman.js');
var async = require('async');
exports.displayUser = function displayUser(req, res) {
var username = req.params.name;
var user;
async.waterfall([
function(callback) {
//Fetch user infomation
userman.getUser({username: username}, callback);
},
function(theUser, callback) {
if (theUser == null) {
callback('no-such-user');
return;
}
user = theUser;
//Fetch domains belong to the user
userman.getDomains({username: username}, callback);
},
], function(err, domains) {
if (err) {
req.flash('error', err);
res.redirect('/');
return;
}
res.render('user', {
locals: {
title: messages.get('user'),
returnto: req.query.returnto,
userinfo: user.net9,
domains: domains,
},
});
});
};
exports.logout = function logout(req, res) {
var redirectURL = req.query.returnto || '/';
req.session.username = null;
res.redirect(redirectURL);
};
exports.login = function login(req, res) {
var callbackUri = utils.getSiteUrl(req) + '/auth_callback';
var redirectUri = accounts9.getAuthorizeUrl(callbackUri);
res.redirect(redirectUri);
};
exports.authCallback = function authCallback(req, res) {
function authError(error) {
req.flash('error', error);
res.redirect('/');
};
error = req.param('error');
code = req.param('code');
if (error !== undefined || code === undefined) {
return authError(error);
}
var callbackUri = utils.getSiteUrl(req) + '/auth_callback';
accounts9.getOAuthAccessToken(code, callbackUri, function(err, access_token, refresh_token) {
if (err) {
return authError(err);
}
accounts9.getUser(function(err, user) {
if (err) {
return authError(err);
}
var username = user.name;
userman.updateNet9(user, function(err) {
if (err) {
return on_error(err);
}
req.session.username = username;
res.redirect('/' + username);
});
});
});
};
exports.checkLogin = function regDomain(req, res, next) {
if (req.session.username) {
next();
} else {
req.flash('error', 'not-login');
res.redirect('/');
}
};
exports.regDomain = function regDomain(req, res) {
var domainName = req.params.name;
if (domainName) {
userman.getDomains({name: domainName}, function(err, domains) {
if (err || domains.length !== 1) {
req.flash('error', 'no-such-domain');
res.redirect('/' + req.session.username);
} else {
var domain = domains[0];
res.render('regdomain', {
locals: {
title: messages.get('edit-domain-information'),
username: req.session.username,
domain: domain,
returnto: req.query.returnto,
},
});
}
});
} else {
res.render('regdomain', {
locals: {
title: messages.get('register-new-domain'),
username: req.session.username,
domain: null,
returnto: req.query.returnto,
},
});
}
};
exports.regDomainDo = function regDomain(req, res) {
userman.regDomain({
username: req.session.username,
name: req.body.name,
ip: req.body.ip,
desc: req.body.desc,
}, function(err) {
if (err) {
req.flash('error', err);
}
res.redirect('/' + req.session.username);
});
};