2022-11-22 13:41:36 +01:00
|
|
|
import fetch from "node-fetch"
|
|
|
|
import env from "../environment"
|
|
|
|
import { checkSlashesInUrl } from "./index"
|
2023-01-11 21:39:33 +01:00
|
|
|
import {
|
|
|
|
db as dbCore,
|
|
|
|
constants,
|
|
|
|
tenancy,
|
|
|
|
logging,
|
2023-03-13 16:02:59 +01:00
|
|
|
env as coreEnv,
|
2023-01-11 21:39:33 +01:00
|
|
|
} from "@budibase/backend-core"
|
2022-11-22 13:41:36 +01:00
|
|
|
import { updateAppRole } from "./global"
|
2022-12-06 18:20:26 +01:00
|
|
|
import { BBContext, User } from "@budibase/types"
|
2021-04-09 16:11:49 +02:00
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export function request(ctx?: BBContext, request?: any) {
|
2021-04-09 16:11:49 +02:00
|
|
|
if (!request.headers) {
|
|
|
|
request.headers = {}
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
if (!ctx) {
|
2023-03-13 16:02:59 +01:00
|
|
|
request.headers[constants.Header.API_KEY] = coreEnv.INTERNAL_API_KEY
|
2022-11-22 13:41:36 +01:00
|
|
|
if (tenancy.isTenantIdSet()) {
|
|
|
|
request.headers[constants.Header.TENANT_ID] = tenancy.getTenantId()
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
2021-06-04 13:13:29 +02:00
|
|
|
}
|
2021-05-10 14:18:05 +02:00
|
|
|
if (request.body && Object.keys(request.body).length > 0) {
|
2021-04-09 16:11:49 +02:00
|
|
|
request.headers["Content-Type"] = "application/json"
|
|
|
|
request.body =
|
|
|
|
typeof request.body === "object"
|
|
|
|
? JSON.stringify(request.body)
|
|
|
|
: request.body
|
2021-05-10 14:18:05 +02:00
|
|
|
} else {
|
|
|
|
delete request.body
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|
2021-05-11 13:02:29 +02:00
|
|
|
if (ctx && ctx.headers) {
|
2022-02-23 19:31:32 +01:00
|
|
|
request.headers = ctx.headers
|
2021-04-09 18:33:21 +02:00
|
|
|
}
|
2023-01-11 21:39:33 +01:00
|
|
|
|
|
|
|
// add x-budibase-correlation-id header
|
|
|
|
logging.correlation.setHeader(request.headers)
|
|
|
|
|
2021-04-09 16:11:49 +02:00
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
async function checkResponse(
|
|
|
|
response: any,
|
|
|
|
errorMsg: string,
|
|
|
|
{ ctx }: { ctx?: BBContext } = {}
|
|
|
|
) {
|
2022-02-25 20:00:12 +01:00
|
|
|
if (response.status !== 200) {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
error = await response.json()
|
|
|
|
} catch (err) {
|
|
|
|
error = await response.text()
|
|
|
|
}
|
2022-02-25 20:01:17 +01:00
|
|
|
const msg = `Unable to ${errorMsg} - ${
|
|
|
|
error.message ? error.message : error
|
|
|
|
}`
|
2022-02-25 20:00:12 +01:00
|
|
|
if (ctx) {
|
|
|
|
ctx.throw(400, msg)
|
|
|
|
} else {
|
|
|
|
throw msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response.json()
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
// have to pass in the tenant ID as this could be coming from an automation
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function sendSmtpEmail(
|
|
|
|
to: string,
|
|
|
|
from: string,
|
|
|
|
subject: string,
|
|
|
|
contents: string,
|
|
|
|
cc: string,
|
|
|
|
bcc: string,
|
2022-11-25 20:57:07 +01:00
|
|
|
automation: boolean
|
2022-11-22 13:41:36 +01:00
|
|
|
) {
|
2021-08-05 10:59:08 +02:00
|
|
|
// tenant ID will be set in header
|
2021-05-11 13:02:29 +02:00
|
|
|
const response = await fetch(
|
2021-08-05 10:59:08 +02:00
|
|
|
checkSlashesInUrl(env.WORKER_URL + `/api/global/email/send`),
|
2022-11-22 13:41:36 +01:00
|
|
|
request(undefined, {
|
2021-05-11 13:02:29 +02:00
|
|
|
method: "POST",
|
2021-05-11 16:08:59 +02:00
|
|
|
body: {
|
|
|
|
email: to,
|
|
|
|
from,
|
|
|
|
contents,
|
|
|
|
subject,
|
2022-09-21 16:58:04 +02:00
|
|
|
cc,
|
|
|
|
bcc,
|
2021-05-11 16:08:59 +02:00
|
|
|
purpose: "custom",
|
2021-09-27 17:28:39 +02:00
|
|
|
automation,
|
2021-05-11 16:08:59 +02:00
|
|
|
},
|
2021-05-11 13:02:29 +02:00
|
|
|
})
|
|
|
|
)
|
2022-02-25 20:00:12 +01:00
|
|
|
return checkResponse(response, "send email")
|
2021-05-11 13:02:29 +02:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function getGlobalSelf(ctx: BBContext, appId?: string) {
|
2022-02-14 19:11:35 +01:00
|
|
|
const endpoint = `/api/global/self`
|
2021-05-19 16:09:57 +02:00
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + endpoint),
|
2021-06-04 13:13:29 +02:00
|
|
|
// we don't want to use API key when getting self
|
2021-08-05 10:59:08 +02:00
|
|
|
request(ctx, { method: "GET" })
|
2021-05-19 16:09:57 +02:00
|
|
|
)
|
2022-02-25 20:00:12 +01:00
|
|
|
let json = await checkResponse(response, "get self globally", { ctx })
|
2021-05-27 15:53:41 +02:00
|
|
|
if (appId) {
|
2022-01-27 19:18:31 +01:00
|
|
|
json = updateAppRole(json)
|
2021-05-27 15:53:41 +02:00
|
|
|
}
|
2021-05-19 16:09:57 +02:00
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function removeAppFromUserRoles(ctx: BBContext, appId: string) {
|
|
|
|
const prodAppId = dbCore.getProdAppID(appId)
|
2021-06-01 16:58:40 +02:00
|
|
|
const response = await fetch(
|
2022-01-31 18:42:51 +01:00
|
|
|
checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`),
|
2021-08-05 10:59:08 +02:00
|
|
|
request(ctx, {
|
2021-06-01 16:58:40 +02:00
|
|
|
method: "DELETE",
|
|
|
|
})
|
|
|
|
)
|
2022-02-25 20:00:12 +01:00
|
|
|
return checkResponse(response, "remove app role")
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function allGlobalUsers(ctx: BBContext) {
|
2022-02-25 20:00:12 +01:00
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
|
|
|
|
// we don't want to use API key when getting self
|
|
|
|
request(ctx, { method: "GET" })
|
|
|
|
)
|
|
|
|
return checkResponse(response, "get users", { ctx })
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function saveGlobalUser(ctx: BBContext) {
|
2022-02-25 20:00:12 +01:00
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
|
|
|
|
// we don't want to use API key when getting self
|
|
|
|
request(ctx, { method: "POST", body: ctx.request.body })
|
|
|
|
)
|
|
|
|
return checkResponse(response, "save user", { ctx })
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function deleteGlobalUser(ctx: BBContext) {
|
2022-02-25 20:00:12 +01:00
|
|
|
const response = await fetch(
|
2022-02-25 20:01:17 +01:00
|
|
|
checkSlashesInUrl(
|
|
|
|
env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
|
|
|
|
),
|
2022-02-25 20:00:12 +01:00
|
|
|
// we don't want to use API key when getting self
|
|
|
|
request(ctx, { method: "DELETE" })
|
|
|
|
)
|
2022-11-22 13:41:36 +01:00
|
|
|
return checkResponse(response, "delete user", { ctx })
|
2022-02-25 20:00:12 +01:00
|
|
|
}
|
|
|
|
|
2022-12-06 18:20:26 +01:00
|
|
|
export async function readGlobalUser(ctx: BBContext): Promise<User> {
|
2022-02-25 20:00:12 +01:00
|
|
|
const response = await fetch(
|
2022-02-25 20:01:17 +01:00
|
|
|
checkSlashesInUrl(
|
|
|
|
env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
|
|
|
|
),
|
2022-02-25 20:00:12 +01:00
|
|
|
// we don't want to use API key when getting self
|
|
|
|
request(ctx, { method: "GET" })
|
|
|
|
)
|
|
|
|
return checkResponse(response, "get user", { ctx })
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|
2022-06-30 12:28:52 +02:00
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function createAdminUser(
|
|
|
|
email: string,
|
|
|
|
password: string,
|
|
|
|
tenantId: string
|
|
|
|
) {
|
2022-06-30 12:28:52 +02:00
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/users/init"),
|
2022-11-22 13:41:36 +01:00
|
|
|
request(undefined, { method: "POST", body: { email, password, tenantId } })
|
2022-06-30 12:28:52 +02:00
|
|
|
)
|
|
|
|
return checkResponse(response, "create admin user")
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function getChecklist() {
|
2022-06-30 12:28:52 +02:00
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"),
|
2022-11-22 13:41:36 +01:00
|
|
|
request(undefined, { method: "GET" })
|
2022-06-30 12:28:52 +02:00
|
|
|
)
|
|
|
|
return checkResponse(response, "get checklist")
|
|
|
|
}
|
2022-09-05 19:28:53 +02:00
|
|
|
|
2022-11-22 13:41:36 +01:00
|
|
|
export async function generateApiKey(userId: string) {
|
2022-09-05 19:28:53 +02:00
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + "/api/global/self/api_key"),
|
2022-11-22 13:41:36 +01:00
|
|
|
request(undefined, { method: "POST", body: { userId } })
|
2022-09-05 19:28:53 +02:00
|
|
|
)
|
|
|
|
return checkResponse(response, "generate API key")
|
|
|
|
}
|