|
|
|
@ -1,4 +1,3 @@ |
|
|
|
const CouchDB = require("../../db") |
|
|
|
const env = require("../../environment") |
|
|
|
const packageJson = require("../../../package.json") |
|
|
|
const { |
|
|
|
@ -45,11 +44,13 @@ 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") |
|
|
|
const context = require("@budibase/backend-core/context") |
|
|
|
|
|
|
|
const URL_REGEX_SLASH = /\/|\\/g |
|
|
|
|
|
|
|
// utility function, need to do away with this
|
|
|
|
async function getLayouts(db) { |
|
|
|
async function getLayouts() { |
|
|
|
const db = context.getAppDB() |
|
|
|
return ( |
|
|
|
await db.allDocs( |
|
|
|
getLayoutParams(null, { |
|
|
|
@ -59,7 +60,8 @@ async function getLayouts(db) { |
|
|
|
).rows.map(row => row.doc) |
|
|
|
} |
|
|
|
|
|
|
|
async function getScreens(db) { |
|
|
|
async function getScreens() { |
|
|
|
const db = context.getAppDB() |
|
|
|
return ( |
|
|
|
await db.allDocs( |
|
|
|
getScreenParams(null, { |
|
|
|
@ -116,8 +118,9 @@ async function createInstance(template) { |
|
|
|
const tenantId = isMultiTenant() ? getTenantId() : null |
|
|
|
const baseAppId = generateAppID(tenantId) |
|
|
|
const appId = generateDevAppID(baseAppId) |
|
|
|
context.updateAppId(appId) |
|
|
|
|
|
|
|
const db = new CouchDB(appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
await db.put({ |
|
|
|
_id: "_design/database", |
|
|
|
// view collation information, read before writing any complex views:
|
|
|
|
@ -127,9 +130,9 @@ async function createInstance(template) { |
|
|
|
|
|
|
|
// NOTE: indexes need to be created before any tables/templates
|
|
|
|
// add view for linked rows
|
|
|
|
await createLinkView(appId) |
|
|
|
await createRoutingView(appId) |
|
|
|
await createAllSearchIndex(appId) |
|
|
|
await createLinkView() |
|
|
|
await createRoutingView() |
|
|
|
await createAllSearchIndex() |
|
|
|
|
|
|
|
// replicate the template data to the instance DB
|
|
|
|
// this is currently very hard to test, downloading and importing template files
|
|
|
|
@ -155,7 +158,7 @@ async function createInstance(template) { |
|
|
|
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 }) |
|
|
|
const apps = await getAllApps({ dev, all }) |
|
|
|
|
|
|
|
// get the locks for all the dev apps
|
|
|
|
if (dev || all) { |
|
|
|
@ -178,12 +181,11 @@ exports.fetch = async ctx => { |
|
|
|
} |
|
|
|
|
|
|
|
exports.fetchAppDefinition = async ctx => { |
|
|
|
const db = new CouchDB(ctx.params.appId) |
|
|
|
const layouts = await getLayouts(db) |
|
|
|
const layouts = await getLayouts() |
|
|
|
const userRoleId = getUserRoleId(ctx) |
|
|
|
const accessController = new AccessController(ctx.params.appId) |
|
|
|
const accessController = new AccessController() |
|
|
|
const screens = await accessController.checkScreensAccess( |
|
|
|
await getScreens(db), |
|
|
|
await getScreens(), |
|
|
|
userRoleId |
|
|
|
) |
|
|
|
ctx.body = { |
|
|
|
@ -194,15 +196,15 @@ exports.fetchAppDefinition = async ctx => { |
|
|
|
} |
|
|
|
|
|
|
|
exports.fetchAppPackage = async ctx => { |
|
|
|
const db = new CouchDB(ctx.params.appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA) |
|
|
|
const layouts = await getLayouts(db) |
|
|
|
let screens = await getScreens(db) |
|
|
|
const layouts = await getLayouts() |
|
|
|
let screens = await getScreens() |
|
|
|
|
|
|
|
// 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) |
|
|
|
const accessController = new AccessController() |
|
|
|
screens = await accessController.checkScreensAccess(screens, userRoleId) |
|
|
|
} |
|
|
|
|
|
|
|
@ -215,7 +217,7 @@ exports.fetchAppPackage = async ctx => { |
|
|
|
} |
|
|
|
|
|
|
|
exports.create = async ctx => { |
|
|
|
const apps = await getAllApps(CouchDB, { dev: true }) |
|
|
|
const apps = await getAllApps({ dev: true }) |
|
|
|
const name = ctx.request.body.name |
|
|
|
checkAppName(ctx, apps, name) |
|
|
|
const url = await getAppUrl(ctx) |
|
|
|
@ -233,7 +235,7 @@ exports.create = async ctx => { |
|
|
|
const instance = await createInstance(instanceConfig) |
|
|
|
const appId = instance._id |
|
|
|
|
|
|
|
const db = new CouchDB(appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
let _rev |
|
|
|
try { |
|
|
|
// if template there will be an existing doc
|
|
|
|
@ -277,7 +279,7 @@ exports.create = async ctx => { |
|
|
|
} |
|
|
|
|
|
|
|
exports.update = async ctx => { |
|
|
|
const apps = await getAllApps(CouchDB, { dev: true }) |
|
|
|
const apps = await getAllApps({ dev: true }) |
|
|
|
// validation
|
|
|
|
const name = ctx.request.body.name |
|
|
|
checkAppName(ctx, apps, name, ctx.params.appId) |
|
|
|
@ -292,7 +294,7 @@ exports.update = async ctx => { |
|
|
|
|
|
|
|
exports.updateClient = async ctx => { |
|
|
|
// Get current app version
|
|
|
|
const db = new CouchDB(ctx.params.appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA) |
|
|
|
const currentVersion = application.version |
|
|
|
|
|
|
|
@ -314,7 +316,7 @@ exports.updateClient = async ctx => { |
|
|
|
|
|
|
|
exports.revertClient = async ctx => { |
|
|
|
// Check app can be reverted
|
|
|
|
const db = new CouchDB(ctx.params.appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA) |
|
|
|
if (!application.revertableVersion) { |
|
|
|
ctx.throw(400, "There is no version to revert to") |
|
|
|
@ -336,7 +338,7 @@ exports.revertClient = async ctx => { |
|
|
|
} |
|
|
|
|
|
|
|
exports.delete = async ctx => { |
|
|
|
const db = new CouchDB(ctx.params.appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
|
|
|
|
const result = await db.destroy() |
|
|
|
/* istanbul ignore next */ |
|
|
|
@ -364,7 +366,8 @@ exports.sync = async (ctx, next) => { |
|
|
|
const prodAppId = getDeployedAppID(appId) |
|
|
|
|
|
|
|
try { |
|
|
|
const prodDb = new CouchDB(prodAppId, { skip_setup: true }) |
|
|
|
// specific case, want to make sure setup is skipped
|
|
|
|
const prodDb = context.getProdAppDB({ skip_setup: true }) |
|
|
|
const info = await prodDb.info() |
|
|
|
if (info.error) throw info.error |
|
|
|
} catch (err) { |
|
|
|
@ -392,7 +395,7 @@ exports.sync = async (ctx, next) => { |
|
|
|
} |
|
|
|
|
|
|
|
// sync the users
|
|
|
|
await syncGlobalUsers(appId) |
|
|
|
await syncGlobalUsers() |
|
|
|
|
|
|
|
if (error) { |
|
|
|
ctx.throw(400, error) |
|
|
|
@ -404,7 +407,7 @@ exports.sync = async (ctx, next) => { |
|
|
|
} |
|
|
|
|
|
|
|
const updateAppPackage = async (appPackage, appId) => { |
|
|
|
const db = new CouchDB(appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA) |
|
|
|
|
|
|
|
const newAppPackage = { ...application, ...appPackage } |
|
|
|
@ -423,7 +426,7 @@ const updateAppPackage = async (appPackage, appId) => { |
|
|
|
} |
|
|
|
|
|
|
|
const createEmptyAppPackage = async (ctx, app) => { |
|
|
|
const db = new CouchDB(app.appId) |
|
|
|
const db = context.getAppDB() |
|
|
|
|
|
|
|
let screensAndLayouts = [] |
|
|
|
for (let layout of BASE_LAYOUTS) { |
|
|
|
|