-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
331 lines (315 loc) · 8.64 KB
/
index.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { flatten, mergeRoles } from './utils';
const SEPARATOR = ':';
/**
* RBAC classref
*/
export class RBAC {
/**
* RBAC roles object
*/
private _rules: RBAC.RoleRules = {};
private _rulesCompiled: { [rule: string]: boolean | RBAC.WhenFn } = {};
private _refs: RBAC.Refs = {};
private _memoize = true;
private _collectRefs(refs?: RBAC.RoleRules) {
if (typeof refs === 'undefined') {
return {};
}
const refsToCompile: RBAC.ResourceRules = {};
for (const refRole of Object.keys(refs)) {
mergeRoles(
refsToCompile,
this._collectRefs(this._refs[refRole]),
refs[refRole]
);
}
return refsToCompile;
}
private _compile() {
const refsToCompile: RBAC.RoleRules = {};
for (const [role, ref] of Object.entries(this._refs)) {
refsToCompile[role] = refsToCompile[role] || {};
refsToCompile[role] = this._collectRefs(ref);
}
this._rulesCompiled = {
...flatten(refsToCompile, SEPARATOR),
...flatten(this._rules, SEPARATOR),
};
}
/**
* RBAC constructor
* @param options RBAC options
*/
constructor(options: RBAC.Options = {}) {
const { roles = {}, memoize = true } = options;
this._memoize = memoize;
for (const [roleName, permissions] of Object.entries(roles)) {
if (permissions.can.length !== 0) {
this._rules[roleName] = this._rules[roleName] || {};
const resRule = this._rules[roleName];
for (const permission of permissions.can) {
if (typeof permission === 'string') {
const [resource, operation = '*'] = permission.split(SEPARATOR);
resRule[resource] = resRule[resource] || {};
const opRule = resRule[resource];
opRule[operation] = true;
} else {
const [resource, operation] = permission.operation
? [permission.name, permission.operation]
: permission.name.split(SEPARATOR);
resRule[resource] = resRule[resource] || {};
const opRule = resRule[resource];
opRule[operation] =
typeof permission.when !== 'undefined' ? permission.when : true;
}
}
}
if (permissions.inherits) {
for (const refRole of permissions.inherits) {
this._refs[roleName] = this._refs[roleName] || {};
// this._rules[refRole] = this._rules[refRole] || {};
this._refs[roleName][refRole] = this._rules[refRole];
}
}
}
this._compile();
}
/**
* Adds new role to rules.
* @public
* @version 1.1.X
* @param role user role
* @param resource resource to access
* @param operation allowed operation
* @param when function for additional checks
*/
public add(
role: string,
resource: string,
operation: string,
when?: RBAC.WhenFn
): void {
this._rules[role] = this._rules[role] || {};
this._rules[role][resource] = this._rules[role][resource] || {};
this._rules[role][resource][operation] =
typeof when === 'undefined' ? true : when;
this._compile();
}
/**
* Remove rule(s).
* @public
* @version 1.1.X
* @param role user role
* @param resource resource to access
* @param operation operation
*/
public remove(role: string, resource = '*', operation = '*'): void {
if (resource === '*') {
if (operation === '*') {
// role:*:*
delete this._rules[role];
} else {
// role:*:operation
if (this._rules[role][resource]) {
delete this._rules[role][resource][operation];
}
for (const [res, opRule] of Object.entries(this._rules[role])) {
if (Object.keys(opRule).indexOf(operation) !== -1) {
delete this._rules[role][res][operation];
}
}
}
} else {
if (operation === '*') {
// role:resource:*
delete this._rules[role][resource];
} else {
// role:resource:operation
delete this._rules[role][resource][operation];
}
}
this._compile();
}
/**
* Checks if user can perform operation without checking when condition.
* @public
* @version 1.X.X
* @param role user role
* @param resource resource to access
* @param operation operation on resource
* @returns true if role has access to resources
*/
public can(role: string, resource: string, operation?: string): boolean;
/**
* Checks if user can perform operation with checking when condition if it's provided.
* @public
* @version 1.X.X
* @param role user role
* @param resource resource to access
* @param operation operation on resource
* @param context context passed to when function, set it to null
* @returns true if role has access to resources.
*/
public can<TContext = any>(
role: string,
resource: string,
operation: string,
context: TContext
): Promise<boolean>;
public can<TContext = any>(
role: string,
resource: string,
operation: string,
context?: TContext
): boolean | Promise<boolean> {
let check: boolean | RBAC.WhenFn = false;
const checkWhen = typeof context !== 'undefined';
const rule = [role, resource, operation].join(SEPARATOR);
if (Object.hasOwnProperty.call(this._rulesCompiled, rule)) {
check = this._rulesCompiled[rule];
} else {
const wildcardRules = [
[role, resource, '*'].join(SEPARATOR),
[role, '*', operation].join(SEPARATOR),
[role, '*', '*'].join(SEPARATOR),
];
for (const wcr of wildcardRules) {
if (Object.hasOwnProperty.call(this._rulesCompiled, wcr)) {
check = this._rulesCompiled[wcr];
if (this._memoize) {
// add to rules
this._rulesCompiled[rule] = this._rulesCompiled[wcr];
this._rules[role][resource] = this._rules[role][resource] || {};
this._rules[role][resource][operation] = check;
}
}
}
}
if (typeof check === 'boolean') {
return check;
}
if (checkWhen) {
return check(context);
}
return Boolean(check);
}
}
export namespace RBAC {
/**
* Dynamic condition check function.
*/
export type WhenFn<TContext = any> = (
context: TContext
) => boolean | Promise<boolean>;
/**
* Interherence references.
* @see {@link RBAC.RoleRules}
*/
export interface Refs {
/**
* Role rules.
* {@link (RBAC:namespace).RoleRules}
*/
[roleName: string]: RoleRules;
}
/**
* Operation permission list.
*/
export interface OperationRules {
/**
* Operation permission.
* @description `true` if allowed or `function` if need additional dynamic checks
* @see {@link RBAC.WhenFn}
*/
[operationName: string]: boolean | WhenFn;
}
/**
* Resource operations list.
* @type Object<string, {@link RBAC.OperationRules}>
* @see {@link RBAC.OperationRules}
*/
export interface ResourceRules {
/**
* Resoure operation.
*/
[resourceName: string]: OperationRules;
}
/**
* Role's resources list.
* @type Object<string, {@link RBAC.ResourceRules}>
* @see {@link RBAC.ResourceRules}
*/
export interface RoleRules {
/**
* Resource permissions list.
*/
[roleName: string]: ResourceRules;
}
/**
* Resoure permission.
*/
export interface ResourcePermission {
/**
* Resourece name or resource with operation.
* @example "foo"
* @example "foo:read"
*/
name: string;
/**
* Operation name.
* @example "read"
*/
operation?: string;
/**
* Dynamic condition check function.
* @see {@link RBAC.WhenFn}
*/
when?: WhenFn;
}
/**
* List of RBAC rules and inherited roles.
*/
export interface RulesObject {
/**
* List of resource and permissions.
* @example
* can: ["foo:create", "bar:*", "*:read"]
* can: ["*"]
* can: [{
* name: "baz",
* operation: "create",
* when: (ctx) => {
* return ctx.user.id === ctx.obj.creatorId
* }
* }]
* @see {@link RBAC.ResourcePermission}
*/
can: Array<string | ResourcePermission>;
/**
* Optionally extend permissions from other roles.
*/
inherits?: Array<string>;
}
/**
* RBAC options.
*/
export interface Options {
/**
* List of roles and their permissions.
* @type Object<string, {@link RBAC.RulesObject}>
* @see {@link RBAC.RulesObject}
*/
roles?: {
/**
* Role and it's permissions.
*/
[roleName: string]: RBAC.RulesObject;
};
/**
* If true makes wildcard matches faster.
* @default true
*/
memoize?: boolean;
}
}
export default RBAC;