2021-04-09 16:11:49 +02:00
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const env = require("../environment")
|
|
|
|
const { checkSlashesInUrl } = require("./index")
|
2021-05-17 15:20:19 +02:00
|
|
|
const { getDeployedAppID } = require("@budibase/auth/db")
|
2021-06-08 17:06:30 +02:00
|
|
|
const { updateAppRole, getGlobalUser } = require("./global")
|
2021-07-23 16:29:14 +02:00
|
|
|
const { Headers } = require("@budibase/auth/constants")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { getTenantId, isTenantIdSet } = require("@budibase/auth/tenancy")
|
2021-04-09 16:11:49 +02:00
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
function request(ctx, request) {
|
2021-04-09 16:11:49 +02:00
|
|
|
if (!request.headers) {
|
|
|
|
request.headers = {}
|
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
if (!ctx) {
|
2021-07-23 16:29:14 +02:00
|
|
|
request.headers[Headers.API_KEY] = env.INTERNAL_API_KEY
|
2021-08-05 10:59:08 +02:00
|
|
|
if (isTenantIdSet()) {
|
|
|
|
request.headers[Headers.TENANT_ID] = getTenantId()
|
|
|
|
}
|
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) {
|
2021-04-09 18:33:21 +02:00
|
|
|
request.headers.cookie = ctx.headers.cookie
|
|
|
|
}
|
2021-04-09 16:11:49 +02:00
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:57:55 +02:00
|
|
|
exports.request = request
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
// have to pass in the tenant ID as this could be coming from an automation
|
2021-09-27 17:28:39 +02:00
|
|
|
exports.sendSmtpEmail = async (to, from, subject, contents, automation) => {
|
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`),
|
2021-05-11 13:02:29 +02:00
|
|
|
request(null, {
|
|
|
|
method: "POST",
|
2021-05-11 16:08:59 +02:00
|
|
|
body: {
|
|
|
|
email: to,
|
|
|
|
from,
|
|
|
|
contents,
|
|
|
|
subject,
|
|
|
|
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
|
|
|
})
|
|
|
|
)
|
2021-05-11 16:08:59 +02:00
|
|
|
|
2021-06-01 16:58:40 +02:00
|
|
|
if (response.status !== 200) {
|
2021-10-01 14:29:08 +02:00
|
|
|
const error = await response.text()
|
|
|
|
throw `Unable to send email - ${error}`
|
2021-05-11 16:08:59 +02:00
|
|
|
}
|
2021-06-01 16:58:40 +02:00
|
|
|
return response.json()
|
2021-05-11 13:02:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:31:53 +02:00
|
|
|
exports.getDeployedApps = async () => {
|
2021-04-09 16:11:49 +02:00
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + `/api/apps`),
|
2021-10-04 14:31:53 +02:00
|
|
|
request(null, {
|
2021-04-09 16:11:49 +02:00
|
|
|
method: "GET",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const json = await response.json()
|
|
|
|
const apps = {}
|
|
|
|
for (let [key, value] of Object.entries(json)) {
|
|
|
|
if (value.url) {
|
|
|
|
value.url = value.url.toLowerCase()
|
|
|
|
apps[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apps
|
|
|
|
} catch (err) {
|
|
|
|
// error, cannot determine deployed apps, don't stop app creation - sort this later
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 15:53:41 +02:00
|
|
|
exports.getGlobalSelf = async (ctx, appId = null) => {
|
2021-08-05 10:59:08 +02:00
|
|
|
const endpoint = `/api/global/users/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
|
|
|
)
|
2021-06-01 16:58:40 +02:00
|
|
|
if (response.status !== 200) {
|
2021-05-19 16:09:57 +02:00
|
|
|
ctx.throw(400, "Unable to get self globally.")
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|
2021-06-01 16:58:40 +02:00
|
|
|
let json = await response.json()
|
2021-05-27 15:53:41 +02:00
|
|
|
if (appId) {
|
2021-06-08 17:06:30 +02:00
|
|
|
json = updateAppRole(appId, json)
|
2021-05-27 15:53:41 +02:00
|
|
|
}
|
2021-05-19 16:09:57 +02:00
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:55:00 +02:00
|
|
|
exports.addAppRoleToUser = async (ctx, appId, roleId, userId = null) => {
|
|
|
|
let user,
|
|
|
|
endpoint,
|
|
|
|
body = {}
|
|
|
|
if (!userId) {
|
|
|
|
user = await exports.getGlobalSelf(ctx)
|
2021-08-05 10:59:08 +02:00
|
|
|
endpoint = `/api/global/users/self`
|
2021-05-19 16:55:00 +02:00
|
|
|
} else {
|
2021-09-02 18:13:00 +02:00
|
|
|
user = await getGlobalUser(appId, userId)
|
2021-05-19 16:55:00 +02:00
|
|
|
body._id = userId
|
2021-08-05 10:59:08 +02:00
|
|
|
endpoint = `/api/global/users`
|
2021-05-19 16:55:00 +02:00
|
|
|
}
|
|
|
|
body = {
|
|
|
|
...body,
|
|
|
|
roles: {
|
|
|
|
...user.roles,
|
2021-06-01 16:58:40 +02:00
|
|
|
[getDeployedAppID(appId)]: roleId,
|
2021-04-09 16:11:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + endpoint),
|
2021-05-19 16:55:00 +02:00
|
|
|
request(ctx, {
|
|
|
|
method: "POST",
|
|
|
|
body,
|
|
|
|
})
|
2021-04-09 16:11:49 +02:00
|
|
|
)
|
2021-06-01 16:58:40 +02:00
|
|
|
if (response.status !== 200) {
|
2021-05-19 16:09:57 +02:00
|
|
|
ctx.throw(400, "Unable to save self globally.")
|
2021-04-19 17:26:33 +02:00
|
|
|
}
|
2021-06-01 16:58:40 +02:00
|
|
|
return response.json()
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
exports.removeAppFromUserRoles = async (ctx, appId) => {
|
2021-06-01 16:58:40 +02:00
|
|
|
const deployedAppId = getDeployedAppID(appId)
|
|
|
|
const response = await fetch(
|
2021-08-05 10:59:08 +02:00
|
|
|
checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${deployedAppId}`),
|
|
|
|
request(ctx, {
|
2021-06-01 16:58:40 +02:00
|
|
|
method: "DELETE",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw "Unable to remove app role"
|
|
|
|
}
|
|
|
|
return response.json()
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|