fixing bug on multi tenant environment

This commit is contained in:
Peter Clement 2022-07-22 10:12:16 +01:00
parent 0895c17a1c
commit 723bbd65e4
8 changed files with 68 additions and 45 deletions

View File

@ -45,19 +45,23 @@
$: publishedApps = $apps.filter(publishedAppsOnly) $: publishedApps = $apps.filter(publishedAppsOnly)
$: { $: {
if (!Object.keys($auth.user?.roles).length) { if (!Object.keys($auth.user?.roles).length && $auth.user?.userGroups) {
userApps = $auth.user?.builder?.global userApps = $auth.user?.builder?.global
? publishedApps ? publishedApps
: publishedApps.filter(app => { : publishedApps.filter(app => {
return userGroups.find(group => { return userGroups.find(group => {
return Object.keys(group.roles).includes(app.appId) return Object.keys(group.roles)
.map(role => apps.extractAppId(role))
.includes(app.appId)
}) })
}) })
} else { } else {
userApps = $auth.user?.builder?.global userApps = $auth.user?.builder?.global
? publishedApps ? publishedApps
: publishedApps.filter(app => : publishedApps.filter(app =>
Object.keys($auth.user?.roles).includes(app.appId) Object.keys($auth.user?.roles)
.map(x => apps.extractAppId(x))
.includes(app.appId)
) )
} }
} }

View File

@ -21,28 +21,33 @@
import { roles } from "stores/backend" import { roles } from "stores/backend"
export let app export let app
$: console.log(app._id)
let assignmentModal let assignmentModal
let appGroups = [] let appGroups = []
let appUsers = [] let appUsers = []
let prevSearch = undefined, let prevSearch = undefined,
search = undefined search = undefined
let pageInfo = createPaginationStore() let pageInfo = createPaginationStore()
let fixedAppId
$: page = $pageInfo.page $: page = $pageInfo.page
$: fetchUsers(page, search) $: fetchUsers(page, search)
$: console.log(app)
$: hasGroupsLicense = $auth.user?.license.features.includes( $: hasGroupsLicense = $auth.user?.license.features.includes(
Constants.Features.USER_GROUPS Constants.Features.USER_GROUPS
) )
$: {
if (!app.tenantId) {
fixedAppId = `app_${app.appId}`
} else {
fixedAppId = `app_${app.tenantId}_${app.appId}`
}
}
$: appUsers = $: appUsers =
$users.data?.filter(x => { $users.data?.filter(x => {
return Object.keys(x.roles).find(y => { return Object.keys(x.roles).find(y => {
return y === app.appId return y === fixedAppId
}) })
}) || [] }) || []
$: appGroups = $groups.filter(x => { $: appGroups = $groups.filter(x => {
return x.apps.find(y => { return x.apps.find(y => {
return y.appId === app.appId return y.appId === app.appId
@ -58,7 +63,7 @@
return group._id === data.id return group._id === data.id
}) })
matchedGroup.apps.push(app) matchedGroup.apps.push(app)
matchedGroup.roles[app.appId] = data.role matchedGroup.roles[fixedAppId] = data.role
groups.actions.save(matchedGroup) groups.actions.save(matchedGroup)
} else if (data.id.startsWith(us_prefix)) { } else if (data.id.startsWith(us_prefix)) {
@ -68,35 +73,36 @@
let newUser = { let newUser = {
...matchedUser, ...matchedUser,
roles: { [app.appId]: data.role, ...matchedUser.roles }, roles: { [fixedAppId]: data.role, ...matchedUser.roles },
} }
await users.save(newUser) await users.save(newUser, { opts: { appId: fixedAppId } })
await fetchUsers(page, search)
} }
}) })
await groups.actions.init() await groups.actions.init()
await users.search({ page, appId: app.appId })
} }
async function removeUser(user) { async function removeUser(user) {
// Remove the user role // Remove the user role
const filteredRoles = { ...user.roles } const filteredRoles = { ...user.roles }
delete filteredRoles[app?.appId] delete filteredRoles[fixedAppId]
await users.save({ await users.save({
...user, ...user,
roles: { roles: {
...filteredRoles, ...filteredRoles,
}, },
}) })
await users.search({ page, appId: app.appId }) await fetchUsers(page, search)
} }
async function removeGroup(group) { async function removeGroup(group) {
// Remove the user role // Remove the user role
let filteredApps = group.apps.filter(
let filteredApps = group.apps.filter(x => x.appId !== app.appId) x => apps.extractAppId(x) !== app.appId
)
const filteredRoles = { ...group.roles } const filteredRoles = { ...group.roles }
delete filteredRoles[app?.appId] delete filteredRoles[fixedAppId]
await groups.actions.save({ await groups.actions.save({
...group, ...group,
@ -104,16 +110,16 @@
roles: { ...filteredRoles }, roles: { ...filteredRoles },
}) })
await users.search({ page, appId: app.appId }) await fetchUsers(page, search)
} }
async function updateUserRole(role, user) { async function updateUserRole(role, user) {
user.roles[app.appId] = role user.roles[fixedAppId] = role
users.save(user) users.save(user)
} }
async function updateGroupRole(role, group) { async function updateGroupRole(role, group) {
group.roles[app.appId] = role group.roles[fixedAppId] = role
groups.actions.save(group) groups.actions.save(group)
} }
@ -129,7 +135,7 @@
prevSearch = search prevSearch = search
try { try {
pageInfo.loading() pageInfo.loading()
await users.search({ page, appId: app.appId }) await users.search({ page, appId: fixedAppId })
pageInfo.fetched($users.hasNextPage, $users.nextPage) pageInfo.fetched($users.hasNextPage, $users.nextPage)
} catch (error) { } catch (error) {
notifications.error("Error getting user list") notifications.error("Error getting user list")
@ -142,7 +148,7 @@
await apps.load() await apps.load()
await roles.fetch() await roles.fetch()
} catch (error) { } catch (error) {
notifications.error("Error") notifications.error(error)
} }
}) })
</script> </script>
@ -174,7 +180,7 @@
autoWidth autoWidth
quiet quiet
value={group.roles[ value={group.roles[
Object.keys(group.roles).find(x => x === app.appId) Object.keys(group.roles).find(x => x === fixedAppId)
]} ]}
/> />
<Icon <Icon
@ -196,7 +202,7 @@
autoWidth autoWidth
quiet quiet
value={user.roles[ value={user.roles[
Object.keys(user.roles).find(x => x === app.appId) Object.keys(user.roles).find(x => x === fixedAppId)
]} ]}
/> />
<Icon <Icon

View File

@ -69,6 +69,8 @@
function addNewInput() { function addNewInput() {
appData = [...appData, { id: "", role: "" }] appData = [...appData, { id: "", role: "" }]
} }
$: console.log(optionSections)
</script> </script>
<ModalContent <ModalContent

View File

@ -38,9 +38,9 @@
return initials == "" ? user.email[0] : initials return initials == "" ? user.email[0] : initials
} }
$: console.log("app_" + app.appId)
onMount(async () => { onMount(async () => {
await users.search({ page: undefined, appId: app.appId }) await users.search({ page: undefined, appId: "app_" + app.appId })
}) })
</script> </script>

View File

@ -4,16 +4,7 @@ import { auth } from "stores/portal"
import { Constants } from "@budibase/frontend-core" import { Constants } from "@budibase/frontend-core"
export function createGroupsStore() { export function createGroupsStore() {
const DEFAULT_CONFIG = { const store = writable([])
name: "",
icon: "",
color: "",
users: [],
apps: [],
roles: {},
}
const store = writable([DEFAULT_CONFIG])
const actions = { const actions = {
init: async () => { init: async () => {

View File

@ -1,5 +1,6 @@
mockAuthWithNoCookie() mockAuthWithNoCookie()
mockWorker() mockWorker()
mockUserGroups()
jest.mock("@budibase/backend-core/db", () => { jest.mock("@budibase/backend-core/db", () => {
const coreDb = jest.requireActual("@budibase/backend-core/db") const coreDb = jest.requireActual("@budibase/backend-core/db")
@ -29,6 +30,16 @@ function mockReset() {
mockWorker() mockWorker()
} }
function mockUserGroups() {
jest.mock("@budibase/pro", () => ({
groups: {
getGroupRoleId: () => {
return "BASIC"
},
},
}))
}
function mockAuthWithNoCookie() { function mockAuthWithNoCookie() {
jest.resetModules() jest.resetModules()
mockWorker() mockWorker()

View File

@ -15,6 +15,7 @@ const { getAppId } = require("@budibase/backend-core/context")
const { groups } = require("@budibase/pro") const { groups } = require("@budibase/pro")
exports.updateAppRole = async (user, { appId } = {}) => { exports.updateAppRole = async (user, { appId } = {}) => {
console.log(appId)
appId = appId || getAppId() appId = appId || getAppId()
if (!user || !user.roles) { if (!user || !user.roles) {
return user return user
@ -37,6 +38,8 @@ exports.updateAppRole = async (user, { appId } = {}) => {
let roleId = await groups.getGroupRoleId(user, appId) let roleId = await groups.getGroupRoleId(user, appId)
user.roleId = roleId user.roleId = roleId
} }
console.log(user.roleId)
delete user.roles delete user.roles
return user return user
} }

View File

@ -31,8 +31,9 @@ export const allUsers = async () => {
export const paginatedUsers = async ({ export const paginatedUsers = async ({
page, page,
search, email,
}: { page?: string; search?: string } = {}) => { appId,
}: { page?: string; email?: string; appId?: string } = {}) => {
const db = tenancy.getGlobalDB() const db = tenancy.getGlobalDB()
// get one extra document, to have the next page // get one extra document, to have the next page
const opts: any = { const opts: any = {
@ -44,19 +45,24 @@ export const paginatedUsers = async ({
opts.startkey = page opts.startkey = page
} }
// property specifies what to use for the page/anchor // property specifies what to use for the page/anchor
let userList, property let userList,
property = "_id",
getKey
if (appId) {
userList = await usersCore.searchGlobalUsersByApp(appId, opts)
getKey = (doc: any) => usersCore.getGlobalUserByAppPage(appId, doc)
} else if (email) {
userList = await usersCore.searchGlobalUsersByEmail(email, opts)
property = "email"
} else {
// no search, query allDocs // no search, query allDocs
if (!search) {
const response = await db.allDocs(dbUtils.getGlobalUserParams(null, opts)) const response = await db.allDocs(dbUtils.getGlobalUserParams(null, opts))
userList = response.rows.map((row: any) => row.doc) userList = response.rows.map((row: any) => row.doc)
property = "_id"
} else {
userList = await usersCore.searchGlobalUsersByEmail(search, opts)
property = "email"
} }
return dbUtils.pagination(userList, PAGE_LIMIT, { return dbUtils.pagination(userList, PAGE_LIMIT, {
paginate: true, paginate: true,
property, property,
getKey,
}) })
} }