Adding fix for global user retrieval as part of admin creation.

This commit is contained in:
mike12345567 2022-06-30 12:01:15 +01:00
parent e7ff299c63
commit 3344a756d7
3 changed files with 24 additions and 20 deletions

View File

@ -1,7 +1,6 @@
const {
generateConfigID,
getConfigParams,
getGlobalUserParams,
getScopedFullConfig,
getAllApps,
} = require("@budibase/backend-core/db")
@ -20,6 +19,7 @@ const {
bustCache,
} = require("@budibase/backend-core/cache")
const { events } = require("@budibase/backend-core")
const { checkAnyUserExists } = require("../../../utilities/users")
const BB_TENANT_CDN = "https://tenants.cdn.budi.live"
@ -405,12 +405,7 @@ exports.configChecklist = async function (ctx) {
})
// They have set up an global user
const users = await db.allDocs(
getGlobalUserParams(null, {
include_docs: true,
limit: 1,
})
)
const userExists = await checkAnyUserExists()
return {
apps: {
checked: apps.length > 0,
@ -423,7 +418,7 @@ exports.configChecklist = async function (ctx) {
link: "/builder/portal/manage/email",
},
adminUser: {
checked: users && users.rows.length >= 1,
checked: userExists,
label: "Create your first user",
link: "/builder/portal/manage/users",
},

View File

@ -8,16 +8,15 @@ import {
events,
errors,
accounts,
db as dbUtils,
users as usersCore,
tenancy,
cache,
} from "@budibase/backend-core"
import { checkAnyUserExists } from "../../../utilities/users"
export const save = async (ctx: any) => {
try {
const user = await users.save(ctx.request.body)
ctx.body = user
ctx.body = await users.save(ctx.request.body)
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
@ -39,15 +38,8 @@ export const adminUser = async (ctx: any) => {
ctx.throw(403, "Organisation already exists.")
}
const response = await tenancy.doWithGlobalDB(tenantId, async (db: any) => {
return db.allDocs(
dbUtils.getGlobalUserParams(null, {
include_docs: true,
})
)
})
if (response.rows.some((row: any) => row.doc.admin)) {
const userExists = await checkAnyUserExists()
if (userExists) {
ctx.throw(
403,
"You cannot initialise once an global user has been created."

View File

@ -0,0 +1,17 @@
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
const { getGlobalUserParams } = require("@budibase/backend-core/db")
exports.checkAnyUserExists = async () => {
try {
const db = getGlobalDB()
const users = await db.allDocs(
getGlobalUserParams(null, {
include_docs: true,
limit: 1,
})
)
return users && users.rows.length >= 1
} catch (err) {
throw new Error("Unable to retrieve user list")
}
}