forked from vgheri/ShopWithMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
securityPolicy.js
75 lines (70 loc) · 2.38 KB
/
securityPolicy.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
/**
* Created with JetBrains WebStorm.
* User: valerio
* Date: 22/06/13
* Time: 15.14
* To change this template use File | Settings | File Templates.
*/
var SecurityToken = require('./infrastructure/securityToken');
var logger = require('utils/logger');
var Q = require('q');
// A user can fetch a shopping list only if the list id is contained in the array "Account.shoppingLists"
// and the list is active (both condition should always be true or false together)
function userCanFetchShoppingList(account, shoppingList) {
var userHasRight = false;
if (account.shoppingLists.indexOf(shoppingList._id) > -1 && shoppingList.isActive) {
userHasRight = true;
}
else {
userHasRight = false;
}
return userHasRight;
}
// A user can fetch a shopping list only if the list id is contained in the array "Account.shoppingLists"
// and the list is active
function userCanUpdateOrDeleteShoppingList(account, shoppingList) {
var userHasRight = false;
if (account._id.equals(shoppingList.createdBy) && account.shoppingLists.indexOf(shoppingList._id) > -1 &&
shoppingList.isActive) {
userHasRight = true;
}
else {
userHasRight = false;
}
return userHasRight;
}
function authorise(req, res, next) {
var apiAccessToken = req.body.apiAccessToken || null;
var userId = req.params.userId || req.body.userId || null;
if (apiAccessToken && userId) {
SecurityToken.authorise(apiAccessToken, userId)
.then(function(authorised) {
if (authorised) {
next();
}
else {
logger.log('info', 'User ' + userId + ' is not authorised. Request from address ' + req.connection.remoteAddress + '.');
res.json(401, {
error: "User is not authorised"
});
}
}, function(err) {
logger.log('error', 'An error has occurred while processing a request ' +
' from ' +
req.connection.remoteAddress + '. Stack trace: ' + err.stack);
res.json(500, {
error: err.message
});
});
}
else {
logger.log('info', 'Bad request from ' +
req.connection.remoteAddress + '. Api access token and user id are mandatory.');
res.json(400, {
error: 'Api access token and user id are mandatory.'
});
}
}
exports.authorise = authorise;
exports.userCanFetchShoppingList = userCanFetchShoppingList;
exports.userCanUpdateOrDeleteShoppingList = userCanUpdateOrDeleteShoppingList;