-
Notifications
You must be signed in to change notification settings - Fork 0
/
canUser.js
40 lines (31 loc) · 878 Bytes
/
canUser.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
const check = (rules, userRoles, actions) => {
// Get user roles
if (!userRoles) {
return false
}
let roles = userRoles
// Handle multiple roles by casting roles into array
roles = roles instanceof Array ? roles : [roles]
let permissions = []
// Combine roles, discard duplicate roles
for (let i = 0; i < Object.keys(roles).length; i++) {
permissions = permissions.concat(
rules[roles[i]].filter((item) => permissions.indexOf(item) < 0),
)
}
if (!permissions) {
return false
}
// Assign all permissions required by the component
if (permissions) {
const actionsList = actions.split(', ')
// Check if the user has all required permissions
for (const action in actionsList) {
if (permissions.includes(actionsList[action])) {
return true
}
}
}
return false
}
module.exports = check