2018-11-30 10:11:36 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Router } = require('express');
|
2018-12-19 14:50:01 +01:00
|
|
|
const checkPermission = require('../middleware/permission-checker');
|
2018-11-30 10:11:36 +01:00
|
|
|
/**
|
|
|
|
* Base class for Controllers to standardize binding to express Router.
|
|
|
|
*/
|
|
|
|
class Controller {
|
2018-12-19 14:50:01 +01:00
|
|
|
constructor(config) {
|
2018-11-30 10:11:36 +01:00
|
|
|
const router = Router();
|
|
|
|
this.app = router;
|
2018-12-19 14:50:01 +01:00
|
|
|
this.config = config;
|
2018-12-19 13:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get(path, handler, permission) {
|
|
|
|
this.app.get(
|
|
|
|
path,
|
2018-12-19 14:50:01 +01:00
|
|
|
checkPermission(this.config, permission),
|
2018-12-19 13:35:54 +01:00
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
post(path, handler, permission) {
|
2018-12-19 13:35:54 +01:00
|
|
|
this.app.post(
|
|
|
|
path,
|
2018-12-19 14:50:01 +01:00
|
|
|
checkPermission(this.config, permission),
|
2018-12-19 13:35:54 +01:00
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
put(path, handler, permission) {
|
2018-12-19 13:35:54 +01:00
|
|
|
this.app.put(
|
|
|
|
path,
|
2018-12-19 14:50:01 +01:00
|
|
|
checkPermission(this.config, permission),
|
2018-12-19 13:35:54 +01:00
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
delete(path, handler, permission) {
|
2018-12-19 13:35:54 +01:00
|
|
|
this.app.delete(
|
|
|
|
path,
|
2018-12-19 14:50:01 +01:00
|
|
|
checkPermission(this.config, permission),
|
2018-12-19 13:35:54 +01:00
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
use(path, router) {
|
|
|
|
this.app.use(path, router);
|
|
|
|
}
|
|
|
|
|
|
|
|
get router() {
|
2018-11-30 10:11:36 +01:00
|
|
|
return this.app;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Controller;
|