/
Graph.ts
246 lines (220 loc) · 7.77 KB
/
Graph.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { Node } from './Node';
import { Subject } from './Subject';
import { Resource } from './Resource';
import { Repository } from '../interfaces';
import { topologicalSort } from '../util';
export interface SchemaNode {
name: string;
repository: Repository;
id?: string;
parent?: string;
parentId?: string;
permissionProperty?: string;
getParents?: typeof Node.prototype.getParents;
getPermission?: typeof Resource.prototype.getPermission;
}
export type Schema = {
resources: SchemaNode[];
subjects: SchemaNode[];
};
/**
Class for building Resource/Subject class hierarchy based on schema
*/
export class Graph {
/**
* Build map of nodeName -> ResourceClass for gracl hierarchy
NOTE: unfortunately we need to duplicate this function for subjects
due to limitations of the way type parameters are handled
See: https://github.com/Microsoft/TypeScript/issues/4890
*/
static buildResourceHierachy(
schemaNodes: SchemaNode[]
): Map<string, typeof Resource> {
const nodeList = topologicalSort(schemaNodes),
classGraph = new Map<string, typeof Resource>();
const createClass = (node: SchemaNode) => {
const getParentsMethod = node.getParents || Node.prototype.getParents;
const getPermissionMethod =
node.getPermission || Resource.prototype.getPermission;
if (node.parent) {
const ParentClass = classGraph.get(node.parent)!;
classGraph.set(
node.name,
class extends ParentClass {
static id = node.id || 'id';
static parentId = node.parentId!;
static displayName = node.name;
static repository = node.repository;
static permissionPropertyKey = node.permissionProperty ||
'permissions';
getParents() {
return getParentsMethod.call(this);
}
getPermission(subject: Subject) {
return getPermissionMethod.call(this, subject);
}
}
);
} else {
classGraph.set(
node.name,
class extends Resource {
static id = node.id || 'id';
static displayName = node.name;
static repository = node.repository;
static permissionPropertyKey = node.permissionProperty ||
'permissions';
getPermission(subject: Subject) {
return getPermissionMethod.call(this, subject);
}
}
);
}
};
nodeList.forEach(createClass);
return classGraph;
}
/**
* Build map of nodeName -> SubjectClass for gracl hierarchy
NOTE: this is a duplicate of buildResourceHierachy that
is necessary for reasons explained above due to limitations
in the type system.
*/
static buildSubjectHierachy(
schemaNodes: SchemaNode[]
): Map<string, typeof Subject> {
const nodeList = topologicalSort(schemaNodes),
classGraph = new Map<string, typeof Subject>();
const createClass = (node: SchemaNode) => {
const getParentsMethod = node.getParents || Node.prototype.getParents;
if (node.parent) {
const ParentClass = classGraph.get(node.parent)!;
classGraph.set(
node.name,
class extends ParentClass {
static id = node.id || 'id';
static parentId = node.parentId!;
static displayName = node.name;
static repository = node.repository;
static permissionPropertyKey = node.permissionProperty ||
'permissions';
getParents() {
return getParentsMethod.call(this);
}
}
);
} else {
classGraph.set(
node.name,
class extends Subject {
static id = node.id || 'id';
static displayName = node.name;
static repository = node.repository;
static permissionPropertyKey = node.permissionProperty ||
'permissions';
}
);
}
};
nodeList.forEach(createClass);
return classGraph;
}
static resolveNodeName(node: string | typeof Node): string {
return typeof node === 'string' ? node : node.displayName;
}
/**
Properties to contain generated classes
*/
resources: Map<string, typeof Resource>;
subjects: Map<string, typeof Subject>;
subjectChildMap = new Map<string, Map<string, typeof Subject>>();
resourceChildMap = new Map<string, Map<string, typeof Resource>>();
constructor(public schema: Schema) {
this.resources = Graph.buildResourceHierachy(schema.resources);
this.subjects = Graph.buildSubjectHierachy(schema.subjects);
// invert resource graph
this.resources.forEach((ChildResource, childName) => {
const parentResources = ChildResource.getHierarchyClassNames();
parentResources.forEach(parentName => {
if (!this.resourceChildMap.has(parentName))
this.resourceChildMap.set(parentName, new Map());
if (parentName !== childName)
this.resourceChildMap.get(parentName)!.set(childName, ChildResource);
});
});
// invert subject graph
this.subjects.forEach((ChildSubject, childName) => {
const parentSubjects = ChildSubject.getHierarchyClassNames();
parentSubjects.forEach(parentName => {
if (!this.subjectChildMap.has(parentName))
this.subjectChildMap.set(parentName, new Map());
if (parentName !== childName)
this.subjectChildMap.get(parentName)!.set(childName, ChildSubject);
});
});
}
/**
* Extract a node class from the graph if it exists
*/
getClass(
node: string | typeof Node,
type: 'subject' | 'resource'
): typeof Resource | typeof Subject {
const name = Graph.resolveNodeName(node);
let map: Map<string, typeof Resource | typeof Subject>;
switch (type) {
case 'subject':
map = this.subjects;
break;
case 'resource':
map = this.resources;
break;
default:
throw new Error(
`Invalid class type ${type}, must be subject or resource!`
);
}
if (map.has(name)) return map.get(name)!;
throw new Error(`No ${type} class found for ${name}!`);
}
/**
* Extract a resource class from the graph if it exists
*/
getResource(node: string | typeof Node): typeof Resource {
const name = Graph.resolveNodeName(node);
return <typeof Resource>this.getClass(name, 'resource');
}
/**
* Extract a subject class from the graph if it exists
*/
getSubject(node: string | typeof Node): typeof Subject {
const name = Graph.resolveNodeName(node);
return <typeof Subject>this.getClass(name, 'subject');
}
getChildResources(node: string | typeof Node): Array<typeof Resource> {
const name = Graph.resolveNodeName(node);
if (!this.resources.has(name))
throw new Error(`No resource class found for ${name}!`);
return Array.from(this.resourceChildMap.get(name)!.values());
}
getChildSubjects(node: string | typeof Node): Array<typeof Subject> {
const name = Graph.resolveNodeName(node);
if (!this.subjects.has(name))
throw new Error(`No subject class found for ${name}!`);
return Array.from(this.subjectChildMap.get(name)!.values());
}
getParentResources(node: string | typeof Node): Array<typeof Resource> {
const name = Graph.resolveNodeName(node);
const ResourceClass = this.getResource(name);
return ResourceClass.getHierarchyClassNames()
.filter(n => n !== name)
.map(n => this.getResource(n));
}
getParentSubjects(node: string | typeof Node): Array<typeof Subject> {
const name = Graph.resolveNodeName(node);
const SubjectClass = this.getSubject(name);
return SubjectClass.getHierarchyClassNames()
.filter(n => n !== name)
.map(n => this.getSubject(n));
}
}