From b5ffd995f49286ffefaeb5e7ba965d1c00e9adfb Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Fri, 15 Jul 2022 11:29:20 +0100 Subject: [PATCH] split out id parsing into pro repo --- packages/backend-core/src/db/conversions.js | 5 +++++ packages/backend-core/src/security/roles.js | 5 +++-- .../pages/builder/portal/manage/groups/[groupId].svelte | 7 +++++++ .../pages/builder/portal/manage/users/[userId].svelte | 9 +++++---- packages/server/src/utilities/global.js | 6 +++++- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/backend-core/src/db/conversions.js b/packages/backend-core/src/db/conversions.js index 455cc712d8..6dba74295f 100644 --- a/packages/backend-core/src/db/conversions.js +++ b/packages/backend-core/src/db/conversions.js @@ -50,3 +50,8 @@ exports.getProdAppID = appId => { const rest = split.join(APP_DEV_PREFIX) return `${APP_PREFIX}${rest}` } + +exports.extractAppId = id => { + const split = id?.split("_") || [] + return split.length ? split[split.length - 1] : null +} diff --git a/packages/backend-core/src/security/roles.js b/packages/backend-core/src/security/roles.js index 7c57cadcbf..44dc4f2d3e 100644 --- a/packages/backend-core/src/security/roles.js +++ b/packages/backend-core/src/security/roles.js @@ -76,7 +76,7 @@ function isBuiltin(role) { /** * Works through the inheritance ranks to see how far up the builtin stack this ID is. */ -function builtinRoleToNumber(id) { +exports.builtinRoleToNumber = id => { const builtins = exports.getBuiltinRoles() const MAX = Object.values(BUILTIN_IDS).length + 1 if (id === BUILTIN_IDS.ADMIN || id === BUILTIN_IDS.BUILDER) { @@ -104,7 +104,8 @@ exports.lowerBuiltinRoleID = (roleId1, roleId2) => { if (!roleId2) { return roleId1 } - return builtinRoleToNumber(roleId1) > builtinRoleToNumber(roleId2) + return exports.builtinRoleToNumber(roleId1) > + exports.builtinRoleToNumber(roleId2) ? roleId2 : roleId1 } diff --git a/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte b/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte index efade705eb..fde2c7213e 100644 --- a/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte +++ b/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte @@ -67,6 +67,13 @@ async function removeUser(id) { let newUsers = group.users.filter(user => user._id !== id) group.users = newUsers + let user = await users.get(id) + + await users.save({ + ...user, + userGroups: [], + }) + await groups.actions.save(group) } diff --git a/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte b/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte index 287fa0f57e..47bd2aab61 100644 --- a/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte +++ b/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte @@ -46,15 +46,15 @@ .filter(x => { if ($userFetch.data?.roles) { return Object.keys($userFetch.data.roles).find(y => { - return x.appId === y + return x.appId === apps.extractAppId(y) }) } }) .map(app => { let roles = Object.fromEntries( - Object.entries($userFetch.data.roles).filter( - ([key]) => key === app.appId - ) + Object.entries($userFetch.data.roles).filter(([key]) => { + return apps.extractAppId(key) === app.appId + }) ) return { name: app.name, @@ -90,6 +90,7 @@ Object.keys(roles).forEach(role => { let roleNumber = RoleUtils.getRolePriority(roles[role]) if (roleNumber > highestRoleNumber) { + highestRoleNumber = roleNumber highestRole = roles[role] } }) diff --git a/packages/server/src/utilities/global.js b/packages/server/src/utilities/global.js index f8ec5ea647..fcc2ecd216 100644 --- a/packages/server/src/utilities/global.js +++ b/packages/server/src/utilities/global.js @@ -12,8 +12,9 @@ const { } = require("@budibase/backend-core/tenancy") const env = require("../environment") const { getAppId } = require("@budibase/backend-core/context") +const { groups } = require("@budibase/pro") -exports.updateAppRole = (user, { appId } = {}) => { +exports.updateAppRole = async (user, { appId } = {}) => { appId = appId || getAppId() if (!user || !user.roles) { return user @@ -32,6 +33,9 @@ exports.updateAppRole = (user, { appId } = {}) => { user.roleId = BUILTIN_ROLE_IDS.ADMIN } else if (!user.roleId) { user.roleId = BUILTIN_ROLE_IDS.PUBLIC + } else if (user?.userGroups?.length) { + let roleId = await groups.getGroupRoleId(user, appId) + user.roleId = roleId } delete user.roles return user