2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
2020-11-24 03:07:18 +13:00
|
|
|
const compileStaticAssets = require("../../utilities/builder/compileStaticAssets")
|
2020-05-15 02:12:30 +12:00
|
|
|
const env = require("../../environment")
|
2020-11-07 01:30:30 +13:00
|
|
|
const { existsSync } = require("fs-extra")
|
2020-06-04 08:23:56 +12:00
|
|
|
const { budibaseAppsDir } = require("../../utilities/budibaseDir")
|
2020-06-19 03:59:31 +12:00
|
|
|
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
|
2020-07-08 00:44:05 +12:00
|
|
|
const fs = require("fs-extra")
|
2020-10-09 22:00:57 +13:00
|
|
|
const { join, resolve } = require("../../utilities/centralPath")
|
2020-10-22 03:28:30 +13:00
|
|
|
const packageJson = require("../../../package.json")
|
2020-10-29 23:21:06 +13:00
|
|
|
const { createLinkView } = require("../../db/linkedRows")
|
2020-11-17 07:04:44 +13:00
|
|
|
const { createRoutingView } = require("../../utilities/routing")
|
2020-10-29 23:21:06 +13:00
|
|
|
const { downloadTemplate } = require("../../utilities/templates")
|
2020-11-03 03:53:51 +13:00
|
|
|
const {
|
|
|
|
generateAppID,
|
2020-11-24 03:07:18 +13:00
|
|
|
getLayoutParams,
|
2020-11-19 07:24:12 +13:00
|
|
|
getScreenParams,
|
2020-11-04 06:42:54 +13:00
|
|
|
generateScreenID,
|
2020-11-03 03:53:51 +13:00
|
|
|
} = require("../../db/utils")
|
2020-11-24 04:46:26 +13:00
|
|
|
const {
|
2020-12-03 02:26:57 +13:00
|
|
|
BUILTIN_ROLE_IDS,
|
2020-11-24 04:46:26 +13:00
|
|
|
AccessController,
|
2020-12-03 02:26:57 +13:00
|
|
|
} = require("../../utilities/security/roles")
|
2020-07-15 08:10:51 +12:00
|
|
|
const {
|
|
|
|
downloadExtractComponentLibraries,
|
|
|
|
} = require("../../utilities/createAppPackage")
|
2020-11-24 04:46:26 +13:00
|
|
|
const { BASE_LAYOUTS } = require("../../constants/layouts")
|
2020-12-05 03:02:58 +13:00
|
|
|
const {
|
|
|
|
createHomeScreen,
|
|
|
|
createLoginScreen,
|
|
|
|
} = require("../../constants/screens")
|
2020-11-04 06:42:54 +13:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
2020-11-24 04:46:26 +13:00
|
|
|
const { recurseMustache } = require("../../utilities/mustache")
|
2021-01-15 06:01:31 +13:00
|
|
|
const { getAllApps } = require("../../utilities")
|
2020-11-26 04:03:19 +13:00
|
|
|
const { USERS_TABLE_SCHEMA } = require("../../constants")
|
2021-01-15 06:01:31 +13:00
|
|
|
const {
|
|
|
|
getDeployedApps,
|
|
|
|
getHostingInfo,
|
|
|
|
HostingTypes,
|
|
|
|
} = require("../../utilities/builder/hosting")
|
2020-04-08 04:25:09 +12:00
|
|
|
|
2020-11-19 07:24:12 +13:00
|
|
|
// utility function, need to do away with this
|
2020-11-24 04:46:26 +13:00
|
|
|
async function getLayouts(db) {
|
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getLayoutParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getScreens(db) {
|
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
|
|
|
}
|
2020-11-19 07:24:12 +13:00
|
|
|
|
2020-12-03 02:26:57 +13:00
|
|
|
function getUserRoleId(ctx) {
|
|
|
|
return !ctx.user.role || !ctx.user.role._id
|
|
|
|
? BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
: ctx.user.role._id
|
2020-11-19 07:24:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-15 06:01:31 +13:00
|
|
|
async function getAppUrlIfNotInUse(ctx) {
|
|
|
|
let url
|
|
|
|
if (ctx.request.body.url) {
|
|
|
|
url = encodeURI(ctx.request.body.url)
|
|
|
|
} else {
|
|
|
|
url = encodeURI(`${ctx.request.body.name}`)
|
|
|
|
}
|
|
|
|
url = `/${url.replace(/\/|\\/g, "")}`
|
|
|
|
const hostingInfo = await getHostingInfo()
|
|
|
|
if (hostingInfo.type === HostingTypes.CLOUD) {
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
const deployedApps = await getDeployedApps()
|
|
|
|
if (
|
|
|
|
deployedApps[url] != null &&
|
|
|
|
deployedApps[url].appId !== ctx.params.appId
|
|
|
|
) {
|
|
|
|
ctx.throw(400, "App name/URL is already in use.")
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2020-10-29 23:21:06 +13:00
|
|
|
async function createInstance(template) {
|
2020-10-29 23:28:27 +13:00
|
|
|
const appId = generateAppID()
|
2020-10-29 23:21:06 +13:00
|
|
|
|
2020-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(appId)
|
2020-10-29 23:21:06 +13:00
|
|
|
await db.put({
|
|
|
|
_id: "_design/database",
|
|
|
|
// view collation information, read before writing any complex views:
|
|
|
|
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
|
|
|
|
views: {},
|
|
|
|
})
|
|
|
|
// add view for linked rows
|
2020-10-29 23:28:27 +13:00
|
|
|
await createLinkView(appId)
|
2020-11-14 04:35:20 +13:00
|
|
|
await createRoutingView(appId)
|
2020-10-29 23:21:06 +13:00
|
|
|
|
|
|
|
// replicate the template data to the instance DB
|
|
|
|
if (template) {
|
|
|
|
const templatePath = await downloadTemplate(...template.key.split("/"))
|
|
|
|
const dbDumpReadStream = fs.createReadStream(
|
|
|
|
join(templatePath, "db", "dump.txt")
|
|
|
|
)
|
|
|
|
const { ok } = await db.load(dbDumpReadStream)
|
|
|
|
if (!ok) {
|
|
|
|
throw "Error loading database dump from template."
|
|
|
|
}
|
2020-11-25 03:04:14 +13:00
|
|
|
} else {
|
|
|
|
// create the users table
|
2020-11-26 04:03:19 +13:00
|
|
|
await db.put(USERS_TABLE_SCHEMA)
|
2020-10-29 23:21:06 +13:00
|
|
|
}
|
|
|
|
|
2020-10-29 23:28:27 +13:00
|
|
|
return { _id: appId }
|
2020-10-29 23:21:06 +13:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:49:16 +12:00
|
|
|
exports.fetch = async function(ctx) {
|
2021-01-15 06:01:31 +13:00
|
|
|
ctx.body = await getAllApps()
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-04-08 04:25:09 +12:00
|
|
|
|
2020-11-19 07:24:12 +13:00
|
|
|
exports.fetchAppDefinition = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.params.appId)
|
2020-11-24 04:46:26 +13:00
|
|
|
const layouts = await getLayouts(db)
|
2020-12-03 02:26:57 +13:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2020-11-24 04:46:26 +13:00
|
|
|
const accessController = new AccessController(ctx.params.appId)
|
2020-12-05 03:02:58 +13:00
|
|
|
const screens = await accessController.checkScreensAccess(
|
2020-11-24 04:46:26 +13:00
|
|
|
await getScreens(db),
|
2020-12-03 02:26:57 +13:00
|
|
|
userRoleId
|
2020-11-24 04:46:26 +13:00
|
|
|
)
|
2020-11-19 07:24:12 +13:00
|
|
|
ctx.body = {
|
2020-11-24 04:46:26 +13:00
|
|
|
layouts,
|
|
|
|
screens,
|
2020-11-19 07:24:12 +13:00
|
|
|
libraries: ["@budibase/standard-components"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:49:16 +12:00
|
|
|
exports.fetchAppPackage = async function(ctx) {
|
2020-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const application = await db.get(ctx.params.appId)
|
2020-11-25 07:11:18 +13:00
|
|
|
const [layouts, screens] = await Promise.all([getLayouts(db), getScreens(db)])
|
2020-11-03 03:53:51 +13:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-24 04:46:26 +13:00
|
|
|
screens,
|
|
|
|
layouts,
|
2020-11-03 03:53:51 +13:00
|
|
|
}
|
2020-11-03 04:46:08 +13:00
|
|
|
await setBuilderToken(ctx, ctx.params.appId, application.version)
|
2020-04-21 03:17:11 +12:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:49:16 +12:00
|
|
|
exports.create = async function(ctx) {
|
2021-01-15 06:01:31 +13:00
|
|
|
const url = await getAppUrlIfNotInUse(ctx)
|
2020-10-29 23:21:06 +13:00
|
|
|
const instance = await createInstance(ctx.request.body.template)
|
2020-10-29 23:28:27 +13:00
|
|
|
const appId = instance._id
|
2020-11-06 01:43:03 +13:00
|
|
|
const version = packageJson.version
|
2020-05-15 02:12:30 +12:00
|
|
|
const newApplication = {
|
2020-10-29 23:28:27 +13:00
|
|
|
_id: appId,
|
2020-04-21 03:17:11 +12:00
|
|
|
type: "app",
|
2020-10-22 03:28:30 +13:00
|
|
|
version: packageJson.version,
|
2020-08-14 22:21:52 +12:00
|
|
|
componentLibraries: ["@budibase/standard-components"],
|
2020-05-27 03:29:16 +12:00
|
|
|
name: ctx.request.body.name,
|
2021-01-15 06:01:31 +13:00
|
|
|
url: url,
|
2020-09-26 01:47:42 +12:00
|
|
|
template: ctx.request.body.template,
|
2020-10-29 23:21:06 +13:00
|
|
|
instance: instance,
|
2020-12-15 07:31:48 +13:00
|
|
|
deployment: {
|
|
|
|
type: "cloud",
|
|
|
|
},
|
2020-04-10 03:53:48 +12:00
|
|
|
}
|
2020-10-29 23:28:27 +13:00
|
|
|
const instanceDb = new CouchDB(appId)
|
2020-10-29 09:35:06 +13:00
|
|
|
await instanceDb.put(newApplication)
|
2020-05-15 02:12:30 +12:00
|
|
|
|
2020-10-29 09:35:06 +13:00
|
|
|
if (env.NODE_ENV !== "jest") {
|
2020-05-27 03:29:16 +12:00
|
|
|
const newAppFolder = await createEmptyAppPackage(ctx, newApplication)
|
2020-07-15 08:07:53 +12:00
|
|
|
await downloadExtractComponentLibraries(newAppFolder)
|
2020-05-27 03:29:16 +12:00
|
|
|
}
|
|
|
|
|
2020-11-06 01:43:03 +13:00
|
|
|
await setBuilderToken(ctx, appId, version)
|
2020-06-11 08:39:30 +12:00
|
|
|
ctx.status = 200
|
2020-05-15 02:12:30 +12:00
|
|
|
ctx.body = newApplication
|
|
|
|
ctx.message = `Application ${ctx.request.body.name} created successfully`
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-05-27 03:29:16 +12:00
|
|
|
|
2020-06-30 03:49:16 +12:00
|
|
|
exports.update = async function(ctx) {
|
2021-01-15 06:01:31 +13:00
|
|
|
const url = await getAppUrlIfNotInUse(ctx)
|
2020-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const application = await db.get(ctx.params.appId)
|
2020-06-30 03:18:43 +12:00
|
|
|
|
|
|
|
const data = ctx.request.body
|
2021-01-15 06:01:31 +13:00
|
|
|
const newData = { ...application, ...data, url }
|
2020-06-30 03:18:43 +12:00
|
|
|
|
|
|
|
const response = await db.put(newData)
|
|
|
|
data._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `Application ${application.name} updated successfully.`
|
|
|
|
ctx.body = response
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:44:05 +12:00
|
|
|
exports.delete = async function(ctx) {
|
2020-10-29 23:28:27 +13:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const app = await db.get(ctx.params.appId)
|
2020-10-29 09:35:06 +13:00
|
|
|
const result = await db.destroy()
|
2020-10-14 04:31:14 +13:00
|
|
|
|
|
|
|
// remove top level directory
|
2020-10-29 23:28:27 +13:00
|
|
|
await fs.rmdir(join(budibaseAppsDir(), ctx.params.appId), {
|
2020-07-08 00:44:05 +12:00
|
|
|
recursive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `Application ${app.name} deleted successfully.`
|
|
|
|
ctx.body = result
|
|
|
|
}
|
|
|
|
|
2020-05-27 03:29:16 +12:00
|
|
|
const createEmptyAppPackage = async (ctx, app) => {
|
2020-06-04 08:23:56 +12:00
|
|
|
const appsFolder = budibaseAppsDir()
|
2020-10-07 21:58:32 +13:00
|
|
|
const newAppFolder = resolve(appsFolder, app._id)
|
2020-05-27 03:29:16 +12:00
|
|
|
|
2020-11-03 03:53:51 +13:00
|
|
|
const db = new CouchDB(app._id)
|
|
|
|
|
2020-10-14 05:02:59 +13:00
|
|
|
if (existsSync(newAppFolder)) {
|
2020-05-27 03:29:16 +12:00
|
|
|
ctx.throw(400, "App folder already exists for this application")
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:38:44 +13:00
|
|
|
fs.mkdirpSync(newAppFolder)
|
2020-11-07 02:40:00 +13:00
|
|
|
|
2020-11-24 05:56:35 +13:00
|
|
|
let screensAndLayouts = []
|
2020-11-24 04:46:26 +13:00
|
|
|
for (let layout of BASE_LAYOUTS) {
|
|
|
|
const cloned = cloneDeep(layout)
|
|
|
|
cloned.title = app.name
|
2020-11-24 05:56:35 +13:00
|
|
|
screensAndLayouts.push(recurseMustache(cloned, app))
|
2020-11-24 04:46:26 +13:00
|
|
|
}
|
2020-11-07 02:40:00 +13:00
|
|
|
|
2020-12-05 03:02:58 +13:00
|
|
|
const homeScreen = createHomeScreen(app)
|
2020-11-24 04:46:26 +13:00
|
|
|
homeScreen._id = generateScreenID()
|
2020-11-24 05:56:35 +13:00
|
|
|
screensAndLayouts.push(homeScreen)
|
2020-11-03 03:53:51 +13:00
|
|
|
|
2020-12-05 03:02:58 +13:00
|
|
|
const loginScreen = createLoginScreen(app)
|
2020-12-02 05:22:06 +13:00
|
|
|
loginScreen._id = generateScreenID()
|
|
|
|
screensAndLayouts.push(loginScreen)
|
|
|
|
|
2020-11-24 05:56:35 +13:00
|
|
|
await db.bulkDocs(screensAndLayouts)
|
2020-12-10 01:30:21 +13:00
|
|
|
await compileStaticAssets(app._id)
|
2020-05-27 03:29:16 +12:00
|
|
|
return newAppFolder
|
|
|
|
}
|