2018-11-30 10:11:36 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Router } = require('express');
|
2018-12-19 13:17:44 +01:00
|
|
|
const { requirePermission } = require('./../permissions');
|
2018-11-30 10:11:36 +01:00
|
|
|
/**
|
|
|
|
* Base class for Controllers to standardize binding to express Router.
|
|
|
|
*/
|
|
|
|
class Controller {
|
2018-12-19 10:36:56 +01:00
|
|
|
constructor(extendedPerms) {
|
2018-11-30 10:11:36 +01:00
|
|
|
const router = Router();
|
|
|
|
this.app = router;
|
2018-12-19 10:36:56 +01:00
|
|
|
this.extendedPerms = extendedPerms;
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
get(path, handler, permission) {
|
|
|
|
if (this.extendedPerms && permission) {
|
|
|
|
this.app.get(
|
|
|
|
path,
|
|
|
|
requirePermission(permission),
|
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-12-19 10:36:56 +01:00
|
|
|
}
|
2018-11-30 10:11:36 +01:00
|
|
|
this.app.get(path, handler.bind(this));
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
post(path, handler, permission) {
|
|
|
|
if (this.extendedPerms && permission) {
|
|
|
|
this.app.post(
|
|
|
|
path,
|
|
|
|
requirePermission(permission),
|
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-12-19 10:36:56 +01:00
|
|
|
}
|
2018-11-30 10:11:36 +01:00
|
|
|
this.app.post(path, handler.bind(this));
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
put(path, handler, permission) {
|
|
|
|
if (this.extendedPerms && permission) {
|
|
|
|
this.app.put(
|
|
|
|
path,
|
|
|
|
requirePermission(permission),
|
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-12-19 10:36:56 +01:00
|
|
|
}
|
2018-11-30 11:11:12 +01:00
|
|
|
this.app.put(path, handler.bind(this));
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:17:44 +01:00
|
|
|
delete(path, handler, permission) {
|
|
|
|
if (this.extendedPerms && permission) {
|
|
|
|
this.app.delete(
|
|
|
|
path,
|
|
|
|
requirePermission(permission),
|
|
|
|
handler.bind(this)
|
|
|
|
);
|
2018-12-19 10:36:56 +01:00
|
|
|
}
|
2018-11-30 11:11:12 +01:00
|
|
|
this.app.delete(path, handler.bind(this));
|
|
|
|
}
|
|
|
|
|
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;
|