1
0
Fork 0
mirror of synced 2024-06-16 09:25:12 +12:00
budibase/packages/server/src/api/controllers/application.js

265 lines
7 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
2020-05-15 02:12:30 +12:00
const env = require("../../environment")
const packageJson = require("../../../package.json")
const {
createLinkView,
createRoutingView,
createAllSearchIndex,
} = require("../../db/views/staticViews")
const {
getTemplateStream,
createApp,
deleteApp,
} = require("../../utilities/fileSystem")
const {
generateAppID,
getLayoutParams,
getScreenParams,
generateScreenID,
2021-05-13 22:06:08 +12:00
generateDevAppID,
DocumentTypes,
AppStatus,
} = require("../../db/utils")
2021-05-15 03:32:51 +12:00
const { BUILTIN_ROLE_IDS, AccessController } = require("@budibase/auth/roles")
const { BASE_LAYOUTS } = require("../../constants/layouts")
const {
createHomeScreen,
createLoginScreen,
} = require("../../constants/screens")
const { cloneDeep } = require("lodash/fp")
const { processObject } = require("@budibase/string-templates")
const { getAllApps } = require("../../utilities")
2020-11-26 04:03:19 +13:00
const { USERS_TABLE_SCHEMA } = require("../../constants")
const { getDeployedApps } = require("../../utilities/workerRequests")
const { clientLibraryPath } = require("../../utilities")
const { getAllLocks } = require("../../utilities/redis")
2021-01-19 01:36:49 +13:00
const URL_REGEX_SLASH = /\/|\\/g
2020-04-08 04:25:09 +12:00
// utility function, need to do away with this
async function getLayouts(db) {
return (
await db.allDocs(
getLayoutParams(null, {
include_docs: true,
})
)
2021-05-04 22:32:22 +12:00
).rows.map(row => row.doc)
}
async function getScreens(db) {
return (
await db.allDocs(
getScreenParams(null, {
include_docs: true,
})
)
2021-05-04 22:32:22 +12:00
).rows.map(row => row.doc)
}
function getUserRoleId(ctx) {
return !ctx.user.role || !ctx.user.role._id
? BUILTIN_ROLE_IDS.PUBLIC
: ctx.user.role._id
}
async function getAppUrlIfNotInUse(ctx) {
let url
if (ctx.request.body.url) {
url = encodeURI(ctx.request.body.url)
} else {
url = encodeURI(`${ctx.request.body.name}`)
}
2021-01-19 01:36:49 +13:00
url = `/${url.replace(URL_REGEX_SLASH, "")}`.toLowerCase()
if (!env.SELF_HOSTED) {
return url
}
const deployedApps = await getDeployedApps(ctx)
if (
deployedApps[url] != null &&
deployedApps[url].appId !== ctx.params.appId
) {
ctx.throw(400, "App name/URL is already in use.")
}
return url
}
async function createInstance(template) {
2021-05-13 22:06:08 +12:00
const baseAppId = generateAppID()
const appId = generateDevAppID(baseAppId)
const db = new CouchDB(appId)
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
await createLinkView(appId)
await createRoutingView(appId)
await createAllSearchIndex(appId)
// replicate the template data to the instance DB
// this is currently very hard to test, downloading and importing template files
/* istanbul ignore next */
2021-03-16 07:32:20 +13:00
if (template && template.useTemplate === "true") {
const { ok } = await db.load(await getTemplateStream(template))
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)
}
return { _id: appId }
}
2021-05-03 19:31:09 +12:00
exports.fetch = async function (ctx) {
const isDev = ctx.query && ctx.query.status === AppStatus.DEV
2021-05-15 03:31:07 +12:00
const apps = await getAllApps(isDev)
2021-05-13 22:06:08 +12:00
// get the locks for all the dev apps
if (isDev) {
const locks = await getAllLocks()
for (let app of apps) {
2021-05-17 08:25:37 +12:00
const lock = locks.find(lock => lock.appId === app.appId)
if (lock) {
app.lockedBy = lock.user
} else {
// make sure its definitely not present
delete app.lockedBy
}
}
2021-05-13 22:06:08 +12:00
}
ctx.body = apps
2020-05-07 21:53:34 +12:00
}
2020-04-08 04:25:09 +12:00
2021-05-03 19:31:09 +12:00
exports.fetchAppDefinition = async function (ctx) {
const db = new CouchDB(ctx.params.appId)
const layouts = await getLayouts(db)
const userRoleId = getUserRoleId(ctx)
const accessController = new AccessController(ctx.params.appId)
const screens = await accessController.checkScreensAccess(
await getScreens(db),
userRoleId
)
ctx.body = {
layouts,
screens,
libraries: ["@budibase/standard-components"],
}
}
2021-05-03 19:31:09 +12:00
exports.fetchAppPackage = async function (ctx) {
const db = new CouchDB(ctx.params.appId)
2021-05-17 08:25:37 +12:00
const application = await db.get(DocumentTypes.APP_METADATA)
const [layouts, screens] = await Promise.all([getLayouts(db), getScreens(db)])
ctx.body = {
application,
screens,
layouts,
clientLibPath: clientLibraryPath(ctx.params.appId),
}
}
2021-05-03 19:31:09 +12:00
exports.create = async function (ctx) {
2021-03-16 07:32:20 +13:00
const { useTemplate, templateKey } = ctx.request.body
const instanceConfig = {
2021-03-16 07:32:20 +13:00
useTemplate,
key: templateKey,
}
if (ctx.request.files && ctx.request.files.templateFile) {
instanceConfig.file = ctx.request.files.templateFile
}
const instance = await createInstance(instanceConfig)
2021-03-16 07:32:20 +13:00
const url = await getAppUrlIfNotInUse(ctx)
const appId = instance._id
2020-05-15 02:12:30 +12:00
const newApplication = {
2021-05-17 08:25:37 +12:00
_id: DocumentTypes.APP_METADATA,
appId: instance._id,
type: "app",
version: packageJson.version,
componentLibraries: ["@budibase/standard-components"],
name: ctx.request.body.name,
url: url,
2020-09-26 01:47:42 +12:00
template: ctx.request.body.template,
instance: instance,
deployment: {
type: "cloud",
},
2020-04-10 03:53:48 +12:00
}
const instanceDb = new CouchDB(appId)
await instanceDb.put(newApplication)
2020-05-15 02:12:30 +12:00
await createEmptyAppPackage(ctx, newApplication)
/* istanbul ignore next */
2021-03-26 02:32:05 +13:00
if (!env.isTest()) {
await createApp(appId)
}
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
}
2021-05-03 19:31:09 +12:00
exports.update = async function (ctx) {
const url = await getAppUrlIfNotInUse(ctx)
const db = new CouchDB(ctx.params.appId)
const application = await db.get(ctx.params.appId)
const data = ctx.request.body
const newData = { ...application, ...data, url }
// the locked by property is attached by server but generated from
// Redis, shouldn't ever store it
if (newData.lockedBy) {
delete newData.lockedBy
}
const response = await db.put(newData)
data._rev = response.rev
ctx.status = 200
ctx.message = `Application ${application.name} updated successfully.`
ctx.body = response
}
2021-05-03 19:31:09 +12:00
exports.delete = async function (ctx) {
const db = new CouchDB(ctx.params.appId)
const app = await db.get(ctx.params.appId)
const result = await db.destroy()
2021-03-26 02:32:05 +13:00
/* istanbul ignore next */
if (!env.isTest()) {
await deleteApp(ctx.params.appId)
}
ctx.status = 200
ctx.message = `Application ${app.name} deleted successfully.`
ctx.body = result
}
const createEmptyAppPackage = async (ctx, app) => {
2021-05-18 08:43:50 +12:00
const db = new CouchDB(app.appId)
let screensAndLayouts = []
for (let layout of BASE_LAYOUTS) {
const cloned = cloneDeep(layout)
screensAndLayouts.push(await processObject(cloned, app))
}
2020-11-07 02:40:00 +13:00
const homeScreen = createHomeScreen(app)
homeScreen._id = generateScreenID()
screensAndLayouts.push(homeScreen)
const loginScreen = createLoginScreen(app)
loginScreen._id = generateScreenID()
screensAndLayouts.push(loginScreen)
await db.bulkDocs(screensAndLayouts)
}