1
0
Fork 0
mirror of synced 2024-06-29 03:20:34 +12:00
budibase/packages/server/middleware/routers.js

404 lines
13 KiB
JavaScript
Raw Normal View History

2019-06-29 09:59:27 +12:00
const Router = require("@koa/router");
2019-06-14 21:05:46 +12:00
const session = require("./session");
const StatusCodes = require("../utilities/statusCodes");
2019-06-29 09:59:27 +12:00
const fs = require("fs");
2019-07-15 17:59:46 +12:00
const { resolve } = require("path");
const send = require('koa-send');
2019-07-27 04:08:59 +12:00
const {
getPackageForBuilder,
getComponents,
2019-07-27 04:08:59 +12:00
savePackage,
getApps,
saveScreen,
renameScreen,
deleteScreen,
2019-09-07 00:04:23 +12:00
componentLibraryInfo
2019-07-27 04:08:59 +12:00
} = require("../utilities/builder");
2019-06-29 09:59:27 +12:00
2019-07-15 17:59:46 +12:00
const builderPath = resolve(__dirname, "../builder");
2019-06-14 21:05:46 +12:00
module.exports = (config, app) => {
2019-06-15 04:01:01 +12:00
const router = new Router();
2019-06-14 21:05:46 +12:00
2019-07-15 17:59:46 +12:00
const prependSlash = path =>
path.startsWith("/")
? path
: `/${path}`;
2019-06-14 21:05:46 +12:00
router
2019-06-15 04:01:01 +12:00
.use(session(config, app))
.use(async (ctx, next) => {
ctx.sessionId = ctx.session._sessCtx.externalKey;
ctx.session.accessed = true;
2019-07-17 09:14:57 +12:00
const pathParts = ctx.path.split("/");
if(pathParts.length < 2) {
ctx.throw(StatusCodes.NOT_FOUND, "App Name not declared");
}
const appname = pathParts[1];
ctx.set("x-bbappname", appname);
2019-07-17 09:14:57 +12:00
if(appname === "_builder") {
await next();
} else {
const instance = await ctx.master.getInstanceApiForSession(
appname,
ctx.sessionId);
ctx.instance = instance.instance;
ctx.publicPath = instance.publicPath;
2019-09-13 02:55:36 +12:00
ctx.sharedPath = instance.sharedPath;
ctx.isAuthenticated = !!instance.instance;
await next();
}
2019-06-15 04:01:01 +12:00
})
2019-07-13 21:35:57 +12:00
.get("/_builder", async (ctx) => {
2019-07-15 17:59:46 +12:00
if(!config.dev) {
ctx.response.status = StatusCodes.FORBIDDEN;
ctx.body = "run in dev mode to access builder";
2019-07-15 17:59:46 +12:00
return;
}
await send(ctx, "/index.html", { root: builderPath });
})
2019-09-09 16:24:14 +12:00
.get("/_builder/:appname/componentlibrary", async (ctx) => {
const info = await componentLibraryInfo(
config,
ctx.params.appname,
ctx.query.lib);
await send(ctx, info.components._lib || "index.js", { root: info.libDir});
})
2019-10-07 18:03:41 +13:00
.get("/_builder/:appname/componentLibraryGenerators", async (ctx) => {
const info = await componentLibraryInfo(
config,
ctx.params.appname,
ctx.query.lib);
await send(ctx, info.generators._lib || "generators.js", { root: info.libDir});
})
2019-07-15 17:59:46 +12:00
.get("/_builder/*", async (ctx, next) => {
if(!config.dev) {
ctx.response.status = StatusCodes.FORBIDDEN;
ctx.body = "run in dev mode to access builder";
2019-07-15 17:59:46 +12:00
return;
}
const path = ctx.path.replace("/_builder", "");
if(path.startsWith("/api/")) {
await next();
2019-07-17 09:14:57 +12:00
} else {
await send(ctx, path, { root: builderPath });
2019-07-15 17:59:46 +12:00
}
2019-07-13 21:35:57 +12:00
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/authenticate", async (ctx, next) => {
2019-06-14 21:05:46 +12:00
const user = await ctx.master.authenticate(
2019-06-15 04:01:01 +12:00
ctx.sessionId,
2019-06-14 21:05:46 +12:00
ctx.params.appname,
ctx.request.body.username,
ctx.request.body.password
);
if(!user) {
ctx.throw(StatusCodes.UNAUTHORIZED, "invalid username or password");
}
2019-10-19 05:32:03 +13:00
ctx.body = user.user_json;
2019-06-15 04:01:01 +12:00
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/setPasswordFromTemporaryCode", async (ctx) => {
const instanceApi = await ctx.master.getFullAccessInstanceApiForUsername(
ctx.params.appname,
ctx.request.body.username
);
2019-07-07 20:03:37 +12:00
if(!instanceApi) {
ctx.request.status = StatusCodes.OK;
return;
}
await instanceApi.authApi.setPasswordFromTemporaryCode(
ctx.request.body.tempCode,
2019-06-27 09:29:28 +12:00
ctx.request.body.newPassword);
ctx.response.status = StatusCodes.OK;
2019-06-16 10:55:32 +12:00
})
.post("/:appname/api/createTemporaryAccess", async (ctx) => {
const instanceApi = await ctx.master.getFullAccessInstanceApiForUsername(
ctx.params.appname,
ctx.request.body.username
);
2019-07-07 20:03:37 +12:00
if(!instanceApi) {
ctx.request.status = StatusCodes.OK;
return;
}
await instanceApi.authApi.createTemporaryAccess(
ctx.request.body.username);
2019-06-16 10:55:32 +12:00
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
.get("/_builder/api/apps", async (ctx) => {
2019-10-12 05:14:23 +13:00
ctx.body = await getApps(config, ctx.master);
ctx.response.status = StatusCodes.OK;
})
2019-07-13 21:35:57 +12:00
.get("/_builder/api/:appname/appPackage", async (ctx) => {
ctx.body = await getPackageForBuilder(
config,
ctx.params.appname);
ctx.response.status = StatusCodes.OK;
})
.post("/_builder/api/:appname/appPackage", async (ctx) => {
2019-07-27 04:08:59 +12:00
2019-07-13 21:35:57 +12:00
ctx.body = await savePackage(
config,
ctx.params.appname,
ctx.request.body);
ctx.response.status = StatusCodes.OK;
2019-07-13 21:35:57 +12:00
})
.get("/_builder/api/:appname/components", async (ctx) => {
try {
ctx.body = getComponents(
config,
ctx.params.appname,
ctx.query.lib);
ctx.response.status = StatusCodes.OK;
} catch(e) {
if(e.status) {
ctx.response.status = e.status;
} else {
throw e;
}
}
})
2019-09-09 16:24:14 +12:00
.get("/_builder/api/:appname/componentlibrary", async (ctx) => {
2019-09-07 00:04:23 +12:00
const info = await componentLibraryInfo(
2019-08-20 08:18:23 +12:00
config,
ctx.params.appname,
2019-09-09 16:24:14 +12:00
ctx.query.lib ? decodeURI(ctx.query.lib) : "");
ctx.body = info.components;
ctx.response.status = StatusCodes.OK;
2019-08-20 08:18:23 +12:00
})
2019-10-07 18:03:41 +13:00
.get("/_builder/api/:appname/generators", async (ctx) => {
const info = await componentLibraryInfo(
config,
ctx.params.appname,
ctx.query.lib ? decodeURI(ctx.query.lib) : "");
ctx.body = info.generators;
ctx.response.status = StatusCodes.OK;
})
.post("/_builder/api/:appname/screen", async (ctx) => {
await saveScreen(
2019-07-27 04:08:59 +12:00
config,
ctx.params.appname,
ctx.request.body);
ctx.response.status = StatusCodes.OK;
})
.patch("/_builder/api/:appname/screen", async (ctx) => {
await renameScreen(
2019-07-27 04:08:59 +12:00
config,
ctx.params.appname,
ctx.request.body.oldname,
ctx.request.body.newname);
ctx.response.status = StatusCodes.OK;
})
.delete("/_builder/api/:appname/screen/*", async (ctx) => {
2019-07-27 18:43:03 +12:00
const name = ctx.request.path.replace(
`/_builder/api/${ctx.params.appname}/screen/`, "");
2019-07-27 18:43:03 +12:00
await deleteScreen(
2019-07-27 04:08:59 +12:00
config,
ctx.params.appname,
2019-09-09 16:24:14 +12:00
decodeURI(name));
2019-07-27 04:08:59 +12:00
ctx.response.status = StatusCodes.OK;
})
2019-07-17 09:14:57 +12:00
.get("/:appname", async (ctx) => {
await send(ctx, "/index.html", { root: ctx.publicPath });
})
.get("/:appname/*", async (ctx, next) => {
const path = ctx.path.replace(`/${ctx.params.appname}`, "");
2019-06-15 04:01:01 +12:00
2019-07-17 09:14:57 +12:00
if(path.startsWith("/api/")) {
await next();
2019-09-13 02:55:36 +12:00
} else if(path.startsWith("/_shared/")) {
await send(
ctx,
path.replace(`/_shared/`, ""),
{ root: ctx.sharedPath });
}
else {
2019-07-17 09:14:57 +12:00
await send(ctx, path, { root: ctx.publicPath });
}
})
.use(async (ctx, next) => {
if(ctx.isAuthenticated) {
2019-06-22 01:00:24 +12:00
await next();
2019-07-17 09:14:57 +12:00
} else {
ctx.response.status = StatusCodes.UNAUTHORIZED;
2019-06-22 01:00:24 +12:00
}
2019-06-14 21:05:46 +12:00
})
2019-06-16 10:55:32 +12:00
.post("/:appname/api/changeMyPassword", async (ctx) => {
await ctx.instance.authApi.changeMyPassword(
ctx.request.body.currentPassword,
ctx.request.body.newPassword
);
ctx.response.status = StatusCodes.OK;
})
.post("/:appname/api/changeMyPassword", async (ctx) => {
await ctx.instance.authApi.changeMyPassword(
ctx.request.body.currentPassword,
ctx.request.body.newPassword
);
ctx.response.status = StatusCodes.OK;
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/executeAction/:actionname", async (ctx) => {
2019-06-16 10:55:32 +12:00
ctx.body = await ctx.instance.actionApi.execute(
ctx.request.body.actionname,
ctx.request.body.parameters);
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/createUser", async (ctx) => {
await ctx.instance.authApi.createUser(
ctx.request.body.user,
ctx.request.body.password
);
2019-06-14 21:05:46 +12:00
2019-06-15 04:01:01 +12:00
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/enableUser", async (ctx) => {
2019-06-16 10:55:32 +12:00
await ctx.instance.authApi.enableUser(
ctx.request.body.username);
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/disableUser", async (ctx) => {
2019-06-16 10:55:32 +12:00
await ctx.instance.authApi.disableUser(
ctx.request.body.username);
2019-06-20 09:05:53 +12:00
await ctx.master.removeSessionsForUser(
ctx.params.appname,
ctx.request.body.username
);
2019-06-16 10:55:32 +12:00
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-15 04:01:01 +12:00
.get("/:appname/api/users", async (ctx) => {
2019-06-15 10:03:01 +12:00
ctx.body = await ctx.instance.authApi.getUsers();
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-15 04:01:01 +12:00
.get("/:appname/api/accessLevels", async (ctx) => {
2019-06-16 10:55:32 +12:00
ctx.body = await ctx.instance.authApi.getAccessLevels();
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-10-03 18:12:13 +13:00
.get("/:appname/api/listRecords/:indexkey", async (ctx) => {
ctx.body = await ctx.instance.indexApi.listItems(
ctx.params.indexkey);
ctx.response.status = StatusCodes.OK;
})
2019-06-15 04:01:01 +12:00
.post("/:appname/api/listRecords/:indexkey", async (ctx) => {
2019-06-16 10:55:32 +12:00
ctx.body = await ctx.instance.indexApi.listItems(
ctx.request.body.indexKey,
{
rangeStartParams:ctx.request.body.rangeStartParams,
rangeEndParams:ctx.request.body.rangeEndParams,
searchPhrase:ctx.request.body.searchPhrase
}
);
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-16 10:55:32 +12:00
.post("/:appname/api/aggregates/:indexkey", async (ctx) => {
ctx.body = await ctx.instance.indexApi.aggregates(
ctx.request.body.indexKey,
{
rangeStartParams:ctx.request.body.rangeStartParams,
rangeEndParams:ctx.request.body.rangeEndParams,
searchPhrase:ctx.request.body.searchPhrase
}
);
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-29 09:59:27 +12:00
.post("/:appname/api/files/*", async (ctx) => {
const file = ctx.request.files.file;
ctx.body = await ctx.instance.recordApi.uploadFile(
getRecordKey(ctx.params.appname, ctx.request.path),
fs.createReadStream(file.path),
file.name
);
ctx.response.status = StatusCodes.OK;
})
.post("/:appname/api/record/*", async (ctx) => {
2019-06-16 10:55:32 +12:00
ctx.body = await ctx.instance.recordApi.save(
ctx.request.body
);
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
.get("/:appname/api/lookup_field/*", async (ctx) => {
const recordKey = getRecordKey(ctx.params.appname, ctx.request.path)
const fields = ctx.query.fields.split(",")
const recordContext = await ctx.instance.recordApi.getContext(
recordKey
);
const allContext = [];
for(let field of fields) {
allContext.push(
await recordContext.referenceOptions(field)
);
}
ctx.body = allContext;
ctx.response.status = StatusCodes.OK;
})
2019-06-29 09:59:27 +12:00
.get("/:appname/api/record/*", async (ctx) => {
2019-07-11 20:43:47 +12:00
try {
ctx.body = await ctx.instance.recordApi.load(
getRecordKey(ctx.params.appname, ctx.request.path)
);
ctx.response.status = StatusCodes.OK;
} catch(e) {
// need to be catching for 404s here
ctx.response.status = StatusCodes.INTERAL_ERROR;
ctx.response.body = e.message;
}
2019-06-14 21:05:46 +12:00
})
2019-06-29 09:59:27 +12:00
.del("/:appname/api/record/*", async (ctx) => {
2019-06-16 10:55:32 +12:00
await ctx.instance.recordApi.delete(
2019-06-29 09:59:27 +12:00
getRecordKey(ctx.params.appname, ctx.request.path)
2019-06-16 10:55:32 +12:00
);
ctx.response.status = StatusCodes.OK;
2019-06-14 21:05:46 +12:00
})
2019-06-26 09:48:22 +12:00
.post("/:appname/api/apphierarchy", async (ctx) => {
ctx.body = await ctx.instance.templateApi.saveApplicationHierarchy(
2019-06-16 10:55:32 +12:00
ctx.body
);
ctx.response.status = StatusCodes.OK;
2019-09-13 02:55:36 +12:00
});
/*.post("/:appname/api/actionsAndTriggers", async (ctx) => {
2019-06-26 09:48:22 +12:00
ctx.body = await ctx.instance.templateApi.saveApplicationHierarchy(
2019-06-16 10:55:32 +12:00
ctx.body
);
ctx.response.status = StatusCodes.OK;
2019-06-15 04:01:01 +12:00
})
2019-06-16 10:55:32 +12:00
.get("/:appname/api/appDefinition", async (ctx) => {
ctx.body = await ctx.instance.templateApi.saveActionsAndTriggers(
ctx.body
);
ctx.response.status = StatusCodes.OK;
2019-09-13 02:55:36 +12:00
})*/
2019-06-14 21:05:46 +12:00
2019-06-29 09:59:27 +12:00
const getRecordKey = (appname, wholePath) =>
wholePath
.replace(`/${appname}/api/files/`, "")
.replace(`/${appname}/api/lookup_field/`, "")
.replace(`/${appname}/api/record/`, "");
2019-06-29 09:59:27 +12:00
2019-06-14 21:05:46 +12:00
return router;
}
/*
front end get authenticateTemporaryAccess {}
*/