2023-07-18 17:57:48 +02:00
|
|
|
import { UserCtx } from "@budibase/types"
|
2023-07-28 16:39:59 +02:00
|
|
|
import { isBuilder, isAdmin, hasBuilderPermissions } from "../users"
|
2023-07-18 17:57:48 +02:00
|
|
|
import { getAppId } from "../context"
|
2023-07-28 16:39:59 +02:00
|
|
|
import env from "../environment"
|
2022-11-24 19:48:51 +01:00
|
|
|
|
2023-07-18 17:57:48 +02:00
|
|
|
export default async (ctx: UserCtx, next: any) => {
|
|
|
|
const appId = getAppId()
|
2023-07-28 16:39:59 +02:00
|
|
|
const builderFn = env.isWorker()
|
|
|
|
? hasBuilderPermissions
|
|
|
|
: env.isApps()
|
|
|
|
? isBuilder
|
|
|
|
: undefined
|
|
|
|
if (!builderFn) {
|
|
|
|
throw new Error("Service name unknown - middleware inactive.")
|
|
|
|
}
|
|
|
|
if (!ctx.internal && !builderFn(ctx.user, appId) && !isAdmin(ctx.user)) {
|
2023-07-18 17:57:48 +02:00
|
|
|
ctx.throw(403, "Admin/Builder user only endpoint.")
|
2022-06-01 16:20:56 +02:00
|
|
|
}
|
|
|
|
return next()
|
|
|
|
}
|