-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (41 loc) · 1.41 KB
/
index.js
File metadata and controls
47 lines (41 loc) · 1.41 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
const create = require('./src/permissionTree');
function resolveToArray(value) {
if (value === undefined) {
return [];
}
return Array.isArray(value) ? value : [value];
}
function isPermited(permitedPermissionStrings, testingPermissionStrings) {
if (!testingPermissionStrings) {
throw Error('provide permission string for testing as first argument');
}
if (!permitedPermissionStrings) {
throw Error('provide permission string that are permitted as second argument');
}
const permits = create(...resolveToArray(permitedPermissionStrings));
const permissions = create(...resolveToArray(testingPermissionStrings));
if (permits['*'] && permits['*']['*'] && permits['*']['*'].includes('*')) {
return true;
}
return Object.keys(permits).every((resource) => {
if (permissions[resource] && permits[resource]['*'] && permits[resource]['*'].includes('*')) {
return true;
}
return Object.keys(permits[resource]).every((action) => {
if (permissions[resource] && permissions[resource][action] && permits[resource][action].includes('*')) {
return true;
}
return permits[resource][action].every((id) => {
if (
permissions[resource]
&& permissions[resource][action]
&& permissions[resource][action].includes(id)
) {
return true;
}
return false;
});
});
});
}
module.exports = isPermited;