2021-05-21 14:07:10 +02:00
|
|
|
const { APP_DEV_PREFIX } = require("../db/utils")
|
|
|
|
const {
|
|
|
|
doesUserHaveLock,
|
|
|
|
updateLock,
|
|
|
|
checkDebounce,
|
|
|
|
setDebounce,
|
|
|
|
} = require("../utilities/redis")
|
2022-04-19 20:42:52 +02:00
|
|
|
const { doWithDB } = require("@budibase/backend-core/db")
|
2022-05-05 13:52:17 +02:00
|
|
|
const { DocumentTypes, getGlobalIDFromUserMetadataID } = require("../db/utils")
|
2022-01-10 20:33:00 +01:00
|
|
|
const { PermissionTypes } = require("@budibase/backend-core/permissions")
|
|
|
|
const { app: appCache } = require("@budibase/backend-core/cache")
|
2021-05-21 14:07:10 +02:00
|
|
|
|
2021-05-21 16:14:35 +02:00
|
|
|
const DEBOUNCE_TIME_SEC = 30
|
2021-05-21 14:07:10 +02:00
|
|
|
|
|
|
|
/************************************************** *
|
|
|
|
* This middleware has been broken out of the *
|
|
|
|
* "authorized" middleware as it had nothing to do *
|
|
|
|
* with authorization, but requires the perms *
|
|
|
|
* imparted by it. This middleware shouldn't *
|
|
|
|
* be called directly, it should always be called *
|
|
|
|
* through the authorized middleware *
|
|
|
|
****************************************************/
|
|
|
|
|
|
|
|
async function checkDevAppLocks(ctx) {
|
|
|
|
const appId = ctx.appId
|
|
|
|
|
|
|
|
// if any public usage, don't proceed
|
|
|
|
if (!ctx.user._id && !ctx.user.userId) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// not a development app, don't need to do anything
|
|
|
|
if (!appId || !appId.startsWith(APP_DEV_PREFIX)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!(await doesUserHaveLock(appId, ctx.user))) {
|
2021-10-06 17:27:46 +02:00
|
|
|
ctx.throw(400, "User does not hold app lock.")
|
2021-05-21 14:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// they do have lock, update it
|
|
|
|
await updateLock(appId, ctx.user)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateAppUpdatedAt(ctx) {
|
|
|
|
const appId = ctx.appId
|
|
|
|
// if debouncing skip this update
|
|
|
|
// get methods also aren't updating
|
2021-05-21 16:03:28 +02:00
|
|
|
if (ctx.method === "GET" || (await checkDebounce(appId))) {
|
2021-05-21 14:07:10 +02:00
|
|
|
return
|
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
await doWithDB(appId, async db => {
|
|
|
|
const metadata = await db.get(DocumentTypes.APP_METADATA)
|
|
|
|
metadata.updatedAt = new Date().toISOString()
|
2022-05-05 13:52:17 +02:00
|
|
|
|
|
|
|
const getInitials = user => {
|
|
|
|
let initials = ""
|
|
|
|
initials += user.firstName ? user.firstName[0] : ""
|
|
|
|
initials += user.lastName ? user.lastName[0] : ""
|
|
|
|
return initials == "" ? undefined : initials
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata.updatedBy = {
|
|
|
|
email: ctx.user.email,
|
|
|
|
firstName: ctx.user.firstName,
|
|
|
|
lastName: ctx.user.lastName,
|
|
|
|
initials: getInitials(ctx.user),
|
|
|
|
_id: getGlobalIDFromUserMetadataID(ctx.user.userId),
|
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
const response = await db.put(metadata)
|
|
|
|
metadata._rev = response.rev
|
|
|
|
await appCache.invalidateAppMetadata(appId, metadata)
|
|
|
|
// set a new debounce record with a short TTL
|
|
|
|
await setDebounce(appId, DEBOUNCE_TIME_SEC)
|
|
|
|
})
|
2021-05-21 14:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = async (ctx, permType) => {
|
|
|
|
const appId = ctx.appId
|
|
|
|
// this only functions within an app context
|
|
|
|
if (!appId) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const isBuilderApi = permType === PermissionTypes.BUILDER
|
|
|
|
const referer = ctx.headers["referer"]
|
2022-05-05 13:52:17 +02:00
|
|
|
|
|
|
|
const overviewPath = "/builder/portal/overview/"
|
|
|
|
const overviewContext = !referer ? false : referer.includes(overviewPath)
|
|
|
|
if (overviewContext) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasAppId = !referer ? false : referer.includes(appId)
|
|
|
|
const editingApp = referer ? hasAppId : false
|
2021-05-21 14:07:10 +02:00
|
|
|
// check this is a builder call and editing
|
|
|
|
if (!isBuilderApi || !editingApp) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// check locks
|
|
|
|
await checkDevAppLocks(ctx)
|
|
|
|
// set updated at time on app
|
|
|
|
await updateAppUpdatedAt(ctx)
|
2021-05-21 15:49:59 +02:00
|
|
|
}
|