forked from tensult/role-acl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (59 loc) · 1.91 KB
/
app.js
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
const AccessControl = require("./index");
const matcher = require("matcher");
AccessControl.isAllowed = function (account, user, resource, action, context) {
const acl = new AccessControl(account.acl);
const permission = acl.permission({
resource,
action,
role: user.policy.roles,
context
});
return permission.granted;
}
AccessControl.getAllowedAttributes = function (account, user, resource, action, context) {
const acl = new AccessControl(account.acl);
const permission = acl.permission({
resource,
action,
role: user.policy.roles,
context
});
return permission.attributes;
}
AccessControl.allowedResources = function (account, user) {
const acl = new AccessControl(account.acl);
const permittedResources = acl.allowedResources({
role: user.policy.roles
});
return matcher(Object.keys(account.permissions), permittedResources);
}
AccessControl.allowedActions = function (account, user, resource) {
const acl = new AccessControl(account.acl);
const permittedActions = acl.allowedActions({
role: user.policy.roles,
resource
});
return matcher(account.permissions[resource], permittedActions);
}
AccessControl.allowedCategories = function (account, user, resource, action) {
const acl = new AccessControl(account.acl);
const permittedCategories = acl.permission({
role: user.policy.roles,
resource: "category",
action: "search"
}).attributes;
if (permittedCategories.length === 1 && permittedCategories[0] === "*") {
return permittedCategories;
}
return permittedCategories.filter((category => {
return acl.permission({
role: user.policy.roles,
resource,
action,
context: {
category
}
}).granted;
}));
}
window.AccessControl = AccessControl;