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

412 lines
11 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,
2021-05-13 22:06:08 +12:00
generateDevAppID,
DocumentTypes,
AppStatus,
} = require("../../db/utils")
const {
BUILTIN_ROLE_IDS,
AccessController,
} = require("@budibase/backend-core/roles")
const { BASE_LAYOUTS } = require("../../constants/layouts")
const { cloneDeep } = require("lodash/fp")
const { processObject } = require("@budibase/string-templates")
const {
getAllApps,
isDevAppID,
getDeployedAppID,
Replication,
} = require("@budibase/backend-core/db")
2020-11-26 04:03:19 +13:00
const { USERS_TABLE_SCHEMA } = require("../../constants")
2021-06-02 03:02:20 +12:00
const {
getDeployedApps,
removeAppFromUserRoles,
} = require("../../utilities/workerRequests")
const { clientLibraryPath, stringToReadStream } = require("../../utilities")
const { getAllLocks } = require("../../utilities/redis")
const {
updateClientLibrary,
backupClientLibrary,
revertClientLibrary,
} = require("../../utilities/fileSystem/clientLibrary")
const { getTenantId, isMultiTenant } = require("@budibase/backend-core/tenancy")
const { syncGlobalUsers } = require("./user")
const { app: appCache } = require("@budibase/backend-core/cache")
const { cleanupAutomations } = require("../../automations/utils")
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 if (ctx.request.body.name) {
url = encodeURI(`${ctx.request.body.name}`)
}
if (url) {
url = `/${url.replace(URL_REGEX_SLASH, "")}`.toLowerCase()
}
const deployedApps = await getDeployedApps()
if (
url &&
deployedApps[url] != null &&
ctx.params != null &&
deployedApps[url].appId !== ctx.params.appId
) {
ctx.throw(400, "App name/URL is already in use.")
}
return url
}
async function createInstance(template) {
const tenantId = isMultiTenant() ? getTenantId() : null
const baseAppId = generateAppID(tenantId)
2021-05-13 22:06:08 +12:00
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: {},
})
// NOTE: indexes need to be created before any tables/templates
// 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
if (template && template.templateString) {
const { ok } = await db.load(stringToReadStream(template.templateString))
if (!ok) {
throw "Error loading database dump from memory."
}
} else if (template && template.useTemplate === "true") {
/* istanbul ignore next */
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 }
}
exports.fetch = async ctx => {
const dev = ctx.query && ctx.query.status === AppStatus.DEV
const all = ctx.query && ctx.query.status === AppStatus.ALL
const apps = await getAllApps(CouchDB, { dev, all })
2021-05-13 22:06:08 +12:00
// get the locks for all the dev apps
if (dev || all) {
const locks = await getAllLocks()
for (let app of apps) {
if (app.status !== "development") {
continue
}
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
exports.fetchAppDefinition = async 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"],
}
}
exports.fetchAppPackage = async 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 = await getLayouts(db)
let screens = await getScreens(db)
// Only filter screens if the user is not a builder
if (!(ctx.user.builder && ctx.user.builder.global)) {
const userRoleId = getUserRoleId(ctx)
const accessController = new AccessController(ctx.params.appId)
screens = await accessController.checkScreensAccess(screens, userRoleId)
}
ctx.body = {
application,
screens,
layouts,
clientLibPath: clientLibraryPath(ctx.params.appId, application.version),
}
}
exports.create = async ctx => {
const { useTemplate, templateKey, templateString } = ctx.request.body
const instanceConfig = {
2021-03-16 07:32:20 +13:00
useTemplate,
key: templateKey,
templateString,
}
if (ctx.request.files && ctx.request.files.templateFile) {
instanceConfig.file = ctx.request.files.templateFile
}
const instance = await createInstance(instanceConfig)
const appId = instance._id
2021-03-16 07:32:20 +13:00
const url = await getAppUrlIfNotInUse(ctx)
const db = new CouchDB(appId)
let _rev
try {
// if template there will be an existing doc
const existing = await db.get(DocumentTypes.APP_METADATA)
_rev = existing._rev
} catch (err) {
// nothing to do
}
2020-05-15 02:12:30 +12:00
const newApplication = {
2021-05-17 08:25:37 +12:00
_id: DocumentTypes.APP_METADATA,
_rev,
2021-05-17 08:25:37 +12:00
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,
tenantId: getTenantId(),
2021-05-22 01:38:58 +12:00
updatedAt: new Date().toISOString(),
createdAt: new Date().toISOString(),
2020-04-10 03:53:48 +12:00
}
const response = await db.put(newApplication, { force: true })
newApplication._rev = response.rev
2020-05-15 02:12:30 +12:00
// Only create the default home screens and layout if we aren't importing
// an app
if (useTemplate !== "true") {
await createEmptyAppPackage(ctx, newApplication)
}
/* istanbul ignore next */
2021-03-26 02:32:05 +13:00
if (!env.isTest()) {
await createApp(appId)
}
await appCache.invalidateAppMetadata(appId, newApplication)
ctx.status = 200
2020-05-15 02:12:30 +12:00
ctx.body = newApplication
2020-05-07 21:53:34 +12:00
}
exports.update = async ctx => {
const data = await updateAppPackage(ctx, ctx.request.body, ctx.params.appId)
ctx.status = 200
ctx.body = data
}
exports.updateClient = async ctx => {
// Get current app version
const db = new CouchDB(ctx.params.appId)
const application = await db.get(DocumentTypes.APP_METADATA)
const currentVersion = application.version
// Update client library and manifest
if (!env.isTest()) {
await backupClientLibrary(ctx.params.appId)
await updateClientLibrary(ctx.params.appId)
}
// Update versions in app package
const appPackageUpdates = {
version: packageJson.version,
revertableVersion: currentVersion,
}
const data = await updateAppPackage(ctx, appPackageUpdates, ctx.params.appId)
ctx.status = 200
ctx.body = data
}
exports.revertClient = async ctx => {
// Check app can be reverted
const db = new CouchDB(ctx.params.appId)
const application = await db.get(DocumentTypes.APP_METADATA)
if (!application.revertableVersion) {
ctx.throw(400, "There is no version to revert to")
}
// Update client library and manifest
if (!env.isTest()) {
await revertClientLibrary(ctx.params.appId)
}
// Update versions in app package
const appPackageUpdates = {
version: application.revertableVersion,
revertableVersion: null,
}
const data = await updateAppPackage(ctx, appPackageUpdates, ctx.params.appId)
ctx.status = 200
ctx.body = data
}
exports.delete = async ctx => {
const db = new CouchDB(ctx.params.appId)
const result = await db.destroy()
2021-03-26 02:32:05 +13:00
/* istanbul ignore next */
if (!env.isTest() && !ctx.query.unpublish) {
await deleteApp(ctx.params.appId)
}
2021-11-18 10:33:35 +13:00
if (ctx.query && ctx.query.unpublish) {
await cleanupAutomations(ctx.params.appId)
}
// make sure the app/role doesn't stick around after the app has been deleted
await removeAppFromUserRoles(ctx, ctx.params.appId)
await appCache.invalidateAppMetadata(ctx.params.appId)
ctx.status = 200
ctx.body = result
}
exports.sync = async (ctx, next) => {
const appId = ctx.params.appId
if (!isDevAppID(appId)) {
ctx.throw(400, "This action cannot be performed for production apps")
}
// replicate prod to dev
const prodAppId = getDeployedAppID(appId)
try {
const prodDb = new CouchDB(prodAppId, { skip_setup: true })
const info = await prodDb.info()
if (info.error) throw info.error
} catch (err) {
// the database doesn't exist. Don't replicate
ctx.status = 200
ctx.body = {
2021-11-09 03:26:44 +13:00
message: "App sync not required, app not deployed.",
}
return next()
}
const replication = new Replication({
source: prodAppId,
target: appId,
})
let error
try {
await replication.replicate({
filter: function (doc) {
return doc._id !== DocumentTypes.APP_METADATA
},
})
} catch (err) {
error = err
}
// sync the users
await syncGlobalUsers(appId)
if (error) {
ctx.throw(400, error)
} else {
ctx.body = {
message: "App sync completed successfully.",
}
}
}
const updateAppPackage = async (ctx, appPackage, appId) => {
const url = await getAppUrlIfNotInUse(ctx)
const db = new CouchDB(appId)
const application = await db.get(DocumentTypes.APP_METADATA)
const newAppPackage = { ...application, ...appPackage, url }
if (appPackage._rev !== application._rev) {
newAppPackage._rev = application._rev
}
// the locked by property is attached by server but generated from
// Redis, shouldn't ever store it
delete newAppPackage.lockedBy
const response = await db.put(newAppPackage)
// remove any cached metadata, so that it will be updated
await appCache.invalidateAppMetadata(appId)
return response
}
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
await db.bulkDocs(screensAndLayouts)
}