-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBAC.js
More file actions
202 lines (162 loc) · 5.53 KB
/
RBAC.js
File metadata and controls
202 lines (162 loc) · 5.53 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
const {any, globToRegex, isGlob} = require('./helpers');
class RBAC {
constructor(roles) {
// if roles is function => async loading, if object then sync loading.
this._inited = false;
if (typeof roles !== 'function' && typeof roles.then !== 'function') {
console.log('Sync Loading');
this.roles = this.init(roles);
this._inited = true;
}
else {
console.log('Async Loading');
// Will await on it, when `can` method is called (maybe user will call `can` long after instantiating the class, so no need to await in the begining)
this._init = this.asyncInit(roles);
}
}
init (roles) {
if (typeof roles !== 'object') {
throw new TypeError('Expected Roles to be an object');
}
let map = new Map();
Object.keys(roles).forEach(role => {
let roleObj = {
can: {},
canGlob: []
};
// Validate Can Defintion
if (!Array.isArray(roles[role].can)) {
throw new TypeError('Expected roles[' + role + '].can to be an array');
}
if (roles[role].inherits) {
if(!Array.isArray(roles[role].inherits)) {
throw new TypeError('Expected roles[' + role + '].inherits to be an array');
}
roleObj.inherits = [];
roles[role].inherits.forEach(child => {
if(typeof child !== 'string') {
throw new TypeError('Expected roles[' + role + '].inherits element');
}
if(!roles[child]) {
throw new TypeError('Undefined inheritance role: ' + child);
}
roleObj.inherits.push(child);
});
}
roles[role].can.forEach(operation => {
if (typeof operation === 'string') {
if(!isGlob(operation)) {
roleObj.can[operation] = 1;
} else {
roleObj.canGlob.push({ name: globToRegex(operation), original: operation });
}
return;
}
else if (typeof operation.name === 'string' && typeof operation.when === 'function'){
if(!isGlob(operation.name)) {
roleObj.can[operation.name] = operation.when;
} else {
roleObj.canGlob.push({ name: globToRegex(operation.name), original: operation.name, when: operation.when });
}
return;
}
throw new TypeError('Unexpected operation type', operation);
// ignore defintion we don't know..
})
map.set(role, roleObj);
});
return map;
}
async asyncInit(roles) {
if (typeof roles === 'function') {
roles = await roles(); // async function that retrieve roles config from either a DB or an API
}
if (typeof roles.then === 'function') {
roles = await roles; // wait for the promise to resolve
}
// Add roles to class and mark as inited
this.roles = this.init(roles);
this._inited = true;
}
async can (role, operation, params) {
// check async init is complete or not
if (!this._inited) {
console.log('Still trying to retrieve Roles Configuration...');
await this._init;
console.log('Done Loading Roles.');
}
// if role input is an array
if (Array.isArray(role)) {
console.log('array of roles, try all');
return any(role.map(r => this.can(r, operation, params)));
}
// checks on data types
if (typeof role !== 'string') {
console.log('Expected first parameter to be string : role');
return false;
}
if (typeof operation !== 'string') {
console.log('Expected second parameter to be string : operation');
return false
}
let roleObj = this.roles.get(role);
// check role exist
if (!roleObj) {
console.log('Undefined Role ', roleObj);
return false;
}
// operation could be string or function
// role can do operation
if (roleObj.can[operation] === 1) {
console.log('Resolved.');
return true;
}
// Operation is conditional, run async function
if (typeof roleObj.can[operation] === 'function') {
console.log('Operation is conditional, run fn');
try {
return roleObj.can[operation](params);
}
catch (e) {
console.log('conditional function threw', e);
return false;
}
}
// operation is not defined at current level try higher
if (!roleObj.can[operation] && !roleObj.canGlob.find(glob => glob.name.test(operation))) {
console.log('Not allowed at this level, try higher');
// If no parents reject
if (!roleObj.inherits || roleObj.inherits.length < 1) {
console.log('No inherit, reject false');
return false;
}
// Return if any parent resolves true or all reject
return any(roleObj.inherits.map(parent => {
console.log('Try from ' + parent);
return this.can(parent, operation, params);
}));
}
// Try globals
let globMatch = roleObj.canGlob.find(glob => glob.name.test(operation));
if(globMatch && !globMatch.when) {
console.log(`We have a globmatch (${globMatch.original}), resolve`);
return true;
}
if(globMatch && globMatch.when) {
console.log(`We have a conditional globmatch (${globMatch.original}), run fn`);
try {
return globMatch.when(params);
}
catch (e) {
console.log('conditional function threw', e);
return false;
}
}
console.log('Shouldnt have reached here, something wrong, reject');
throw new Error('something went wrong');
}
}
RBAC.create = function create(opts) {
return new RBAC(opts);
}
module.exports = RBAC;