diff --git a/packages/worker/src/api/controllers/global/configs.js b/packages/worker/src/api/controllers/global/configs.js index 227b86d181..6de428c1b1 100644 --- a/packages/worker/src/api/controllers/global/configs.js +++ b/packages/worker/src/api/controllers/global/configs.js @@ -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", }, diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 27ca780e9e..32d392a690 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -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." diff --git a/packages/worker/src/utilities/users.js b/packages/worker/src/utilities/users.js new file mode 100644 index 0000000000..93057ca34f --- /dev/null +++ b/packages/worker/src/utilities/users.js @@ -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") + } +}