-
Notifications
You must be signed in to change notification settings - Fork 7
/
ActionBasedPolicy.ts
139 lines (129 loc) · 3.97 KB
/
ActionBasedPolicy.ts
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
import {
ActionBasedType,
ConditionResolver,
EvaluateActionBasedInterface,
ProxyOptions
} from './types';
import { ActionBased } from './ActionBasedStatement';
import { Policy } from './Policy';
export interface ActionBasedPolicyInterface<T extends object> {
statements: ActionBasedType[];
conditionResolver?: ConditionResolver;
context?: T;
}
export class ActionBasedPolicy<T extends object> extends Policy<
T,
ActionBasedType
> {
private denyStatements: ActionBased<T>[];
private allowStatements: ActionBased<T>[];
private statements: ActionBasedType[];
constructor({
statements,
conditionResolver,
context
}: ActionBasedPolicyInterface<T>) {
super({ context, conditionResolver });
const statementInstances = statements.map(
(statement) => new ActionBased(statement)
);
this.allowStatements = statementInstances.filter(
(s) => s.effect === 'allow'
);
this.denyStatements = statementInstances.filter((s) => s.effect === 'deny');
this.statements = statementInstances.map((statement) =>
statement.getStatement()
);
}
addStatement(this: ActionBasedPolicy<T>, statement: ActionBasedType): void {
const statementInstance = new ActionBased(statement);
if (statementInstance.effect === 'allow') {
this.allowStatements.push(statementInstance);
} else {
this.denyStatements.push(statementInstance);
}
this.statements.push(statementInstance.getStatement());
}
getStatements(this: ActionBasedPolicy<T>): ActionBasedType[] {
return this.statements;
}
evaluate(
this: ActionBasedPolicy<T>,
{ action, context }: EvaluateActionBasedInterface<T>
): boolean {
const args = { action, context };
return !this.cannot(args) && this.can(args);
}
can(
this: ActionBasedPolicy<T>,
{ action, context }: EvaluateActionBasedInterface<T>
): boolean {
return this.allowStatements.some((s) =>
s.matches({
action,
context: context || this.context,
conditionResolver: this.conditionResolver
})
);
}
cannot(
this: ActionBasedPolicy<T>,
{ action, context }: EvaluateActionBasedInterface<T>
): boolean {
return this.denyStatements.some((s) =>
s.matches({
action,
context: context || this.context,
conditionResolver: this.conditionResolver
})
);
}
generateProxy<U extends object>(
this: ActionBasedPolicy<T>,
obj: U,
options: ProxyOptions = {}
): U {
const { get = {}, set = {} } = options;
const { allow: allowGet = true, propertyMap: propertyMapGet = {} } = get;
const { allow: allowSet = true, propertyMap: propertyMapSet = {} } = set;
const handler = {
...(allowGet
? {
get: (target: U, prop: string | symbol): any => {
const property = Reflect.has(propertyMapGet, prop)
? Reflect.get(propertyMapGet, prop)
: prop;
if (typeof prop === 'string') {
if (this.evaluate({ action: property })) {
return Reflect.get(target, prop);
} else {
throw new Error(`Unauthorize to get ${prop} property`);
}
} else {
return Reflect.get(target, prop);
}
}
}
: {}),
...(allowSet
? {
set: (target: U, prop: string | symbol, value: any): boolean => {
const property = Reflect.has(propertyMapSet, prop)
? Reflect.get(propertyMapSet, prop)
: prop;
if (typeof prop === 'string') {
if (this.evaluate({ action: property })) {
return Reflect.set(target, prop, value);
} else {
throw new Error(`Unauthorize to set ${prop} property`);
}
} else {
return false;
}
}
}
: {})
};
return new Proxy(obj, handler);
}
}