3e0ee90ab7dadefc676823f21937000fa4aeddc5
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / dlux-web / src / app / routingConfig.js
1 (function(exports){
2
3     var config = {
4
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
8         */
9         roles :[
10             'public',
11             'user',
12             'admin'],
13
14         /*
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
17          */
18         accessLevels : {
19             'public' : "*",
20             'anon': ['public'],
21             'user' : ['user', 'admin'],
22             'admin': ['admin']
23         }
24
25     };
26
27     exports.userRoles = buildRoles(config.roles);
28     exports.accessLevels = buildAccessLevels(config.accessLevels, exports.userRoles);
29
30     /*
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
33         roles array parameter
34      */
35
36     function buildRoles(roles){
37
38         var bitMask = "01";
39         var userRoles = {};
40
41         for(var role in roles){
42             var intCode = parseInt(bitMask, 2);
43             userRoles[roles[role]] = {
44                 bitMask: intCode,
45                 title: roles[role]
46             };
47             bitMask = (intCode << 1 ).toString(2);
48         }
49
50         return userRoles;
51     }
52
53     /*
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.
56      */
57     function buildAccessLevels(accessLevelDeclarations, userRoles){
58
59         var accessLevels = {};
60         var resultBitMask = '';
61         for(var level in accessLevelDeclarations){
62
63             if(typeof accessLevelDeclarations[level] == 'string'){
64                 if(accessLevelDeclarations[level] == '*'){
65
66                     resultBitMask = '';
67
68                     for( var roleTemp in userRoles){
69                         resultBitMask += "1";
70                     }
71                     //accessLevels[level] = parseInt(resultBitMask, 2);
72                     accessLevels[level] = {
73                         bitMask: parseInt(resultBitMask, 2),
74                         title: accessLevelDeclarations[level]
75                     };
76                 }
77                 else {
78                     console.log("Access Control Error: Could not parse '" + accessLevelDeclarations[level] + "' as access definition for level '" + level + "'");
79                 }
80
81             }
82             else {
83
84                 resultBitMask = 0;
85                 for(var role in accessLevelDeclarations[level]){
86                     if(userRoles.hasOwnProperty(accessLevelDeclarations[level][role])) {
87                         resultBitMask = resultBitMask | userRoles[accessLevelDeclarations[level][role]].bitMask;
88                     }
89                     else {
90                         console.log("Access Control Error: Could not find role '" + accessLevelDeclarations[level][role] + "' in registered roles while building access for '" + level + "'");
91                     }
92                 }
93                 accessLevels[level] = {
94                     bitMask: resultBitMask,
95                     title: accessLevelDeclarations[level][role]
96                 };
97             }
98         }
99
100         return accessLevels;
101     }
102
103 })(typeof exports === 'undefined' ? this['routingConfig'] = {} : exports);