-
Notifications
You must be signed in to change notification settings - Fork 3
/
sgl.js
96 lines (91 loc) · 3.14 KB
/
sgl.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
function getMatchingMinimalSubsets() {
return [];
}
function checkSatisfies(group, c, disjoint) {
// If the condition calls for us to match by id, do so. Note that we do
// NOT need to also match by other characteristics; although a principal can
// have both an id and roles, condition cannot use both at the same time.
if (c.id) {
for (var i = 0; i < group.length; ++i) {
var p = group[i];
if (p.id == c.id) {
return true;
}
}
}
// If we have to match by role, see if our group includes enough that have the
// required role.
else if (c.roles) {
var n = c.n ? c.n : 1;
for (var i = 0; i < group.length; ++i) {
var p = group[i];
// c.roles is a string; p.roles is an array
if (p.roles && p.roles.indexOf(c.roles) > -1) {
n -= 1;
if (n == 0) {
return true;
}
}
}
}
// If we are looking for a match against any one of several conditions,
// test each condition individually, and return true if we find the right
// number of matches.
else if (c.any) {
var n = c.n ? c.n : 1;
for (var i = 0; i < c.any.length; ++i) {
var condition = c.any[i];
if (checkSatisfies(group, condition, false)) {
n -= 1;
if (n == 0) {
return true;
}
}
}
return false;
}
else if (c.all) {
// If we're doing all (boolean AND) and disjoint subsets, we have to calculate
// the actual subsets of the group that satisfy subsets of the c,
// before we can return True or False.
if (disjoint) {
disjointSubsets = getMatchingMinimalSubsets(group, c);
return !!disjointSubsets;
}
// This is much easier. Just see if all c are satisfied without checking to
// see if the subsets of group that satisfies each are disjoint.
else {
for (var i = 0; i < c.all.length; ++i) {
var condition = c.all[i];
if (!checkSatisfies(group, condition, false)) {
return false;
}
}
return true;
}
}
return false;
}
module.exports = {
satisfies: function(group, condition, disjoint) {
if (typeof group == 'object') {
if (!Array.isArray(group)) {
group = [group];
}
} else {
throw "group must be obj or array";
}
// TODO: eliminate duplicates in group
if (typeof condition == 'object') {
// Did we get a rule instead of a condition?
if (condition.hasOwnProperty('when')) {
condition = condition.when;
}
} else {
throw "condition must be a rule or the .when of a rule"
}
// Now that we've checked all preconditions, call the internal
// function that does all the work and that is recursive.
return checkSatisfies(group, condition, disjoint);
}
}