2019-06-28 23:59:27 +02:00
|
|
|
const Router = require("@koa/router");
|
2019-06-14 11:05:46 +02:00
|
|
|
const session = require("./session");
|
|
|
|
const StatusCodes = require("../utilities/statusCodes");
|
2019-06-28 23:59:27 +02:00
|
|
|
const fs = require("fs");
|
2019-07-15 07:59:46 +02:00
|
|
|
const { resolve } = require("path");
|
|
|
|
const send = require('koa-send');
|
2019-07-14 08:46:36 +02:00
|
|
|
const { getPackageForBuilder,
|
|
|
|
savePackage, getApps } = require("../utilities/builder");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2019-07-15 07:59:46 +02:00
|
|
|
const builderPath = resolve(__dirname, "../builder");
|
|
|
|
|
2019-06-14 11:05:46 +02:00
|
|
|
module.exports = (config, app) => {
|
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
const router = new Router();
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2019-07-15 07:59:46 +02:00
|
|
|
const prependSlash = path =>
|
|
|
|
path.startsWith("/")
|
|
|
|
? path
|
|
|
|
: `/${path}`;
|
|
|
|
|
2019-06-14 11:05:46 +02:00
|
|
|
router
|
2019-06-14 18:01:01 +02:00
|
|
|
.use(session(config, app))
|
|
|
|
.use(async (ctx, next) => {
|
|
|
|
ctx.sessionId = ctx.session._sessCtx.externalKey;
|
|
|
|
ctx.session.accessed = true;
|
|
|
|
await next();
|
|
|
|
})
|
2019-07-13 11:35:57 +02:00
|
|
|
.get("/_builder", async (ctx) => {
|
2019-07-15 07:59:46 +02:00
|
|
|
if(!config.dev) {
|
|
|
|
ctx.request.status = StatusCodes.FORBIDDEN;
|
|
|
|
ctx.request.body = "run in dev mode to access builder";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await send(ctx, "/index.html", { root: builderPath });
|
|
|
|
|
|
|
|
})
|
|
|
|
.get("/_builder/*", async (ctx, next) => {
|
|
|
|
if(!config.dev) {
|
|
|
|
ctx.request.status = StatusCodes.FORBIDDEN;
|
|
|
|
ctx.request.body = "run in dev mode to access builder";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = ctx.path.replace("/_builder", "");
|
|
|
|
|
|
|
|
if(path.startsWith("/api/")) {
|
|
|
|
await next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await send(ctx, path, { root: builderPath });
|
|
|
|
|
2019-07-13 11:35:57 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.get("/:appname", async (ctx) => {
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
|
|
|
ctx.response.body = "UI Served Here";
|
|
|
|
})
|
|
|
|
.post("/:appname/api/authenticate", async (ctx, next) => {
|
2019-06-14 11:05:46 +02:00
|
|
|
const user = await ctx.master.authenticate(
|
2019-06-14 18:01:01 +02:00
|
|
|
ctx.sessionId,
|
2019-06-14 11:05:46 +02:00
|
|
|
ctx.params.appname,
|
|
|
|
ctx.request.body.username,
|
|
|
|
ctx.request.body.password
|
|
|
|
);
|
|
|
|
if(!user) {
|
|
|
|
ctx.throw(StatusCodes.UNAUTHORIZED, "invalid username or password");
|
|
|
|
}
|
2019-06-14 18:01:01 +02:00
|
|
|
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/setPasswordFromTemporaryCode", async (ctx) => {
|
2019-06-16 23:17:22 +02:00
|
|
|
const instanceApi = await ctx.master.getFullAccessInstanceApiForUsername(
|
|
|
|
ctx.params.appname,
|
|
|
|
ctx.request.body.username
|
|
|
|
);
|
|
|
|
|
2019-07-07 10:03:37 +02:00
|
|
|
if(!instanceApi) {
|
|
|
|
ctx.request.status = StatusCodes.OK;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-16 23:17:22 +02:00
|
|
|
await instanceApi.authApi.setPasswordFromTemporaryCode(
|
|
|
|
ctx.request.body.tempCode,
|
2019-06-26 23:29:28 +02:00
|
|
|
ctx.request.body.newPassword);
|
2019-06-16 23:17:22 +02:00
|
|
|
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-16 00:55:32 +02:00
|
|
|
})
|
|
|
|
.post("/:appname/api/createTemporaryAccess", async (ctx) => {
|
2019-06-16 23:17:22 +02:00
|
|
|
const instanceApi = await ctx.master.getFullAccessInstanceApiForUsername(
|
|
|
|
ctx.params.appname,
|
|
|
|
ctx.request.body.username
|
|
|
|
);
|
|
|
|
|
2019-07-07 10:03:37 +02:00
|
|
|
if(!instanceApi) {
|
|
|
|
ctx.request.status = StatusCodes.OK;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-16 23:17:22 +02:00
|
|
|
await instanceApi.authApi.createTemporaryAccess(
|
|
|
|
ctx.request.body.username);
|
2019-06-16 00:55:32 +02:00
|
|
|
|
2019-06-16 23:17:22 +02:00
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-07-14 08:46:36 +02:00
|
|
|
.get("/_builder/api/apps", async (ctx) => {
|
|
|
|
if(!config.dev) {
|
|
|
|
ctx.request.status = StatusCodes.FORBIDDEN;
|
|
|
|
ctx.request.body = "run in dev mode to access builder";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = await getApps(config);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
|
|
|
|
|
|
|
})
|
2019-07-13 11:35:57 +02:00
|
|
|
.get("/_builder/api/:appname/appPackage", async (ctx) => {
|
|
|
|
if(!config.dev) {
|
|
|
|
ctx.request.status = StatusCodes.FORBIDDEN;
|
|
|
|
ctx.request.body = "run in dev mode to access builder";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = await getPackageForBuilder(
|
|
|
|
config,
|
|
|
|
ctx.params.appname);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
|
|
|
|
|
|
|
})
|
|
|
|
.post("/_builder/api/:appname/appPackage", async (ctx) => {
|
|
|
|
if(!config.dev) {
|
|
|
|
ctx.request.status = StatusCodes.FORBIDDEN;
|
|
|
|
ctx.body = "run in dev mode to access builder";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = await savePackage(
|
|
|
|
config,
|
|
|
|
ctx.params.appname,
|
|
|
|
ctx.request.body);
|
2019-07-14 08:46:36 +02:00
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-07-13 11:35:57 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.use(async (ctx, next) => {
|
|
|
|
|
|
|
|
const pathParts = ctx.path.split("/");
|
|
|
|
|
|
|
|
if(pathParts.length < 2) {
|
|
|
|
ctx.throw(StatusCodes.NOT_FOUND, "App Name not declared");
|
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
ctx.instance = await ctx.master.getInstanceApiForSession(
|
|
|
|
pathParts[1],
|
|
|
|
ctx.sessionId);
|
|
|
|
|
2019-06-21 15:00:24 +02:00
|
|
|
if(ctx.instance === null) {
|
|
|
|
ctx.response.status = StatusCodes.UNAUTHORIZED;
|
|
|
|
} else {
|
|
|
|
await next();
|
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-16 00:55:32 +02: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-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/executeAction/:actionname", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.actionApi.execute(
|
|
|
|
ctx.request.body.actionname,
|
|
|
|
ctx.request.body.parameters);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/createUser", async (ctx) => {
|
|
|
|
await ctx.instance.authApi.createUser(
|
|
|
|
ctx.request.body.user,
|
|
|
|
ctx.request.body.password
|
|
|
|
);
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/enableUser", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
await ctx.instance.authApi.enableUser(
|
|
|
|
ctx.request.body.username);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/disableUser", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
await ctx.instance.authApi.disableUser(
|
|
|
|
ctx.request.body.username);
|
2019-06-19 23:05:53 +02:00
|
|
|
|
|
|
|
await ctx.master.removeSessionsForUser(
|
|
|
|
ctx.params.appname,
|
|
|
|
ctx.request.body.username
|
|
|
|
);
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.get("/:appname/api/users", async (ctx) => {
|
2019-06-15 00:03:01 +02:00
|
|
|
ctx.body = await ctx.instance.authApi.getUsers();
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.get("/:appname/api/accessLevels", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.authApi.getAccessLevels();
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/listRecords/:indexkey", async (ctx) => {
|
2019-06-16 00:55:32 +02: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 11:05:46 +02:00
|
|
|
})
|
2019-06-16 00:55:32 +02: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 11:05:46 +02:00
|
|
|
})
|
2019-06-28 23:59:27 +02: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 00:55:32 +02:00
|
|
|
ctx.body = await ctx.instance.recordApi.save(
|
|
|
|
ctx.request.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-07-05 17:56:53 +02: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-28 23:59:27 +02:00
|
|
|
.get("/:appname/api/record/*", async (ctx) => {
|
2019-07-11 10:43:47 +02: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 11:05:46 +02:00
|
|
|
})
|
2019-06-28 23:59:27 +02:00
|
|
|
.del("/:appname/api/record/*", async (ctx) => {
|
2019-06-16 00:55:32 +02:00
|
|
|
await ctx.instance.recordApi.delete(
|
2019-06-28 23:59:27 +02:00
|
|
|
getRecordKey(ctx.params.appname, ctx.request.path)
|
2019-06-16 00:55:32 +02:00
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-25 23:48:22 +02:00
|
|
|
.post("/:appname/api/apphierarchy", async (ctx) => {
|
|
|
|
ctx.body = await ctx.instance.templateApi.saveApplicationHierarchy(
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 11:05:46 +02:00
|
|
|
})
|
2019-06-14 18:01:01 +02:00
|
|
|
.post("/:appname/api/actionsAndTriggers", async (ctx) => {
|
2019-06-25 23:48:22 +02:00
|
|
|
ctx.body = await ctx.instance.templateApi.saveApplicationHierarchy(
|
2019-06-16 00:55:32 +02:00
|
|
|
ctx.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 18:01:01 +02:00
|
|
|
})
|
2019-06-16 00:55:32 +02:00
|
|
|
.get("/:appname/api/appDefinition", async (ctx) => {
|
|
|
|
ctx.body = await ctx.instance.templateApi.saveActionsAndTriggers(
|
|
|
|
ctx.body
|
|
|
|
);
|
|
|
|
ctx.response.status = StatusCodes.OK;
|
2019-06-14 18:01:01 +02:00
|
|
|
});
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2019-06-28 23:59:27 +02:00
|
|
|
const getRecordKey = (appname, wholePath) =>
|
|
|
|
wholePath
|
2019-07-05 17:56:53 +02:00
|
|
|
.replace(`/${appname}/api/files/`, "")
|
|
|
|
.replace(`/${appname}/api/lookup_field/`, "")
|
|
|
|
.replace(`/${appname}/api/record/`, "");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2019-06-14 11:05:46 +02:00
|
|
|
return router;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
front end get authenticateTemporaryAccess {}
|
|
|
|
*/
|