forked from 3drepo/3drepo.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes_post.js
201 lines (163 loc) · 6.78 KB
/
routes_post.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Copyright (C) 2014 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/***************************************************************************
* @file Contains the POST routes
****************************************************************************/
var schemaValidator = require('./js/core/db_schema.js')();
var log_iface = require('./js/core/logger.js');
var logger = log_iface.logger;
var responseCodes = require('./js/core/response_codes.js');
var config = require('./js/core/config.js');
function createSession(place, res, req, user)
{
req.session.regenerate(function(err) {
if(err)
responseCodes.respond(place, responseCodes.EXTERNAL_ERROR(err), res, {account: user.username});
else
{
logger.log('debug', 'Authenticated ' + user.username + ' and signed token.')
req.session.user = user;
responseCodes.respond(place, responseCodes.OK, res, {account: user.username});
}
});
}
module.exports = function(router, dbInterface, checkAccess){
this.postMap = [];
this.post = function(regex, shouldCheckAccess, callback) {
this.postMap.push({regex: regex, shouldCheckAccess: shouldCheckAccess, callback: callback});
};
// Log the user into the API
this.post('/login', false, function(req, res) {
var responsePlace = "Login POST";
this.dbInterface.authenticate(req.body.username, req.body.password, function(err, user)
{
logger.log('debug', 'Attempting to log user ' + req.body.username);
if(err.value) {
console.log("MESSAGE: " + err.message);
responseCodes.respond(responsePlace, err, res, {account: req.body.username});
} else {
if(user)
{
createSession(responsePlace, res, req, user);
} else {
responseCodes.respond(responsePlace, responseCodes.USER_NOT_FOUND, res, {account: req.body.username});
}
}
});
});
// Log the user out of the API
this.post('/logout', false, function(req, res) {
if(!req.session.user)
{
return responseCodes.respond("Logout POST", responseCodes.NOT_LOGGED_IN, res, {});
}
var username = req.session.user.username;
req.session.destroy(function() {
logger.log('debug', 'User ' + username + ' has logged out.')
responseCodes.respond("Logout POST", responseCodes.OK, res, {account: username});
});
});
// Update or create a user's account
this.post('/:account', false, function(req, res) {
var responsePlace = "Account POST";
logger.log("debug", "Trying to affect user" + req.params["account"]);
this.dbInterface.getUserInfo( req.params["account"], false, function (err, user)
{
if (!user)
{
// Trying to sign-up
logger.log("debug", "Trying to add user " + req.params["account"]);
this.dbInterface.createUser(req.params["account"], req.body.password, req.body.email, function(err) {
createSession(responsePlace, res, req, req.params["account"]);
});
} else {
if(!req.session.user)
{
return responseCodes.respond(responsePlace, responseCodes.NOT_LOGGED_IN, res, {});
}
if (req.session.user.username != req.params['account'])
{
responseCodes.respond(responsePlace, responseCodes.NOT_AUTHORIZED, res, {account: req.params["account"]});
} else {
// Modify account here
logger.log("debug", "Updating account for " + req.params["account"]);
if (req.body.oldPassword)
{
this.dbInterface.updatePassword(req.params["account"], req.body, function(err) {
responseCodes.onError(responsePlace, err, res, {account: req.params["account"]});
});
} else {
this.dbInterface.updateUser(req.params["account"], req.body, function(err) {
responseCodes.onError(responsePlace, err, res, {account: req.params["account"]});
});
}
}
}
});
});
// Update or create a user's account
//this.post('/:account/:project', false, function(req, res) {
//});
this.post('/:account/:project/wayfinder/record', false, function(req, res) {
var resCode = responseCodes.OK;
var responsePlace = 'Wayfinder record POST';
logger.log('debug', 'Posting wayfinder record information');
console.log(JSON.stringify(req.session));
if (!("user" in req.session)) {
responseCodes.respond('Wayfinder record POST', responseCodes.NOT_LOGGED_IN, res, {});
} else {
var data = JSON.parse(req.body.data);
var timestamp = JSON.parse(req.body.timestamp);
this.dbInterface.storeWayfinderInfo(req.params["account"], req.params["project"], req.session.user.username, req.sessionID, data, timestamp, function(err) {
responseCodes.onError('Wayfinder record POST', err, res, {});
});
}
});
// Ability to add a named viewpoint
this.post('/:account/:project/:branch/viewpoint', true, function(req, res) {
var responsePlace = 'Adding a viewpoint';
logger.log('debug', 'Adding a new viewpoint to ' + req.params["account"] + req.params["project"]);
var data = JSON.parse(req.body.data);
this.dbInterface.getRootNode(req.params["account"], req.params["project"], req.params["branch"], null, function(err, root) {
if (err.value) return callback(err);
console.log(JSON.stringify(root));
this.dbInterface.storeViewpoint(req.params["account"], req.params["project"], req.params["branch"], req.session.user, this.dbInterface.uuidToString(root["shared_id"]), data, function(err) {
responseCodes.onError(responsePlace, err, res, {});
});
});
});
this.post('/:account/:project/issues/:sid', true, function(req, res) {
var responsePlace = 'Adding or updating an issue';
var data = JSON.parse(req.body.data);
logger.log('debug', 'Upserting an issues for object ' + req.params['sid'] + ' in ' + req.params["account"] + '/' + req.params["project"]);
this.dbInterface.storeIssue(req.params["account"], req.params["project"], req.params["sid"], req.session.user.username, data, function(err) {
responseCodes.onError(responsePlace, err, res, {});
});
});
// Register handlers with Express Router
for(var idx in this.postMap)
{
var item = this.postMap[idx];
console.log('debug', 'Adding POST call for ' + item['regex']);
var resFunction = schemaValidator.validate(item.regex);
if (item.shouldCheckAccess)
router.post(item.regex.toString(), resFunction, checkAccess, item.callback);
else
router.post(item.regex, resFunction, item.callback);
}
return this;
};