-
Notifications
You must be signed in to change notification settings - Fork 4
/
process.js
65 lines (58 loc) · 1.45 KB
/
process.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
(function() {
"use strict";
var process = function(permdata) {
function to_kv(arr) {
var obj = {};
arr.forEach(function(item) {
var key = item.replace(/-/g, '_');
obj[key] = item;
});
return obj;
};
var roles = to_kv(Object.keys(permdata.roles));
var permissions = to_kv(permdata.permissions);
var desc = {};
Object.keys(roles).forEach(function(k) {
desc[k] = permdata.roles[k].desc;
});
var role_permissions = {};
var copy = {};
Object.keys(permdata.role_permissions).forEach(function(role) {
var spec = permdata.role_permissions[role];
if(!role_permissions[role]) role_permissions[role] = [];
var perms = role_permissions[role];
if(spec.copy) {
// handle copies later
copy[role] = spec;
return;
}
if(spec.all_but) {
permdata.permissions.forEach(function(role) {
if(spec.all_but.indexOf(role) >= 0) return;
perms.push(role);
});
}
(spec.perms || []).forEach(function(perm) {
perms.push(perm);
});
});
Object.keys(copy).forEach(function(role) {
var spec = copy[role];
var perms = role_permissions[role];
if(!spec.copy) return;
role_permissions[spec.copy].forEach(function(perm) {
perms.push(perm);
});
(spec.perms || []).forEach(function(perm) {
perms.push(perm);
});
});
return {
roles: roles,
permissions: permissions,
role_permissions: role_permissions,
desc: desc,
};
};
this.Permissions = {process: process};
}).call(typeof(exports) === 'undefined' ? window : exports);