-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAttributeCondition.js
More file actions
65 lines (60 loc) · 2.5 KB
/
Copy pathAttributeCondition.js
File metadata and controls
65 lines (60 loc) · 2.5 KB
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
/**
* Created by Hao on 2017/3/6.
*/
let Operators = require('./Operators');
class AttributeCondition {
constructor(context, condition) {
this.context = context;
this.logger = this.context.registry.getLogger();
this.attribute = Object.keys(condition)[0];
this.expression = condition[Object.keys(condition)[0]];
this.operators = new Operators();
this.name = "PDP AttrCond";
}
isApplicable(message, expression = this.expression) {
/**
* Verifies if the condition is applicable to the message.
* First, the system value that corresponds to the attribute is retrieved;
* then, that value is compared with the parameter specified in the condition
* by executing the operator implementation.
* @param {Object} message
*/
this.context[this.attribute] = {message: message};
let value = this.context[this.attribute];
let final = false;
if (expression.constructor === Object) {
let results = [];
for (let operator in expression) {
let result = false;
if (!(expression.hasOwnProperty(operator))) continue;
let params = expression[operator];
// if logical operator
if (operator==="not" || operator==="allOf" || operator === "anyOf"){
params = Array.isArray(params)?params:[params];
result = this.operators[operator](params.map(param=>{return this.isApplicable(message, param)}));
}
// otherwise it is comparative operator
// if params is an array
else if (operator !== "in" && params.constructor === Array) {
result = params.some(param => {
return this.operators[operator](value, param, this.attribute);
});
}
// otherwise it is a value
else {
result = this.operators[operator](value, params, this.attribute);
}
results.push(result);
}
final = this.operators.allOf(results);
} else if (expression.constructor === Array) {
final = expression.some(express=>{
return this.isApplicable(message, express);
});
} else {
throw new Error(`Unsupported condition format`);
}
return final;
}
}
module.exports = AttributeCondition;