-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbase-controller.ts
144 lines (131 loc) · 4.95 KB
/
base-controller.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
/**
* 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 base-controller.
*
* @module internal/controllers
*/
import express, { Router, RequestHandler } from 'express';
import { SwaggerSpecification } from 'swagger-model-validator';
import Policy, { MethodPolicy } from './policy';
import PolicyMiddleware from '../middleware/policy-middleware';
import RequestValidatorMiddleware from '../middleware/request-validator-middleware';
import RoleManager from '../rbac/role-manager';
import RestrictionMiddleware from '../middleware/restriction-middleware';
/**
* This interface is a wrapper around all the parameters of the BaseController,
* such that changes are easily reflected in all other controllers.
*/
export interface BaseControllerOptions {
specification: SwaggerSpecification,
roleManager: RoleManager,
}
/**
* The BaseController class is responsible for:
* - Storing route definitions.
* - Generating router objects based on the policy.
*/
export default abstract class BaseController {
/**
* The express router used by this controller.
*/
private router: Router;
/**
* A reference to the swagger specification passed in the base controller options.
*/
public specification: SwaggerSpecification;
/**
* A reference to the role manager passed in the base controller options.
*/
protected roleManager: RoleManager;
/**
* Defines a new route on the router. Private helper function to reduce code duplication.
* @param spec
* @param route The route string.
* @param methodPolicy The policy which should be added to the router.
* @param callback The addition function of the appropiate method of the router.
*/
private static defineRoute(
spec: SwaggerSpecification,
route: string,
methodPolicy: MethodPolicy,
callback: (route: string, ...handler: RequestHandler[]) => void,
) {
const handlers: RequestHandler[] = [];
if (methodPolicy.body) {
const validator = new RequestValidatorMiddleware(spec, methodPolicy.body);
handlers.push(validator.getMiddleware());
}
handlers.push(new PolicyMiddleware(methodPolicy.policy).getMiddleware());
handlers.push(new RestrictionMiddleware(() => methodPolicy.restrictions || {}).getMiddleware());
handlers.push(methodPolicy.handler);
callback(
route,
...handlers,
);
}
/**
* Creates a new controller instance, and generates the router based on its defined policy.
* @options - The options from which to extract parameters.
*/
public constructor(options: BaseControllerOptions) {
this.router = express.Router({ strict: true });
this.specification = options.specification;
this.roleManager = options.roleManager;
const spec = options.specification;
// Generate routes based on the policy
const policy = this.getPolicy();
Object.keys(policy).forEach((route: string) => {
const routePolicy = policy[route];
const bind = (f: Function) => f.bind(this.router);
if (routePolicy.GET) {
BaseController.defineRoute(spec, route, routePolicy.GET, bind(this.router.get));
}
if (routePolicy.POST) {
BaseController.defineRoute(spec, route, routePolicy.POST, bind(this.router.post));
}
if (routePolicy.PATCH) {
BaseController.defineRoute(spec, route, routePolicy.PATCH, bind(this.router.patch));
}
if (routePolicy.DELETE) {
BaseController.defineRoute(spec, route, routePolicy.DELETE, bind(this.router.delete));
}
if (routePolicy.PUT) {
BaseController.defineRoute(spec, route, routePolicy.PUT, bind(this.router.put));
}
});
// If the request is not handled by the above handlers, the method is not supported.
Object.keys(policy).forEach((route: string) => {
this.router.use(route, (_req, res) => res.status(405).end('Method not allowed.'));
});
}
/**
* Gets the policy defined by child classes. This policy includes all routes that the controller
* accepts, the authorization middleware, and the final handler function for every route.
* @return The policy of this controller.
*/
public abstract getPolicy(): Policy;
/**
* @return the router used by this controller.
*/
public getRouter(): Router {
return this.router;
}
}