2021-04-09 16:11:49 +02:00
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const env = require("../environment")
|
|
|
|
const { checkSlashesInUrl } = require("./index")
|
|
|
|
const { BUILTIN_ROLE_IDS } = require("./security/roles")
|
|
|
|
|
|
|
|
function getAppRole(appId, user) {
|
|
|
|
if (!user.roles) {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
user.roleId = user.roles[appId]
|
|
|
|
if (!user.roleId) {
|
|
|
|
user.roleId = BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
}
|
2021-04-15 16:57:55 +02:00
|
|
|
delete user.roles
|
2021-04-09 16:11:49 +02:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:57:55 +02:00
|
|
|
function request(ctx, request) {
|
2021-04-09 16:11:49 +02:00
|
|
|
if (!request.headers) {
|
|
|
|
request.headers = {}
|
|
|
|
}
|
|
|
|
if (request.body) {
|
|
|
|
request.headers["Content-Type"] = "application/json"
|
|
|
|
request.body =
|
|
|
|
typeof request.body === "object"
|
|
|
|
? JSON.stringify(request.body)
|
|
|
|
: request.body
|
|
|
|
}
|
2021-04-09 18:33:21 +02:00
|
|
|
if (ctx.headers) {
|
|
|
|
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-04-09 16:11:49 +02:00
|
|
|
exports.getDeployedApps = async ctx => {
|
|
|
|
if (!env.SELF_HOSTED) {
|
|
|
|
throw "Can only check apps for self hosted environments"
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + `/api/apps`),
|
2021-04-15 16:57:55 +02:00
|
|
|
request(ctx, {
|
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-04-19 17:26:33 +02:00
|
|
|
exports.deleteGlobalUser = async (ctx, globalId) => {
|
|
|
|
const endpoint = `/api/admin/users/${globalId}`
|
2021-04-09 16:11:49 +02:00
|
|
|
const reqCfg = { method: "DELETE" }
|
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + endpoint),
|
2021-04-15 16:57:55 +02:00
|
|
|
request(ctx, reqCfg)
|
2021-04-09 16:11:49 +02:00
|
|
|
)
|
|
|
|
return response.json()
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:26:33 +02:00
|
|
|
exports.getGlobalUsers = async (ctx, appId = null, globalId = null) => {
|
|
|
|
const endpoint = globalId
|
|
|
|
? `/api/admin/users/${globalId}`
|
|
|
|
: `/api/admin/users`
|
2021-04-09 16:11:49 +02:00
|
|
|
const reqCfg = { method: "GET" }
|
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + endpoint),
|
2021-04-15 16:57:55 +02:00
|
|
|
request(ctx, reqCfg)
|
2021-04-09 16:11:49 +02:00
|
|
|
)
|
|
|
|
let users = await response.json()
|
2021-04-15 16:57:55 +02:00
|
|
|
if (!appId) {
|
|
|
|
return users
|
|
|
|
}
|
2021-04-09 16:11:49 +02:00
|
|
|
if (Array.isArray(users)) {
|
|
|
|
users = users.map(user => getAppRole(appId, user))
|
|
|
|
} else {
|
|
|
|
users = getAppRole(appId, users)
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:26:33 +02:00
|
|
|
exports.saveGlobalUser = async (ctx, appId, body, globalId = null) => {
|
|
|
|
const globalUser = await exports.getGlobalUsers(ctx, appId, globalId)
|
2021-04-09 16:11:49 +02:00
|
|
|
const roles = globalUser.roles || {}
|
|
|
|
if (body.roleId) {
|
|
|
|
roles[appId] = body.roleId
|
|
|
|
}
|
|
|
|
const endpoint = `/api/admin/users`
|
|
|
|
const reqCfg = {
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
...globalUser,
|
2021-04-13 18:11:55 +02:00
|
|
|
password: body.password || undefined,
|
2021-04-09 16:11:49 +02:00
|
|
|
status: body.status,
|
2021-04-19 17:26:33 +02:00
|
|
|
email: body.email,
|
2021-04-09 16:11:49 +02:00
|
|
|
roles,
|
2021-04-13 19:12:35 +02:00
|
|
|
builder: {
|
|
|
|
global: true,
|
|
|
|
},
|
2021-04-09 16:11:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
checkSlashesInUrl(env.WORKER_URL + endpoint),
|
2021-04-15 16:57:55 +02:00
|
|
|
request(ctx, reqCfg)
|
2021-04-09 16:11:49 +02:00
|
|
|
)
|
|
|
|
const json = await response.json()
|
|
|
|
if (json.status !== 200 && response.status !== 200) {
|
|
|
|
ctx.throw(400, "Unable to save global user.")
|
|
|
|
}
|
|
|
|
delete body.email
|
|
|
|
delete body.password
|
|
|
|
delete body.roleId
|
|
|
|
delete body.status
|
2021-04-13 18:11:55 +02:00
|
|
|
delete body.roles
|
|
|
|
delete body.builder
|
2021-04-19 17:26:33 +02:00
|
|
|
return {
|
|
|
|
...body,
|
|
|
|
_id: json._id,
|
|
|
|
}
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|