2021-06-08 17:06:30 +02:00
|
|
|
const {
|
|
|
|
getMultiIDParams,
|
|
|
|
getGlobalIDFromUserMetadataID,
|
|
|
|
} = require("../db/utils")
|
|
|
|
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
|
2021-08-02 19:34:43 +02:00
|
|
|
const { getDeployedAppID } = require("@budibase/auth/db")
|
2021-06-08 17:06:30 +02:00
|
|
|
const { getGlobalUserParams } = require("@budibase/auth/db")
|
2021-07-06 19:10:04 +02:00
|
|
|
const { user: userCache } = require("@budibase/auth/cache")
|
2021-08-02 19:34:43 +02:00
|
|
|
const { getGlobalDB } = require("@budibase/auth/tenancy")
|
2021-06-08 17:06:30 +02:00
|
|
|
|
|
|
|
exports.updateAppRole = (appId, user) => {
|
|
|
|
if (!user.roles) {
|
|
|
|
return user
|
|
|
|
}
|
2021-06-14 16:23:24 +02:00
|
|
|
|
|
|
|
// always use the deployed app
|
|
|
|
user.roleId = user.roles[getDeployedAppID(appId)]
|
|
|
|
// if a role wasn't found then either set as admin (builder) or public (everyone else)
|
|
|
|
if (!user.roleId && user.builder && user.builder.global) {
|
2021-06-08 17:06:30 +02:00
|
|
|
user.roleId = BUILTIN_ROLE_IDS.ADMIN
|
2021-06-14 16:23:24 +02:00
|
|
|
} else if (!user.roleId) {
|
2021-07-06 19:43:04 +02:00
|
|
|
user.roleId = BUILTIN_ROLE_IDS.BASIC
|
2021-06-08 17:06:30 +02:00
|
|
|
}
|
|
|
|
delete user.roles
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
function processUser(appId, user) {
|
2021-06-08 17:06:30 +02:00
|
|
|
if (user) {
|
|
|
|
delete user.password
|
|
|
|
}
|
|
|
|
return exports.updateAppRole(appId, user)
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
exports.getCachedSelf = async (ctx, appId) => {
|
2021-07-15 18:57:02 +02:00
|
|
|
const user = await userCache.getUser(ctx.user._id, ctx.user.tenantId)
|
2021-07-06 19:10:04 +02:00
|
|
|
return processUser(appId, user)
|
|
|
|
}
|
|
|
|
|
2021-07-15 18:57:02 +02:00
|
|
|
exports.getGlobalUser = async (ctx, appId, userId) => {
|
2021-08-02 19:34:43 +02:00
|
|
|
const db = getGlobalDB()
|
2021-07-06 19:10:04 +02:00
|
|
|
let user = await db.get(getGlobalIDFromUserMetadataID(userId))
|
|
|
|
return processUser(appId, user)
|
|
|
|
}
|
|
|
|
|
2021-07-15 18:57:02 +02:00
|
|
|
exports.getGlobalUsers = async (ctx, appId = null, users = null) => {
|
2021-08-02 19:34:43 +02:00
|
|
|
const db = getGlobalDB()
|
2021-06-08 17:06:30 +02:00
|
|
|
let globalUsers
|
|
|
|
if (users) {
|
|
|
|
const globalIds = users.map(user => getGlobalIDFromUserMetadataID(user._id))
|
|
|
|
globalUsers = (await db.allDocs(getMultiIDParams(globalIds))).rows.map(
|
|
|
|
row => row.doc
|
|
|
|
)
|
|
|
|
} else {
|
2021-06-08 17:11:46 +02:00
|
|
|
globalUsers = (
|
|
|
|
await db.allDocs(
|
|
|
|
getGlobalUserParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
2021-06-08 17:06:30 +02:00
|
|
|
}
|
2021-06-08 17:11:46 +02:00
|
|
|
globalUsers = globalUsers
|
|
|
|
.filter(user => user != null)
|
|
|
|
.map(user => {
|
|
|
|
delete user.password
|
|
|
|
return user
|
|
|
|
})
|
2021-06-08 17:06:30 +02:00
|
|
|
if (!appId) {
|
|
|
|
return globalUsers
|
|
|
|
}
|
|
|
|
return globalUsers.map(user => exports.updateAppRole(appId, user))
|
2021-06-08 17:11:46 +02:00
|
|
|
}
|