-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrbac-service.ts
266 lines (237 loc) · 8.87 KB
/
rbac-service.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the rbac-service.
*
* @module rbac
*/
import RoleResponse from '../controller/response/rbac/role-response';
import PermissionResponse from '../controller/response/rbac/permission-response';
import ActionResponse from '../controller/response/rbac/action-response';
import RelationResponse from '../controller/response/rbac/relation-response';
import Role from '../entity/rbac/role';
import Permission from '../entity/rbac/permission';
import { ActionDefinition, EntityDefinition, PermissionDefinition } from '../rbac/role-definitions';
import PermissionRule from '../rbac/permission-rule';
import { PaginationParameters } from '../helpers/pagination';
import { DeepPartial, FindManyOptions, FindOptionsRelations } from 'typeorm';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import { UpdateRoleRequest } from '../controller/request/rbac-request';
interface RoleFilterParameters {
roleId?: number;
systemDefault?: boolean;
returnPermissions?: boolean;
}
export default class RBACService {
public static findPermission(permissions: PermissionRule[], toFind: PermissionRule): PermissionRule | undefined {
return permissions.find((p2) => toFind.entity === p2.entity
&& toFind.action === p2.action
&& toFind.relation === p2.relation
&& JSON.stringify(toFind.attributes) === JSON.stringify(p2.attributes));
}
/**
* Map list of permissions to an entity response
* @param permissions
*/
public static asPermissionResponse(permissions: Permission[]): PermissionResponse[] {
const entities = permissions.reduce((e: string[], permission) => {
if (e.includes(permission.entity)) return e;
return [...e, permission.entity];
}, []);
return entities.map((entityName): PermissionResponse => {
const entityPermissions = permissions.filter((p) => p.entity === entityName);
const actions = entityPermissions.reduce((a: string[], permission) => {
if (a.includes(permission.action)) return a;
return [...a, permission.action];
}, []);
return {
entity: entityName,
// Map every action permission to response
actions: actions.map((actionName): ActionResponse => {
const actionPermissions = entityPermissions.filter((p) => p.action === actionName);
const relationPermissions = actionPermissions.reduce((r: Permission[], permission) => {
if (r.some((r2) => r2.relation === permission.relation)) return r;
return [...r, permission];
}, []);
return {
action: actionName,
// Map every relation permission to response
relations: relationPermissions.map((relationPerm): RelationResponse => ({
relation: relationPerm.relation,
attributes: relationPerm.attributes,
})),
};
}),
};
});
}
/**
* Converts a Role object to an RoleResponse, which can be
* returned in the API response. If any permissions are
* present, those are parsed as well.
* @param role - The role definitions to parse
*/
public static asRoleResponse(role: Role): RoleResponse {
return {
id: role.id,
name: role.name,
systemDefault: role.systemDefault,
userTypes: role.userTypes ?? [],
// Map every entity permission to response
permissions: role.permissions ? this.asPermissionResponse(role.permissions) : undefined,
};
}
/**
* Convert a human-readable permission definition to a list of
* database permission rules
* @param def
*/
public static definitionToRules(def: PermissionDefinition): PermissionRule[] {
const permissions: PermissionRule[] = [];
Object.keys(def).forEach(entity => {
Object.keys(def[entity]).forEach((action) => {
Object.keys(def[entity][action]).forEach((relation) => {
const attributes = Array.from(def[entity][action][relation]);
permissions.push({ entity, action, relation, attributes });
});
});
});
return permissions;
}
/**
* Convert a list of database permission rules to a human-readable
* permissions object
* @param rules
*/
public static rulesToDefinition(rules: PermissionRule[]): PermissionDefinition {
return rules.reduce((permDef: PermissionDefinition, permission1, i1, rolePermissions) => {
if (permDef[permission1.entity]) return permDef;
permDef[permission1.entity] = rolePermissions
.filter((p) => p.entity === permission1.entity)
.reduce((entDef: EntityDefinition, permission2, i2, entityPermissions) => {
if (entDef[permission2.entity]) return entDef;
entDef[permission2.action] = entityPermissions
.filter((p) => p.action === permission2.action)
.reduce((actDef: ActionDefinition, permission3) => {
if (actDef[permission3.relation]) return;
actDef[permission3.relation] = new Set(permission3.attributes);
return actDef;
}, {});
return entDef;
}, {});
return permDef;
}, {});
}
/**
* Get a tuple with a list of all roles and the total number of
* roles matching the parameters
* @param params
* @param take
* @param skip
*/
public static async getRoles(params: RoleFilterParameters = {}, { take, skip }: PaginationParameters = {}): Promise<[Role[], number]> {
const options = this.getOptions(params);
return Role.findAndCount({ ...options, take, skip });
}
/**
* Create an new role with the given parameters
* @param params
*/
public static async createRole(params: UpdateRoleRequest) {
return Role.save({ ...params, permissions: [] });
}
/**
* Update an existing role with the given parameters
* @param roleId
* @param params
*/
public static async updateRole(roleId: number, params: UpdateRoleRequest): Promise<Role> {
const role = await Role.findOne({ where: { id: roleId } });
if (role == null) throw new Error('Role not found.');
if (role.systemDefault) throw new Error('Cannot update system default role.');
role.name = params.name;
await role.save();
const [[r]] = await this.getRoles({ roleId, returnPermissions: true });
return r;
}
/**
* Remove an existing role. Cannot delete system default roles
* @param roleId
*/
public static async removeRole(roleId: number) {
const [[role]] = await this.getRoles({ roleId });
if (!role) throw new Error('Role not found.');
if (role.systemDefault) throw new Error('Cannot delete system default role.');
await Role.remove(role);
}
/**
* Add zero or more new permissions
* @param roleId
* @param permissions
*/
public static async addPermissions(roleId: number, permissions: PermissionRule[]) {
const [[role]] = await this.getRoles({ roleId, returnPermissions: true });
if (!role) throw new Error('Role not found.');
return Permission.save(permissions.map((p): DeepPartial<Permission> => ({
...p,
role,
roleId: role.id,
})));
}
/**
* Remove an existing permission
* @param roleId
* @param permissionRule
*/
public static async removePermission(roleId: number, permissionRule: Omit<PermissionRule, 'attributes'>) {
const match = (await Permission.findOne({ where: {
roleId,
entity: permissionRule.entity,
action: permissionRule.action,
relation: permissionRule.relation,
}, relations: { role: true } }));
if (!match) {
throw new Error('Permission not found.');
}
if (match.role.systemDefault) {
throw new Error('Cannot change permissions of system default role.');
}
await Permission.remove(match);
}
/**
* Build findOptions object
* @param params
*/
public static getOptions(params: RoleFilterParameters): FindManyOptions<Role> {
const filterMapping: FilterMapping = {
roleId: 'id',
systemDefault: 'systemDefault',
};
const relations: FindOptionsRelations<Role> = {
permissions: params.returnPermissions,
};
return {
where: {
...QueryFilter.createFilterWhereClause(filterMapping, params),
},
relations,
};
}
}