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