1
0
Fork 0
mirror of synced 2024-06-14 16:35:02 +12:00
budibase/packages/worker/src/api/controllers/global/roles.js
2022-04-08 01:28:22 +01:00

68 lines
1.8 KiB
JavaScript

const { getAllRoles } = require("@budibase/backend-core/roles")
const {
getAllApps,
getProdAppID,
DocumentTypes,
} = require("@budibase/backend-core/db")
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
const { user: userCache } = require("@budibase/backend-core/cache")
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
const { users } = require("../../../sdk")
exports.fetch = async ctx => {
const tenantId = ctx.user.tenantId
// always use the dev apps as they'll be most up to date (true)
const apps = await getAllApps({ tenantId, all: true })
const promises = []
for (let app of apps) {
// use dev app IDs
promises.push(getAllRoles(app.appId))
}
const roles = await Promise.all(promises)
const response = {}
for (let app of apps) {
const deployedAppId = getProdAppID(app.appId)
response[deployedAppId] = {
roles: roles.shift(),
name: app.name,
version: app.version,
url: app.url,
}
}
ctx.body = response
}
exports.find = async ctx => {
const appId = ctx.params.appId
await doInAppContext(appId, async () => {
const db = getAppDB()
const app = await db.get(DocumentTypes.APP_METADATA)
ctx.body = {
roles: await getAllRoles(),
name: app.name,
version: app.version,
url: app.url,
}
})
}
exports.removeAppRole = async ctx => {
const { appId } = ctx.params
const db = getGlobalDB()
const allUsers = await users.allUsers(ctx)
const bulk = []
const cacheInvalidations = []
for (let user of allUsers) {
if (user.roles[appId]) {
cacheInvalidations.push(userCache.invalidateUser(user._id))
delete user.roles[appId]
bulk.push(user)
}
}
await db.bulkDocs(bulk)
await Promise.all(cacheInvalidations)
ctx.body = {
message: "App role removed from all users",
}
}