/
routingConfig.js
99 lines (79 loc) · 3.36 KB
/
routingConfig.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
(function(exports){
var config = {
/* List all the roles you wish to use in the app
* You have a max of 31 before the bit shift pushes the accompanying integer out of
* the memory footprint for an integer
*/
roles :[
'public',
'user',
'admin'],
/*
Build out all the access levels you want referencing the roles listed above
You can use the "*" symbol to represent access to all roles.
The left-hand side specifies the name of the access level, and the right-hand side
specifies what user roles have access to that access level. E.g. users with user role
'user' and 'admin' have access to the access level 'user'.
*/
accessLevels : {
'public' : "*",
'anon': ['public'],
'user' : ['user', 'admin'],
'admin': ['admin']
}
}
exports.userRoles = buildRoles(config.roles);
exports.accessLevels = buildAccessLevels(config.accessLevels, exports.userRoles);
/*
Method to build a distinct bit mask for each role
It starts off with "1" and shifts the bit to the left for each element in the
roles array parameter
*/
function buildRoles(roles){
var bitMask = "01";
var userRoles = {};
for(var role in roles){
var intCode = parseInt(bitMask, 2);
userRoles[roles[role]] = {
bitMask: intCode,
title: roles[role]
};
bitMask = (intCode << 1 ).toString(2)
}
return userRoles;
}
/*
This method builds access level bit masks based on the accessLevelDeclaration parameter which must
contain an array for each access level containing the allowed user roles.
*/
function buildAccessLevels(accessLevelDeclarations, userRoles){
var accessLevels = {};
for(var level in accessLevelDeclarations){
if(typeof accessLevelDeclarations[level] == 'string'){
if(accessLevelDeclarations[level] == '*'){
var resultBitMask = '';
for( var role in userRoles){
resultBitMask += "1"
}
//accessLevels[level] = parseInt(resultBitMask, 2);
accessLevels[level] = {
bitMask: parseInt(resultBitMask, 2)
};
}
else console.log("Access Control Error: Could not parse '" + accessLevelDeclarations[level] + "' as access definition for level '" + level + "'")
}
else {
var resultBitMask = 0;
for(var role in accessLevelDeclarations[level]){
if(userRoles.hasOwnProperty(accessLevelDeclarations[level][role]))
resultBitMask = resultBitMask | userRoles[accessLevelDeclarations[level][role]].bitMask
else console.log("Access Control Error: Could not find role '" + accessLevelDeclarations[level][role] + "' in registered roles while building access for '" + level + "'")
}
accessLevels[level] = {
bitMask: resultBitMask
};
}
}
return accessLevels;
}
})(typeof exports === 'undefined' ? this['routingConfig'] = {} : exports);