-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrbac-service.ts
57 lines (56 loc) · 2.34 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
/**
* SudoSOS back-end API service.
* Copyright (C) 2020 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/>.
*/
import RoleResponse from '../controller/response/rbac/role-response';
import { RoleDefinitions } from '../rbac/role-manager';
import EntityResponse from '../controller/response/rbac/entity-response';
import ActionResponse from '../controller/response/rbac/action-response';
import RelationResponse from '../controller/response/rbac/relation-response';
export default class RBACService {
/**
* Converts the RoleDefinitions object to an Roleresponse, which can be
* returned in the API response.
* @param definitions - The definitions to parse
*/
public static asRoleResponse(definitions: RoleDefinitions): RoleResponse[] {
return Object.keys(definitions).map((roleName): RoleResponse => {
const role = definitions[roleName];
return {
role: roleName,
// Map every entity permission to response
entities: Object.keys(role.permissions).map((entityName): EntityResponse => {
const entity = role.permissions[entityName];
return {
entity: entityName,
// Map every action permission to response
actions: Object.keys(entity).map((actionName): ActionResponse => {
const action = entity[actionName];
return {
action: actionName,
// Map every relation permission to response
relations: Object.keys(action).map((relationName): RelationResponse => ({
relation: relationName,
attributes: [...action[relationName]],
})),
};
}),
};
}),
};
});
}
}