From dd4a963c3e5916a022c6abebc4c7087a5fd6bd95 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 17 May 2021 14:20:19 +0100 Subject: [PATCH] Updating role system to never think about the dev app. --- packages/auth/src/db/utils.js | 12 ++++++++++++ packages/server/src/utilities/workerRequests.js | 12 ++++++++++-- packages/worker/src/api/controllers/admin/roles.js | 11 +++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index f065f9f89a..3f97a01a1c 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -137,6 +137,18 @@ exports.getRoleParams = (roleId = null, otherProps = {}) => { return getDocParams(DocumentTypes.ROLE, roleId, otherProps) } +/** + * Convert a development app ID to a deployed app ID. + */ +exports.getDeployedAppID = appId => { + // if dev, convert it + if (appId.startsWith(exports.APP_DEV_PREFIX)) { + const id = appId.split(exports.APP_DEV_PREFIX)[1] + return `${exports.APP_PREFIX}${id}` + } + return appId +} + /** * Lots of different points in the system need to find the full list of apps, this will * enumerate the entire CouchDB cluster and get the list of databases (every app). diff --git a/packages/server/src/utilities/workerRequests.js b/packages/server/src/utilities/workerRequests.js index aecc79eaa6..14296aa94b 100644 --- a/packages/server/src/utilities/workerRequests.js +++ b/packages/server/src/utilities/workerRequests.js @@ -2,6 +2,7 @@ const fetch = require("node-fetch") const env = require("../environment") const { checkSlashesInUrl } = require("./index") const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles") +const { getDeployedAppID } = require("@budibase/auth/db") function getAppRole(appId, user) { if (!user.roles) { @@ -95,6 +96,8 @@ exports.deleteGlobalUser = async (ctx, globalId) => { } exports.getGlobalUsers = async (ctx, appId = null, globalId = null) => { + // always use the deployed app + appId = getDeployedAppID(appId) const endpoint = globalId ? `/api/admin/users/${globalId}` : `/api/admin/users` @@ -119,9 +122,14 @@ exports.saveGlobalUser = async (ctx, appId, body) => { const globalUser = body._id ? await exports.getGlobalUsers(ctx, appId, body._id) : {} - const roles = globalUser.roles || {} + const preRoles = globalUser.roles || {} if (body.roleId) { - roles[appId] = body.roleId + preRoles[appId] = body.roleId + } + // make sure no dev app IDs in roles + const roles = {} + for (let [appId, roleId] of Object.entries(preRoles)) { + roles[getDeployedAppID(appId)] = roleId } const endpoint = `/api/admin/users` const reqCfg = { diff --git a/packages/worker/src/api/controllers/admin/roles.js b/packages/worker/src/api/controllers/admin/roles.js index 3bb5647ca7..b515a507b2 100644 --- a/packages/worker/src/api/controllers/admin/roles.js +++ b/packages/worker/src/api/controllers/admin/roles.js @@ -1,17 +1,24 @@ const { getAllRoles } = require("@budibase/auth/roles") -const { getAllApps } = require("@budibase/auth/db") +const { getAllApps, getDeployedAppID } = require("@budibase/auth/db") exports.fetch = async ctx => { // always use the dev apps as they'll be most up to date (true) const apps = await getAllApps(true) const promises = [] for (let app of apps) { + // use dev app IDs promises.push(getAllRoles(app._id)) } const roles = await Promise.all(promises) const response = {} for (let app of apps) { - response[app._id] = roles.shift() + const deployedAppId = getDeployedAppID(app._id) + response[deployedAppId] = { + roles: roles.shift(), + name: app.name, + version: app.version, + url: app.url, + } } ctx.body = response }