Repository navigation
Expand file tree
/
Copy pathUserController.js
More file actions
374 lines (336 loc) · 13.4 KB
/
Copy pathUserController.js
File metadata and controls
374 lines (336 loc) · 13.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* UserController
*
* @module :: Controller
* @description :: A set of functions called `actions`.
*
* Actions contain code telling Sails how to respond to a certain type of request.
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
*
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
* and/or override them with custom routes (`config/routes.js`)
*
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
*
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
"use strict";
var async = require("async");
var moment = require("moment-timezone");
var languages = require("./../../config/i18n.js");
module.exports = {
/**
* Overrides for the settings in `config/controllers.js`
* (specific to UserController)
*/
_config: {},
/**
* User add action. This will render a new user add GUI.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
add: function(request, response) {
response.view({
languages: languages.i18n.locales,
timezones: DateService.getTimezones()
});
},
/**
* User edit action. This will render a user edit GUI.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
edit: function(request, response) {
var userId = request.param("id");
// Fetch user data
DataService.getUser(userId, function(error, user) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view({
user: user,
languages: languages.i18n.locales,
timezones: DateService.getTimezones()
});
}
});
},
/**
* User list action. This will render a GUI where all users are shown in list.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
list: function(request, response) {
async.waterfall(
[
// Fetch users
function(callback) {
DataService.getUsers({}, callback);
},
// Fetch last logins for users
function(users, callback) {
fetchLastSignIn(users, callback);
}
],
/**
* Main callback function which is called after all waterfall jobs are processed
* or an error occurred on those.
*
* @param {null|Error} error
* @param {sails.model.user[]} users
*/
function(error, users) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view({
users: users
});
}
}
);
/**
* Private function to fetch last sign in data for each user.
*
* @param {sails.model.user[]} users
* @param {Function} next
*/
function fetchLastSignIn(users, next) {
// Map users
async.map(
users,
/**
* Iterator function which is called with every user object in input array.
* Function will fetch last sign in information of current user.
*
* @param {sails.model.user} user User object
* @param {Function} callback Callback function to call after job is finished
*/
function(user, callback) {
// Fetch user last sign in record
UserLogin
.findOne({
userId: user.id
})
.sort("id DESC")
.limit(1)
.done(function(error, loginData) {
if (error) {
callback(error, null);
} else {
// Add last login to user data
user.lastLogin = (!loginData) ? null : loginData.stamp;
if (user.lastLogin !== null) {
moment.lang(request.user.language);
user.lastLogin = DateService.convertDateObjectToUtc(user.lastLogin);
user.lastLogin.tz(request.user.momentTimezone);
}
callback(null, user);
}
});
},
/**
* Callback function which is called after all users have been processed with iterator
* function.
*
* @param {null|Error} error Error object
* @param {sails.model.user[]} users User objects extended by 'lastLogin' information
*/
function(error, users) {
next(error, users);
}
);
}
},
/**
* User sign in history action. This will render a GUI where is shown specified user sign in
* history data. This data contains
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
history: function(request, response) {
var userId = request.param("id");
// Make parallel jobs for history action
async.parallel(
{
// Fetch single user data
user: function(callback) {
DataService.getUser(userId, callback);
},
// Fetch user sign in data
history: function(callback) {
DataService.getUserSignInData(userId, callback);
}
},
/**
* Callback function which is called after all parallel jobs have been processed.
*
* @param {Error} error Error object
* @param {{}} data Object that contains 'user' and 'history' data
*/
function (error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
moment.lang(request.user.language);
// Iterate sign in rows and make formatted stamp
_.each(data.history, function(row) {
row.stamp = DateService.convertDateObjectToUtc(row.stamp);
row.stamp.tz(request.user.momentTimezone);
});
// Group sign in data by IP addresses
data.ipData = _.groupBy(data.history, function(row) {
return row.ip;
});
// Group sign in data by user agents
data.agentData = _.groupBy(data.history, function(row) {
return row.agent;
});
response.view(data);
}
}
);
},
/**
* User projects action. Basically this action will show all projects that current user
* is affected to in some role.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
projects: function(request, response) {
var userId = request.param("id");
// Make parallel jobs for 'projects' action
async.parallel(
{
// Fetch single user data
user: function(callback) {
DataService.getUser(userId, callback);
},
// Fetch project data
projects: function(callback) {
DataService.getProjects({}, callback);
}
},
/**
* Callback function which is called after all parallel jobs have been processed.
*
* @param {null|Error} error Error object
* @param {{
* user: sails.model.user,
* projects: sails.model.project[]
* }} data Object that contains 'user' and 'projects' data
*/
function (error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
filterOutInvalidProjects(data);
}
}
);
/**
* Private function to filter out projects that current user has no access to.
*
* @param {{}} data
*/
function filterOutInvalidProjects(data) {
moment.lang(request.user.language);
async.filter(
data.projects,
/**
* Iterator function which will filter out projects that current user has no access.
*
* @param {sails.model.project} project Project object
* @param {Function} callback Callback function to call after project is processed
*/
function(project, callback) {
AuthService.hasProjectAccess(data.user, project.id, function(error, role) {
if (role !== false) {
project.role = role;
project.roleText = "Unknown";
project.dateStart = DateService.convertDateObjectToUtc(project.dateStart);
project.dateStart.tz(request.user.momentTimezone);
project.dateEnd = DateService.convertDateObjectToUtc(project.dateEnd);
project.dateEnd.tz(request.user.momentTimezone);
switch (project.role) {
case -3:
project.roleText = "Administrator";
break;
case -2:
project.roleText = "Manager (Primary)";
break;
case -1:
project.roleText = "Manager";
break;
case 0:
project.roleText = "Viewer";
break;
case 1:
project.roleText = "User";
break;
}
callback(true);
} else {
callback(false);
}
}, true);
},
/**
* Callback function which is called after all projects have been processed by
* specified iterator function.
*
* @param {sails.model.project[]} projects Project objects extended by role
*/
function(projects) {
data.projects = projects;
response.view(data);
}
);
}
},
/**
* User password change action.
*
* todo add support for admin users to change anyone password
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
changePassword: function(request, response) {
var userId = request.param("userId");
var passwordCurrent = request.param("passwordCurrent");
var passwordNew = request.param("password");
var passwordReType = request.param("passwordReType");
if (passwordNew !== passwordReType) {
response.send(400, "Given passwords doesn't match.");
} else if (passwordNew.length < 10) {
response.send(400, "Given new password is too short.");
} else if (passwordCurrent.length < 10) {
response.send(400, "Given current password is too short.");
} else {
// Get user object
DataService.getUser(userId, function(error, user) {
if (error) {
ResponseService.makeError(error, request, response);
} else if (!user.validPassword(passwordCurrent)) {
response.send(400, "Given current password value doesn't match.");
} else {
// Set new password value for user
user.password = passwordNew;
user.save(function(error) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.json(true);
}
});
}
});
}
}
};