-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
140 lines (122 loc) · 3.68 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var _ = require('lodash'),
async = require('async');
function RBAC(rules, checkFullPath, cacheTrees) {
this.rules = rules;
this.checkFullPath = !!checkFullPath;
this.cacheTrees = !!cacheTrees;
if (this.cacheTrees) {
this.trees = {};
}
}
RBAC.prototype = {
'check': function (role, permission, params, cb) {
var result = false;
// If params not given, consider last argument as callback
if (arguments.length < 4) {
cb = params;
params = {};
}
// Create a rbac tree from the current role
var tree = this.getTree(role);
// Find all paths from root to permission
var paths = findPaths(tree, permission);
// Sort by shortest first (i.e. no. of nodes)
paths = _.sortBy(paths, function (path) {
return path.length;
});
var checkFullPath = this.checkFullPath;
// Check each path serially
async.eachSeries(paths, function (path, cb) {
checkPath(path, 1, params, checkFullPath, function (err, res) {
if (!err && res) {
result = true;
return cb(new BreakError('passed'));
}
cb(err, null);
});
}, function (err) {
if (err && err instanceof BreakError) {
return cb(null, result);
}
cb(err, result);
});
},
'getTree': function (role) {
if (!this.cacheTrees) {
return {
'value' : role,
'children': toTree(role, this.rules)
};
}
if (this.trees[role]) {
return this.trees[role];
}
this.trees[role] = {
'value' : role,
'children': toTree(role, this.rules)
};
return this.trees[role];
}
};
function BreakError(msg) {
Error.apply(this, arguments);
}
BreakError.prototype = new Error();
function toTree(role, rules) {
var self = arguments.callee;
return _.reduce(rules, function (arr, rule) {
if (rule.a === role) {
arr.push({
'value' : rule.can,
'when' : rule.when,
'children': self(rule.can, rules)
});
}
return arr;
}, []);
}
function findPaths(root, permission) {
var self = arguments.callee;
var paths = [];
if (root.value === permission) {
paths.push([root]);
} else {
_.each(root.children, function (child) {
var childpaths = self(child, permission);
_.each(childpaths, function (childpath) {
var path = [root];
path.push.apply(path, childpath);
paths.push(path);
});
});
}
return paths;
}
function checkPath(path, index, params, checkFullPath, cb) {
if (index >= path.length) {
// reached end
return cb(null, true);
}
var self = arguments.callee;
var node = path[index];
if (!node.when) {
if (!checkFullPath || !node.children) {
// no condition to get access to this node,
// permission granted
return cb(null, true);
} else {
return self(path, index + 1, params, checkFullPath, cb);
}
} else {
// test condition associated with current node
node.when(params, function (err, res) {
if (!err && res) {
// reached this node, move on to next
self(path, index + 1, params, checkFullPath, cb);
} else {
return cb(err, false);
}
});
}
}
module.exports = RBAC;