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 09dc9aa297..d97d4cddc3 100644 --- a/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte +++ b/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte @@ -41,7 +41,7 @@ $: fetchUsers(page, searchTerm) $: group = $groups.find(x => x._id === groupId) $: filtered = $users.data - $: groupApps = $apps.filter(x => group?.apps.includes(x.appId)) + $: groupApps = $apps.filter(app => group?.roles?.includes(app.appId)) $: { if (loaded && !group?._id) { $goto("./") diff --git a/packages/builder/src/pages/builder/portal/overview/_components/AccessTab.svelte b/packages/builder/src/pages/builder/portal/overview/_components/AccessTab.svelte index 2329a796bc..2014c3e2fe 100644 --- a/packages/builder/src/pages/builder/portal/overview/_components/AccessTab.svelte +++ b/packages/builder/src/pages/builder/portal/overview/_components/AccessTab.svelte @@ -35,13 +35,16 @@ }) let assignmentModal - let appGroups = [] - let appUsers = [] + let appGroups + let appUsers $: fixedAppId = apps.getProdAppID(app.devId) $: appUsers = $usersFetch.rows - $: appGroups = $groups.filter(x => { - return x.apps.includes(app.appId) + $: appGroups = $groups.filter(group => { + if (!group.roles) { + return false + } + return Object.keys(group.roles).includes(fixedAppId) }) async function removeUser(user) { @@ -58,16 +61,8 @@ } async function removeGroup(group) { - const filteredApps = group.apps.filter( - x => apps.extractAppId(x) !== app.appId - ) - const filteredRoles = { ...group.roles } - delete filteredRoles[fixedAppId] - await groups.actions.save({ - ...group, - apps: filteredApps, - roles: { ...filteredRoles }, - }) + await groups.actions.removeApp(group._id, fixedAppId) + await groups.actions.init() await usersFetch.refresh() } @@ -77,8 +72,8 @@ } async function updateGroupRole(role, group) { - group.roles[fixedAppId] = role - await groups.actions.save(group) + await groups.actions.addApp(group._id, fixedAppId, role) + await usersFetch.refresh() } onMount(async () => { @@ -99,10 +94,10 @@ Access
- Assign users to your app and define their access here + Assign users/groups to your app and define their access here
@@ -173,7 +168,7 @@ No users assigned
- Assign users to your app and set their access here + Assign users/groups to your app and set their access here
@@ -182,7 +177,7 @@ cta icon="UserArrow" > - Assign Users + Assign access
diff --git a/packages/builder/src/pages/builder/portal/overview/_components/AssignmentModal.svelte b/packages/builder/src/pages/builder/portal/overview/_components/AssignmentModal.svelte index bffb9317e1..0b969073f3 100644 --- a/packages/builder/src/pages/builder/portal/overview/_components/AssignmentModal.svelte +++ b/packages/builder/src/pages/builder/portal/overview/_components/AssignmentModal.svelte @@ -72,14 +72,7 @@ if (!group) { continue } - await groups.actions.save({ - ...group, - apps: [...group.apps, app.appId], - roles: { - ...group.roles, - [fixedAppId]: data.role, - }, - }) + await groups.actions.addApp(group._id, fixedAppId, data.role) } // Assign user else if (data.id.startsWith(us_prefix)) { @@ -96,7 +89,6 @@ // Refresh data when completed await usersFetch.refresh() - await groups.actions.init() dispatch("update") } @@ -121,7 +113,8 @@ search = search?.toLowerCase() return (allGroups || []).filter(group => { // Filter out assigned groups - if (group.apps.includes(appId)) { + const appIds = Object.keys(group.roles || {}) + if (appIds.includes(appId)) { return false } diff --git a/packages/builder/src/stores/portal/groups.js b/packages/builder/src/stores/portal/groups.js index ac7147f26e..656b77f082 100644 --- a/packages/builder/src/stores/portal/groups.js +++ b/packages/builder/src/stores/portal/groups.js @@ -64,6 +64,16 @@ export function createGroupsStore() { // refresh the group enrichment await getGroup(groupId) }, + + addApp: async (groupId, appId, roleId) => { + await API.addAppsToGroup(groupId, [{ appId, roleId }]) + await getGroup(groupId) + }, + + removeApp: async (groupId, appId) => { + await API.removeAppsFromGroup(groupId, [{ appId }]) + await getGroup(groupId) + }, } return { diff --git a/packages/frontend-core/src/api/groups.js b/packages/frontend-core/src/api/groups.js index 10be473ee1..c27f11e0ea 100644 --- a/packages/frontend-core/src/api/groups.js +++ b/packages/frontend-core/src/api/groups.js @@ -73,19 +73,19 @@ export const buildGroupsEndpoints = API => { /** * Adds apps to a group * @param groupId The group to update - * @param appIds The app IDs to be added + * @param appArray Array of objects, containing the appId and roleId to be added */ - addAppsToGroup: async (groupId, appIds) => { - return updateGroupResource(groupId, "apps", "add", appIds) + addAppsToGroup: async (groupId, appArray) => { + return updateGroupResource(groupId, "apps", "add", appArray) }, /** * Removes apps from a group * @param groupId The group to update - * @param appIds The app IDs to be removed + * @param appArray Array of objects, containing the appId to be removed */ - removeAppsFromGroup: async (groupId, appIds) => { - return updateGroupResource(groupId, "apps", "remove", appIds) + removeAppsFromGroup: async (groupId, appArray) => { + return updateGroupResource(groupId, "apps", "remove", appArray) }, } } diff --git a/packages/frontend-core/src/api/user.js b/packages/frontend-core/src/api/user.js index 6ba801da34..9a02ef757c 100644 --- a/packages/frontend-core/src/api/user.js +++ b/packages/frontend-core/src/api/user.js @@ -89,7 +89,7 @@ export const buildUserEndpoints = API => ({ * @param groups the array of group ids to add all users to */ createUsers: async ({ users, groups }) => { - return await API.post({ + const res = await API.post({ url: "/api/global/users/bulk", body: { create: { @@ -98,6 +98,8 @@ export const buildUserEndpoints = API => ({ }, }, }) + console.log(res) + return res.created }, /** @@ -115,7 +117,7 @@ export const buildUserEndpoints = API => ({ * @param userIds the ID of the user to delete */ deleteUsers: async userIds => { - return await API.post({ + const res = await API.post({ url: `/api/global/users/bulk`, body: { delete: { @@ -123,6 +125,7 @@ export const buildUserEndpoints = API => ({ }, }, }) + return res.deleted }, /** diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 77d44ef4d2..1914ea15ee 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -17,6 +17,7 @@ import { tenancy, } from "@budibase/backend-core" import { checkAnyUserExists } from "../../../utilities/users" +import { groups } from "@budibase/pro" const MAX_USERS_UPLOAD_LIMIT = 1000 @@ -41,7 +42,18 @@ const bulkCreate = async (users: User[], groupIds: string[]) => { "Max limit for upload is 1000 users. Please reduce file size and try again." ) } - return await userSdk.bulkCreate(users, groupIds) + const created = await userSdk.bulkCreate(users, groupIds) + const success = created?.successful + // now update the groups + if (Array.isArray(success) && groupIds) { + const groupPromises = [] + const createdUserIds = success.map(user => user._id) + for (let groupId of groupIds) { + groupPromises.push(groups.addUsers(groupId, createdUserIds)) + } + await Promise.all(groupPromises) + } + return created } export const bulkUpdate = async (ctx: any) => {