-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpermissions.js
99 lines (81 loc) · 2.05 KB
/
permissions.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
96
97
98
99
'use strict'
const u = require('./util')
function toArray (str) {
return Array.isArray(str) ? str : str.split('.')
}
function isPerms (p) {
return (
p &&
typeof p.pre === 'function' &&
typeof p.test === 'function' &&
typeof p.post === 'function'
)
}
/*
perms:
a given capability may be permitted to call a particular api.
but only if a perms function returns true for the arguments
it passes.
suppose, an app may be given access, but may only create functions
with it's own properties.
create perms:
{
allow: ['add', 'query'], deny: [...],
rules: {
add: {
call: function (value) {
return (value.type === 'task' || value.type === '_task')
},
query: {
call: function (value) {
safe.contains(value, {path: ['content', 'type'], eq: 'task'}) ||
safe.contains(value, {path: ['content', 'type'], eq: '_task'})
},
filter: function (value) {
return (value.type === 'task' || value.type === '_task')
}
}
}
}
*/
module.exports = function Permissions (opts) {
if (isPerms(opts)) return opts
if (typeof opts === 'function') return { pre: opts }
let allow = null
let deny = {}
function perms (opts) {
if (opts.allow) {
allow = {}
for (const path of opts.allow) {
u.set(allow, toArray(path), true)
}
} else {
allow = null
}
if (opts.deny) {
for (const path of opts.deny) {
u.set(deny, toArray(path), true)
}
} else {
deny = {}
}
return this
}
if (opts) perms(opts)
perms.pre = (name) => {
name = Array.isArray(name) ? name : [name]
if (allow && !u.prefix(allow, name)) {
return new Error(`method:${name} is not in list of allowed methods`)
}
if (deny && u.prefix(deny, name)) {
return new Error(`method:${name} is on list of disallowed methods`)
}
}
perms.post = () => {
// TODO
}
// alias for pre, used in tests.
perms.test = (name) => perms.pre(name)
perms.get = () => ({ allow, deny })
return perms
}