2020-12-02 14:20:56 +01:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const controller = require("../controllers/role")
|
|
|
|
const authorized = require("../../middleware/authorized")
|
|
|
|
const { BUILDER } = require("../../utilities/security/permissions")
|
2020-12-02 18:08:25 +01:00
|
|
|
const Joi = require("joi")
|
|
|
|
const joiValidator = require("../../middleware/joi-validator")
|
|
|
|
const {
|
|
|
|
BUILTIN_PERMISSION_IDS,
|
|
|
|
} = require("../../utilities/security/permissions")
|
2020-12-02 14:20:56 +01:00
|
|
|
|
|
|
|
const router = Router()
|
|
|
|
|
2020-12-02 18:08:25 +01:00
|
|
|
function generateValidator() {
|
|
|
|
// prettier-ignore
|
|
|
|
return joiValidator.body(Joi.object({
|
|
|
|
_id: Joi.string().optional(),
|
|
|
|
_rev: Joi.string().optional(),
|
|
|
|
name: Joi.string().required(),
|
|
|
|
permissionId: Joi.string().valid(...Object.values(BUILTIN_PERMISSION_IDS)).required(),
|
|
|
|
inherits: Joi.string().optional(),
|
|
|
|
}).unknown(true))
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:20:56 +01:00
|
|
|
router
|
2020-12-02 18:08:25 +01:00
|
|
|
.post("/api/roles", authorized(BUILDER), generateValidator(), controller.save)
|
2020-12-02 14:20:56 +01:00
|
|
|
.get("/api/roles", authorized(BUILDER), controller.fetch)
|
|
|
|
.get("/api/roles/:roleId", authorized(BUILDER), controller.find)
|
|
|
|
.delete("/api/roles/:roleId/:rev", authorized(BUILDER), controller.destroy)
|
|
|
|
|
|
|
|
module.exports = router
|