forked from monken/node-pbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditions.js
166 lines (160 loc) · 5 KB
/
conditions.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const ipcheck = require('ipcheck');
const isString = require('lodash/isString'),
isBoolean = require('lodash/isBoolean'),
isNumber = require('lodash/isNumber'),
isArray = require('lodash/isArray'),
isUndefined = require('lodash/isUndefined'),
isEmpty = require('lodash/isEmpty'),
forEach = require('lodash/forEach'),
every = require('lodash/every');
const isBoolish = function(value) {
return isBoolean(value) || value === 'true' || value === 'false';
}
const conditions = {
NumericEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return a === b;
},
NumericNotEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return !this.conditions.NumericEquals.apply(this, arguments);
},
NumericLessThan(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return a < b;
},
NumericGreaterThanEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return !this.conditions.NumericLessThan.apply(this, arguments);
},
NumericGreaterThan(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return a > b;
},
NumericLessThanEquals(a, b) {
if (!isNumber(a) || !isNumber(b)) return false;
return !this.conditions.NumericGreaterThan.apply(this, arguments);
},
DateEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return a >= b && a <= b;
},
DateNotEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return !this.conditions.DateEquals.apply(this, arguments);
},
DateLessThan(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return a < b;
},
DateGreaterThanEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return !this.conditions.DateLessThan.apply(this, arguments);
},
DateGreaterThan(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return a > b;
},
DateLessThanEquals(a, b) {
a = new Date(a), b = new Date(b);
if (a == 'Invalid Date' || b == 'Invalid Date') return false;
return !this.conditions.DateGreaterThan.apply(this, arguments);
},
BinaryEquals(a, b) {
if (!isString(b) || !(a instanceof Buffer)) return false;
return a.equals(Buffer.from(b, 'base64'));
},
BinaryNotEquals(a, b) {
if (!isString(b) || !(a instanceof Buffer)) return false;
return !a.equals(Buffer.from(b, 'base64'));
},
ArnLike: function ArnLike(a, b) {
if (!isString(b)) return false;
return new RegExp('^' +
b.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&")
.replace(/\*/g, '[^:]*') // TODO: check if last part of ARN can contain ':'
.replace(/\?/g, '.') + '$')
.test(a);
},
ArnNotLike: function StringNotLike(a, b) {
if (!isString(b)) return false;
return !this.conditions.ArnLike.apply(this, arguments);
},
ArnEquals: function ArnEquals(a, b) {
return this.conditions.ArnLike(a, b);
},
ArnNotEquals: function ArnEquals(a, b) {
return this.conditions.ArnNotLike(a, b);
},
Null(a, b) {
if (!isBoolean(b)) return false;
return b ? isUndefined(a) : !isUndefined(a);
},
IpAddress(a, b) {
return ipcheck.match(a, b);
},
NotIpAddress() {
return !this.conditions.IpAddress.apply(this, arguments);
},
StringEquals(a, b) {
if (!isString(a) || !isString(b)) return false;
return a === b;
},
StringNotEquals(a, b) {
if (!isString(a) || !isString(b)) return false;
return a !== b;
},
StringEqualsIgnoreCase(a, b) {
if (!isString(a) || !isString(b)) return false;
return a.toLowerCase() === b.toLowerCase();
},
StringNotEqualsIgnoreCase(a, b) {
if (!isString(a) || !isString(b)) return false;
return a.toLowerCase() !== b.toLowerCase();
},
StringLike(a, b) {
if (!isString(b)) return false;
return new RegExp('^' +
b.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&")
.replace(/\*/g, '.*')
.replace(/\?/g, '.') + '$')
.test(a);
},
StringNotLike(a, b) {
if (!isString(b)) return false;
return !this.conditions.StringLike.apply(this, arguments);
},
Bool(a, b) {
if (!isBoolish(a) || !isBoolish(b)) return false;
return a.toString() === b.toString();
},
};
forEach(conditions, function (fn, condition) {
conditions[condition + 'IfExists'] = function (a, b) {
if (isUndefined(a)) return true;
else return fn.apply(this, arguments);
};
conditions['ForAllValues:' + condition] = function (a, b) {
if (!isArray(a)) a = [a];
return every(a, value => {
return b.find(key => {
return fn.call(this, value, key);
});
});
};
conditions['ForAnyValue:' + condition] = function (a, b) {
if (!isArray(a)) a = [a];
return a.find(value => {
return b.find(key => {
return fn.call(this, value, key);
});
});
};
});
module.exports = conditions;