Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathProjectUserController.js
More file actions
425 lines (378 loc) · 15 KB
/
Copy pathProjectUserController.js
File metadata and controls
425 lines (378 loc) · 15 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* ProjectUserController
*
* @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
*/
var async = require("async");
module.exports = {
/**
* Overrides for the settings in `config/controllers.js`
* (specific to ProjectUserController)
*/
_config: {},
/**
* This actions makes view for project users. Note that 'projectId' parameter is
* required for this action.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
users: function(request, response) {
var projectId = request.param("projectId");
var data = {
users: false,
project: false,
role: false
};
async.parallel(
{
// Fetch project data
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch current user role in project
role: function(callback) {
AuthService.hasProjectAccess(request.user, projectId, callback, true);
},
// Fetch project users
projectUsers: function(callback) {
DataService.getProjectUsers({projectId: projectId}, callback);
},
// Fetch admin users
adminUsers: function(callback) {
DataService.getUsers({ admin: true, username: {"!": "admin"} }, callback);
},
// Fetch all users
users: function(callback) {
DataService.getUsers({}, callback);
}
},
/**
* Callback function that is called after all parallel jobs are done
* or some error has happen in those
*
* @param {null|Error} error Error data
* @param {{
* project: sails.model.project,
* role: sails.helper.role,
* projectUsers: sails.model.projectUser[],
* adminUsers: sails.model.user[],
* users: sails.model.user[]
* }} results Object that contains all needed data
*/
function callback(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.project = results.project;
data.role = results.role;
// Remove possible duplicate users and admin users
results.projectUsers = _.filter(results.projectUsers, function(projectUser) {
var founded = false;
if (projectUser.userId === results.project.managerId) {
founded = true;
} else {
var adminUser = _.find(results.adminUsers, function(adminUser) {
return adminUser.id === projectUser.userId;
});
if (adminUser) {
founded = true;
}
}
return !founded;
});
// Add real project main manager to user list
results.projectUsers.push({
projectId: results.project.id,
userId: results.project.managerId,
role: -1,
main: 1
});
// Add admin users
_.each(results.adminUsers, function(adminUser) {
var founded = _.find(results.projectUsers, function(projectUser) {
return projectUser.userId === adminUser.id;
});
if (!founded) {
results.projectUsers.push({
projectId: results.project.id,
userId: adminUser.id,
role: -1,
main: 0,
admin: true
});
}
});
determineUserData(results.projectUsers, results.users);
}
}
);
/**
* Private function to determine project user data. This will combine project user and
* user data objects for GUI.
*
* @param {sails.model.projectUser[]} projectUsers
* @param {sails.model.user[]} users
*/
function determineUserData(projectUsers, users) {
async.map(
projectUsers,
/**
* Iterator function to combine project user and main user data.
*
* @param {sails.model.projectUser} projectUser
* @param {Function} callback
*/
function(projectUser, callback) {
var user = _.find(users, function(user) {
return user.id === projectUser.userId;
});
if (!user) {
callback("User not found, weird this should not happen...", null);
} else {
var roleText = "Unknown";
switch (projectUser.role) {
case -1:
roleText = "Manager";
break;
case 0:
roleText = "Viewer";
break;
case 1:
roleText = "User";
break;
}
projectUser.roleText = roleText;
user.projectUser = projectUser;
callback(null, user);
}
},
/**
* Main callback function which is called after all project users have been
* processed.
*
* @param {null|Error} error
* @param {sails.model.user[]} users
*/
function(error, users) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.users = users.sort(HelperService.dynamicSortMultiple("lastName", "firstName", "username"));
response.view(data);
}
}
);
}
},
/**
* This actions fetches available users for specified project. These users can be
* attached to specified project.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
availableUsers: function(request, response) {
var projectId = parseInt(request.param("projectId"), 10);
async.parallel(
{
// Fetch project data
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch current project users
projectUsers: function(callback) {
DataService.getProjectUsers({projectId: projectId}, callback);
}
},
/**
* Main callback function which is called after all parallel jobs are done.
*
* @param {null|Error} error
* @param {{
* project: sails.model.project,
* projectUsers: sails.model.projectUser[]
* }} results
*/
function (error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
fetchAvailableUsers(results);
}
}
);
/**
* Private function to fetch available users for this project. Available users are not
* yet attached to this project nor user is administrator user. Administrator users have
* always access to projects.
*
* @param {{
* project: sails.model.project,
* projectUsers: sails.model.projectUser[]
* }} data
*/
function fetchAvailableUsers(data) {
var userIds = _.map(data.projectUsers, function(user) {
return {id: {"!": user.userId}};
});
userIds.push({id: {"!": data.project.managerId}});
var where = {
and: userIds,
admin: 0
};
// Fetch available users
DataService.getUsers(where, function(error, users) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.json(200, users);
}
});
}
},
/**
* This actions fetches available users for specified project. These users can be
* attached to specified project.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
ownProjects: function(request, response) {
async.waterfall(
[
// Fetch project user data
function(callback) {
DataService.getProjectUsers({userId: request.user.id}, callback)
},
// Map project user data
function(projectUsers, callback) {
var projectIds = _.map(projectUsers, function(projectUser) {
return {id: projectUser.projectId};
});
callback(null, projectIds);
}
],
/**
* Main callback function which is called after all waterfall jobs are done.
*
* @param {null|Error} error Possible error
* @param {{}} conditions Project conditions
*/
function(error, conditions) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
fetchProjects(conditions);
}
}
);
/**
* Private function to fetch project data where current user is in some role. Note
* that administrator users have all access to all projects.
*
* @param {{id: {Number}}[]} conditions Project ids as an array of objects
*/
function fetchProjects(conditions) {
var where = {};
// Administrator user has access to all projects
if (!request.user.admin) {
// Add project manager condition
conditions.push({
managerId: request.user.id
});
where = {
or: conditions
};
}
// Fetch project data
DataService.getProjects(where, function(error, projects) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.json(200, projects);
}
});
}
},
/**
* Method returns user role in specified project. Successfully output is always
* json which contains user role:
*
* -3 = User is administrator
* -2 = Project manager (primary)
* -1 = Project manager
* 0 = Only view rights
* 1 = Normal user
*
* false if nothing matches.
*
* @param {Request} request Request object
* @param {Response} response Response object
*
* @constructor
*/
getRole: function(request, response) {
var projectId = parseInt(request.param("projectId"), 10);
// Admin user, always return -3
if (request.user.admin) {
response.json(200, -3);
} else { // Otherwise fetch user role
async.parallel(
{
/**
* Function to fetch possible Project object for signed in user and
* specified project. User must be project manager.
*
* @param {Function} callback
*/
primary: function(callback) {
DataService.getProject({id: projectId, managerId: request.user.id}, callback, true);
},
/**
* Function to fetch possible ProjectUser object for signed in user
* and specified project.
*
* @param {Function} callback
*/
contributor: function(callback) {
DataService.getProjectUser({projectId: projectId, userId: request.user.id}, callback, true);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error.
* @param {{
* primary: sails.model.project
* contributor: sails.model.projectUser
* }} results Fetched data as an object
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
var output = false;
if (results.primary) {
output = -2;
} else if (results.contributor) {
output = results.contributor.role;
}
response.json(200, output);
}
}
);
}
}
};
You can’t perform that action at this time.
