Skip to content
This repository was archived by the owner on Jul 5, 2022. It is now read-only.

Commit 19402cb

Browse files
committed
feat: add resource privilege check module
1 parent 3cfa5d8 commit 19402cb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/resource.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
PRIV_READ,
3+
WILDCARD,
4+
ALLOW,
5+
DENY
6+
} from './constants'
7+
8+
export default function(acl, role, privilege = PRIV_READ) {
9+
const {resource} = acl
10+
11+
if (!resource) {
12+
return false
13+
}
14+
15+
if (!resource[ALLOW]) {
16+
return false
17+
}
18+
19+
let isAllowed = false
20+
21+
// wildcard allow
22+
if (resource[ALLOW] && resource[ALLOW][WILDCARD]) {
23+
if (resource[ALLOW][WILDCARD].includes(privilege)) {
24+
isAllowed = true
25+
}
26+
}
27+
28+
// allow
29+
if (resource[ALLOW] && resource[ALLOW][role.name]) {
30+
if (resource[ALLOW][role.name].includes(privilege)) {
31+
isAllowed = true
32+
}
33+
}
34+
35+
// wildcard deny
36+
if (resource[DENY] && resource[DENY][WILDCARD]) {
37+
if (resource[DENY][WILDCARD].includes(privilege)) {
38+
isAllowed = false
39+
}
40+
}
41+
42+
// deny
43+
if (resource[DENY] && resource[DENY][role.name]) {
44+
if (resource[DENY][role.name].includes(privilege)) {
45+
isAllowed = false
46+
}
47+
}
48+
49+
return isAllowed
50+
}

0 commit comments

Comments
 (0)