2020-12-02 18:08:25 +01:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const controller = require("../controllers/permission")
|
|
|
|
const authorized = require("../../middleware/authorized")
|
2021-02-05 16:58:25 +01:00
|
|
|
const {
|
|
|
|
BUILDER,
|
|
|
|
PermissionLevels,
|
|
|
|
} = require("../../utilities/security/permissions")
|
|
|
|
const Joi = require("joi")
|
|
|
|
const joiValidator = require("../../middleware/joi-validator")
|
2020-12-02 18:08:25 +01:00
|
|
|
|
|
|
|
const router = Router()
|
|
|
|
|
2021-02-08 18:22:07 +01:00
|
|
|
function generateValidator() {
|
2021-02-05 16:58:25 +01:00
|
|
|
const permLevelArray = Object.values(PermissionLevels)
|
|
|
|
// prettier-ignore
|
2021-02-08 18:22:07 +01:00
|
|
|
return joiValidator.params(Joi.object({
|
|
|
|
level: Joi.string().valid(...permLevelArray).required(),
|
|
|
|
resourceId: Joi.string(),
|
|
|
|
roleId: Joi.string(),
|
2021-02-05 16:58:25 +01:00
|
|
|
}).unknown(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
router
|
|
|
|
.get("/api/permission/builtin", authorized(BUILDER), controller.fetchBuiltin)
|
|
|
|
.get("/api/permission/levels", authorized(BUILDER), controller.fetchLevels)
|
2021-02-08 18:22:07 +01:00
|
|
|
.get(
|
|
|
|
"/api/permission/:resourceId",
|
|
|
|
authorized(BUILDER),
|
|
|
|
controller.getResourcePerms
|
|
|
|
)
|
|
|
|
// adding a specific role/level for the resource overrides the underlying access control
|
2021-02-05 19:46:15 +01:00
|
|
|
.post(
|
2021-02-08 18:22:07 +01:00
|
|
|
"/api/permission/:roleId/:resourceId/:level",
|
2021-02-05 16:58:25 +01:00
|
|
|
authorized(BUILDER),
|
2021-02-08 18:22:07 +01:00
|
|
|
generateValidator(),
|
2021-02-05 16:58:25 +01:00
|
|
|
controller.addPermission
|
|
|
|
)
|
2021-02-08 18:22:07 +01:00
|
|
|
// deleting the level defaults it back the underlying access control for the resource
|
2021-02-05 19:46:15 +01:00
|
|
|
.delete(
|
2021-02-08 18:22:07 +01:00
|
|
|
"/api/permission/:roleId/:resourceId/:level",
|
2021-02-05 16:58:25 +01:00
|
|
|
authorized(BUILDER),
|
2021-02-08 18:22:07 +01:00
|
|
|
generateValidator(),
|
2021-02-05 16:58:25 +01:00
|
|
|
controller.removePermission
|
|
|
|
)
|
2020-12-02 18:08:25 +01:00
|
|
|
|
|
|
|
module.exports = router
|