split out id parsing into pro repo

This commit is contained in:
Peter Clement 2022-07-15 11:29:20 +01:00
parent 3f721400e6
commit b5ffd995f4
5 changed files with 25 additions and 7 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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]
}
})

View File

@ -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