5 /* List all the roles you wish to use in the app
6 * You have a max of 31 before the bit shift pushes the accompanying integer out of
7 * the memory footprint for an integer
15 Build out all the access levels you want referencing the roles listed above
16 You can use the "*" symbol to represent access to all roles
21 'user' : ['user', 'admin'],
27 exports.userRoles = buildRoles(config.roles);
28 exports.accessLevels = buildAccessLevels(config.accessLevels, exports.userRoles);
31 Method to build a distinct bit mask for each role
32 It starts off with "1" and shifts the bit to the left for each element in the
36 function buildRoles(roles){
41 for(var role in roles){
42 var intCode = parseInt(bitMask, 2);
43 userRoles[roles[role]] = {
47 bitMask = (intCode << 1 ).toString(2);
54 This method builds access level bit masks based on the accessLevelDeclaration parameter which must
55 contain an array for each access level containing the allowed user roles.
57 function buildAccessLevels(accessLevelDeclarations, userRoles){
59 var accessLevels = {};
60 var resultBitMask = '';
61 for(var level in accessLevelDeclarations){
63 if(typeof accessLevelDeclarations[level] == 'string'){
64 if(accessLevelDeclarations[level] == '*'){
68 for( var roleTemp in userRoles){
71 //accessLevels[level] = parseInt(resultBitMask, 2);
72 accessLevels[level] = {
73 bitMask: parseInt(resultBitMask, 2),
74 title: accessLevelDeclarations[level]
78 console.log("Access Control Error: Could not parse '" + accessLevelDeclarations[level] + "' as access definition for level '" + level + "'");
85 for(var role in accessLevelDeclarations[level]){
86 if(userRoles.hasOwnProperty(accessLevelDeclarations[level][role])) {
87 resultBitMask = resultBitMask | userRoles[accessLevelDeclarations[level][role]].bitMask;
90 console.log("Access Control Error: Could not find role '" + accessLevelDeclarations[level][role] + "' in registered roles while building access for '" + level + "'");
93 accessLevels[level] = {
94 bitMask: resultBitMask,
95 title: accessLevelDeclarations[level][role]
103 })(typeof exports === 'undefined' ? this['routingConfig'] = {} : exports);