-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (95 loc) · 2.87 KB
/
index.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
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
module.exports = () => {
const res = {
_forward: {},
_backward: {},
_permissions: {},
add: (from, to) => {
if (!res._forward[from]) res._forward[from] = {}
res._forward[from][to] = true
if (!res._backward[to]) res._backward[to] = {}
res._backward[to][from] = true
},
remove: (from, to) => {
if (res._forward[from]) delete res._forward[from][to]
if (res._backward[to]) delete res._backward[to][from]
},
allow: (from, to) => {
if (!res._permissions[from]) res._permissions[from] = {}
res._permissions[from][to] = true
},
disallow: (from, to) => {
if (!res._permissions[from]) return
delete res._permissions[from][to]
},
parents: (from) => {
if (!res._forward[from]) return []
return Object.keys(res._forward[from])
},
hasparent: (from, to) => {
if (!res._forward[from]) return false
for (let child of Object.keys(res._forward[from])) {
if (child == to) return true
if (res.hasparent(child, to)) return true
}
return false
},
ancestors: (from) => {
let current = []
let processing = [from]
let ancestors = []
while (processing.length > 0) {
ancestors = ancestors.concat(current)
current = []
for (let check of processing)
current = current.concat(res.parents(check))
processing = current
}
return ancestors
},
children: (from) => {
if (!res._backward[from]) return []
return Object.keys(res._backward[from])
},
haschild: (from, to) => {
if (!res._backward[from]) return false
for (let child of Object.keys(res._backward[from])) {
if (child == to) return true
if (res.haschild(child, to)) return true
}
return false
},
descendants: (from) => {
let current = []
let processing = [from]
let descendants = []
while (processing.length > 0) {
descendants = descendants.concat(current)
current = []
for (let check of processing)
current = current.concat(res.children(check))
processing = current
}
return descendants
},
members: (from) => [from].concat(res.ancestors(from)),
permissions: (from) => {
const members = res.members(from).concat(res.members('*'))
let permissions = []
for (let member of members) {
if (!res._permissions[member]) continue
permissions = permissions.concat(Object.keys(res._permissions[member]))
}
return permissions
},
can: (from, to) => {
const members = res.members(from).concat(res.members('*'))
for (let member of members) {
if (!res._permissions[member]) continue
if (res._permissions[member][to]) return true
if (res._permissions[member]['*']) return true
}
return false
}
}
return res
}