2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-05-14 16:12:30 +02:00
|
|
|
const env = require("../../environment")
|
2020-10-21 16:28:30 +02:00
|
|
|
const packageJson = require("../../../package.json")
|
2021-03-25 19:04:44 +01:00
|
|
|
const {
|
|
|
|
createLinkView,
|
|
|
|
createRoutingView,
|
2021-03-26 00:42:50 +01:00
|
|
|
createAllSearchIndex,
|
2021-03-25 19:04:44 +01:00
|
|
|
} = require("../../db/views/staticViews")
|
2021-03-19 20:07:47 +01:00
|
|
|
const {
|
|
|
|
getTemplateStream,
|
|
|
|
createApp,
|
|
|
|
deleteApp,
|
|
|
|
} = require("../../utilities/fileSystem")
|
2020-11-02 15:53:51 +01:00
|
|
|
const {
|
|
|
|
generateAppID,
|
2020-11-23 15:07:18 +01:00
|
|
|
getLayoutParams,
|
2020-11-18 19:24:12 +01:00
|
|
|
getScreenParams,
|
2020-11-03 18:42:54 +01:00
|
|
|
generateScreenID,
|
2021-05-13 12:06:08 +02:00
|
|
|
generateDevAppID,
|
|
|
|
DocumentTypes,
|
|
|
|
AppStatus,
|
2020-11-02 15:53:51 +01:00
|
|
|
} = require("../../db/utils")
|
2021-05-14 17:32:51 +02:00
|
|
|
const { BUILTIN_ROLE_IDS, AccessController } = require("@budibase/auth/roles")
|
2020-11-23 16:46:26 +01:00
|
|
|
const { BASE_LAYOUTS } = require("../../constants/layouts")
|
2021-05-20 14:05:58 +02:00
|
|
|
const { createHomeScreen } = require("../../constants/screens")
|
2020-11-03 18:42:54 +01:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
2021-01-20 14:32:15 +01:00
|
|
|
const { processObject } = require("@budibase/string-templates")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { getAllApps } = require("@budibase/auth/db")
|
2020-11-25 16:03:19 +01:00
|
|
|
const { USERS_TABLE_SCHEMA } = require("../../constants")
|
2021-06-01 17:02:20 +02:00
|
|
|
const {
|
|
|
|
getDeployedApps,
|
|
|
|
removeAppFromUserRoles,
|
|
|
|
} = require("../../utilities/workerRequests")
|
2021-04-01 13:48:38 +02:00
|
|
|
const { clientLibraryPath } = require("../../utilities")
|
2021-05-13 13:16:09 +02:00
|
|
|
const { getAllLocks } = require("../../utilities/redis")
|
2021-07-07 18:07:42 +02:00
|
|
|
const {
|
2021-07-08 13:56:54 +02:00
|
|
|
updateClientLibrary,
|
|
|
|
backupClientLibrary,
|
|
|
|
revertClientLibrary,
|
|
|
|
} = require("../../utilities/fileSystem/clientLibrary")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { getTenantId, isMultiTenant } = require("@budibase/auth/tenancy")
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2021-01-18 13:36:49 +01:00
|
|
|
const URL_REGEX_SLASH = /\/|\\/g
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-11-18 19:24:12 +01:00
|
|
|
// utility function, need to do away with this
|
2020-11-23 16:46:26 +01:00
|
|
|
async function getLayouts(db) {
|
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getLayoutParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
).rows.map(row => row.doc)
|
2020-11-23 16:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getScreens(db) {
|
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
).rows.map(row => row.doc)
|
2020-11-23 16:46:26 +01:00
|
|
|
}
|
2020-11-18 19:24:12 +01:00
|
|
|
|
2020-12-02 14:26:57 +01:00
|
|
|
function getUserRoleId(ctx) {
|
|
|
|
return !ctx.user.role || !ctx.user.role._id
|
|
|
|
? BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
: ctx.user.role._id
|
2020-11-18 19:24:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 18:01:31 +01:00
|
|
|
async function getAppUrlIfNotInUse(ctx) {
|
|
|
|
let url
|
|
|
|
if (ctx.request.body.url) {
|
|
|
|
url = encodeURI(ctx.request.body.url)
|
2021-06-29 09:20:17 +02:00
|
|
|
} else if (ctx.request.body.name) {
|
2021-01-14 18:01:31 +01:00
|
|
|
url = encodeURI(`${ctx.request.body.name}`)
|
|
|
|
}
|
2021-06-29 09:20:17 +02:00
|
|
|
if (url) {
|
|
|
|
url = `/${url.replace(URL_REGEX_SLASH, "")}`.toLowerCase()
|
|
|
|
}
|
2021-03-24 19:21:23 +01:00
|
|
|
if (!env.SELF_HOSTED) {
|
2021-01-14 18:01:31 +01:00
|
|
|
return url
|
|
|
|
}
|
2021-04-09 16:11:49 +02:00
|
|
|
const deployedApps = await getDeployedApps(ctx)
|
2021-01-14 18:01:31 +01:00
|
|
|
if (
|
2021-06-29 09:20:17 +02:00
|
|
|
url &&
|
2021-01-14 18:01:31 +01:00
|
|
|
deployedApps[url] != null &&
|
|
|
|
deployedApps[url].appId !== ctx.params.appId
|
|
|
|
) {
|
|
|
|
ctx.throw(400, "App name/URL is already in use.")
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:32:25 +02:00
|
|
|
async function createInstance(template) {
|
2021-08-05 10:59:08 +02:00
|
|
|
const tenantId = isMultiTenant() ? getTenantId() : null
|
|
|
|
const baseAppId = generateAppID(tenantId)
|
2021-05-13 12:06:08 +02:00
|
|
|
const appId = generateDevAppID(baseAppId)
|
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2020-10-29 11:21:06 +01: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: {},
|
|
|
|
})
|
|
|
|
|
|
|
|
// replicate the template data to the instance DB
|
2021-03-10 13:56:30 +01:00
|
|
|
// this is currently very hard to test, downloading and importing template files
|
|
|
|
/* istanbul ignore next */
|
2021-03-15 19:32:20 +01:00
|
|
|
if (template && template.useTemplate === "true") {
|
2021-03-19 15:43:41 +01:00
|
|
|
const { ok } = await db.load(await getTemplateStream(template))
|
2020-10-29 11:21:06 +01:00
|
|
|
if (!ok) {
|
|
|
|
throw "Error loading database dump from template."
|
|
|
|
}
|
2020-11-24 15:04:14 +01:00
|
|
|
} else {
|
|
|
|
// create the users table
|
2020-11-25 16:03:19 +01:00
|
|
|
await db.put(USERS_TABLE_SCHEMA)
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 16:15:36 +02:00
|
|
|
// add view for linked rows
|
|
|
|
await createLinkView(appId)
|
|
|
|
await createRoutingView(appId)
|
|
|
|
await createAllSearchIndex(appId)
|
|
|
|
|
2021-06-08 19:06:16 +02:00
|
|
|
return { _id: appId }
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-05-19 16:09:57 +02:00
|
|
|
const dev = ctx.query && ctx.query.status === AppStatus.DEV
|
|
|
|
const all = ctx.query && ctx.query.status === AppStatus.ALL
|
2021-08-05 10:59:08 +02:00
|
|
|
const apps = await getAllApps(CouchDB, { dev, all })
|
2021-05-13 12:06:08 +02:00
|
|
|
|
2021-05-13 13:16:09 +02:00
|
|
|
// get the locks for all the dev apps
|
2021-05-19 16:09:57 +02:00
|
|
|
if (dev || all) {
|
2021-05-13 13:16:09 +02:00
|
|
|
const locks = await getAllLocks()
|
|
|
|
for (let app of apps) {
|
2021-05-19 16:09:57 +02:00
|
|
|
if (app.status !== "development") {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-16 22:25:37 +02:00
|
|
|
const lock = locks.find(lock => lock.appId === app.appId)
|
2021-05-13 13:16:09 +02:00
|
|
|
if (lock) {
|
|
|
|
app.lockedBy = lock.user
|
2021-05-13 15:17:04 +02:00
|
|
|
} else {
|
|
|
|
// make sure its definitely not present
|
|
|
|
delete app.lockedBy
|
2021-05-13 13:16:09 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-13 12:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = apps
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetchAppDefinition = async function (ctx) {
|
2020-11-18 19:24:12 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
2020-11-23 16:46:26 +01:00
|
|
|
const layouts = await getLayouts(db)
|
2020-12-02 14:26:57 +01:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2020-11-23 16:46:26 +01:00
|
|
|
const accessController = new AccessController(ctx.params.appId)
|
2020-12-04 15:02:58 +01:00
|
|
|
const screens = await accessController.checkScreensAccess(
|
2020-11-23 16:46:26 +01:00
|
|
|
await getScreens(db),
|
2020-12-02 14:26:57 +01:00
|
|
|
userRoleId
|
2020-11-23 16:46:26 +01:00
|
|
|
)
|
2020-11-18 19:24:12 +01:00
|
|
|
ctx.body = {
|
2020-11-23 16:46:26 +01:00
|
|
|
layouts,
|
|
|
|
screens,
|
2020-11-18 19:24:12 +01:00
|
|
|
libraries: ["@budibase/standard-components"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetchAppPackage = async function (ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
2021-05-16 22:25:37 +02:00
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA)
|
2021-06-30 22:54:48 +02:00
|
|
|
const layouts = await getLayouts(db)
|
|
|
|
let screens = await getScreens(db)
|
|
|
|
|
|
|
|
// Only filter screens if the user is not a builder
|
2021-07-05 18:05:34 +02:00
|
|
|
if (!(ctx.user.builder && ctx.user.builder.global)) {
|
2021-06-30 22:54:48 +02:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
|
|
|
const accessController = new AccessController(ctx.params.appId)
|
|
|
|
screens = await accessController.checkScreensAccess(screens, userRoleId)
|
|
|
|
}
|
2020-11-02 15:53:51 +01:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-23 16:46:26 +01:00
|
|
|
screens,
|
|
|
|
layouts,
|
2021-04-01 13:48:38 +02:00
|
|
|
clientLibPath: clientLibraryPath(ctx.params.appId),
|
2020-11-02 15:53:51 +01:00
|
|
|
}
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.create = async function (ctx) {
|
2021-03-15 19:32:20 +01:00
|
|
|
const { useTemplate, templateKey } = ctx.request.body
|
2021-03-16 14:43:46 +01:00
|
|
|
const instanceConfig = {
|
2021-03-15 19:32:20 +01:00
|
|
|
useTemplate,
|
|
|
|
key: templateKey,
|
2021-03-16 14:43:46 +01:00
|
|
|
}
|
|
|
|
if (ctx.request.files && ctx.request.files.templateFile) {
|
|
|
|
instanceConfig.file = ctx.request.files.templateFile
|
|
|
|
}
|
2021-08-03 16:32:25 +02:00
|
|
|
const instance = await createInstance(instanceConfig)
|
2021-06-08 19:06:16 +02:00
|
|
|
const appId = instance._id
|
2021-03-15 19:32:20 +01:00
|
|
|
|
2021-01-14 18:01:31 +01:00
|
|
|
const url = await getAppUrlIfNotInUse(ctx)
|
2021-06-08 19:06:16 +02:00
|
|
|
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-14 16:12:30 +02:00
|
|
|
const newApplication = {
|
2021-05-16 22:25:37 +02:00
|
|
|
_id: DocumentTypes.APP_METADATA,
|
2021-06-08 19:06:16 +02:00
|
|
|
_rev,
|
2021-05-16 22:25:37 +02:00
|
|
|
appId: instance._id,
|
2020-04-20 17:17:11 +02:00
|
|
|
type: "app",
|
2020-10-21 16:28:30 +02:00
|
|
|
version: packageJson.version,
|
2020-08-14 12:21:52 +02:00
|
|
|
componentLibraries: ["@budibase/standard-components"],
|
2020-05-26 17:29:16 +02:00
|
|
|
name: ctx.request.body.name,
|
2021-01-14 18:01:31 +01:00
|
|
|
url: url,
|
2020-09-25 15:47:42 +02:00
|
|
|
template: ctx.request.body.template,
|
2020-10-29 11:21:06 +01:00
|
|
|
instance: instance,
|
2021-08-05 10:59:08 +02:00
|
|
|
tenantId: getTenantId(),
|
2021-05-21 15:38:58 +02:00
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
createdAt: new Date().toISOString(),
|
2020-04-09 17:53:48 +02:00
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
const response = await db.put(newApplication, { force: true })
|
|
|
|
newApplication._rev = response.rev
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2021-03-19 20:07:47 +01:00
|
|
|
await createEmptyAppPackage(ctx, newApplication)
|
2021-03-10 13:56:30 +01:00
|
|
|
/* istanbul ignore next */
|
2021-03-25 14:32:05 +01:00
|
|
|
if (!env.isTest()) {
|
2021-03-19 20:07:47 +01:00
|
|
|
await createApp(appId)
|
2020-05-26 17:29:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 22:39:30 +02:00
|
|
|
ctx.status = 200
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.body = newApplication
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.update = async function (ctx) {
|
2021-07-07 18:07:42 +02:00
|
|
|
const data = await updateAppPackage(ctx, ctx.request.body, ctx.params.appId)
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = data
|
|
|
|
}
|
2020-06-29 17:18:43 +02:00
|
|
|
|
2021-07-07 18:07:42 +02:00
|
|
|
exports.updateClient = async function (ctx) {
|
2021-07-07 18:35:28 +02:00
|
|
|
// 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
|
2021-07-08 14:20:52 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
await backupClientLibrary(ctx.params.appId)
|
|
|
|
await updateClientLibrary(ctx.params.appId)
|
|
|
|
}
|
2021-07-07 18:35:28 +02:00
|
|
|
|
|
|
|
// Update versions in app package
|
|
|
|
const appPackageUpdates = {
|
|
|
|
version: packageJson.version,
|
|
|
|
revertableVersion: currentVersion,
|
|
|
|
}
|
2021-07-07 18:07:42 +02:00
|
|
|
const data = await updateAppPackage(ctx, appPackageUpdates, ctx.params.appId)
|
2020-06-29 17:18:43 +02:00
|
|
|
ctx.status = 200
|
2021-07-07 18:07:42 +02:00
|
|
|
ctx.body = data
|
2020-06-29 17:18:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 13:56:54 +02:00
|
|
|
exports.revertClient = async function (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
|
2021-07-08 14:20:52 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
await revertClientLibrary(ctx.params.appId)
|
|
|
|
}
|
2021-07-08 13:56:54 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.delete = async function (ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
2021-06-10 13:07:39 +02:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
const result = await db.destroy()
|
2021-03-25 14:32:05 +01:00
|
|
|
/* istanbul ignore next */
|
2021-06-10 13:07:39 +02:00
|
|
|
if (!env.isTest() && !ctx.query.unpublish) {
|
2021-03-23 18:54:02 +01:00
|
|
|
await deleteApp(ctx.params.appId)
|
|
|
|
}
|
2021-06-01 16:58:40 +02:00
|
|
|
// make sure the app/role doesn't stick around after the app has been deleted
|
2021-08-05 10:59:08 +02:00
|
|
|
await removeAppFromUserRoles(ctx, ctx.params.appId)
|
2020-07-07 14:44:05 +02:00
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = result
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:07:42 +02:00
|
|
|
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
|
2021-07-09 17:42:09 +02:00
|
|
|
delete newAppPackage.lockedBy
|
2021-07-07 18:07:42 +02:00
|
|
|
|
2021-07-08 13:56:54 +02:00
|
|
|
return await db.put(newAppPackage)
|
2021-07-07 18:07:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
const createEmptyAppPackage = async (ctx, app) => {
|
2021-05-17 22:43:50 +02:00
|
|
|
const db = new CouchDB(app.appId)
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-11-23 17:56:35 +01:00
|
|
|
let screensAndLayouts = []
|
2020-11-23 16:46:26 +01:00
|
|
|
for (let layout of BASE_LAYOUTS) {
|
|
|
|
const cloned = cloneDeep(layout)
|
2021-01-20 14:32:15 +01:00
|
|
|
screensAndLayouts.push(await processObject(cloned, app))
|
2020-11-23 16:46:26 +01:00
|
|
|
}
|
2020-11-06 14:40:00 +01:00
|
|
|
|
2020-12-04 15:02:58 +01:00
|
|
|
const homeScreen = createHomeScreen(app)
|
2020-11-23 16:46:26 +01:00
|
|
|
homeScreen._id = generateScreenID()
|
2020-11-23 17:56:35 +01:00
|
|
|
screensAndLayouts.push(homeScreen)
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-11-23 17:56:35 +01:00
|
|
|
await db.bulkDocs(screensAndLayouts)
|
2020-05-26 17:29:16 +02:00
|
|
|
}
|