Switching over app role assignment to use the new backend rather than being performed in the frontend.

This commit is contained in:
mike12345567 2022-09-21 22:58:06 +01:00
parent 39689d27f6
commit 2c5d2f7b12
7 changed files with 53 additions and 40 deletions

View File

@ -41,7 +41,7 @@
$: fetchUsers(page, searchTerm) $: fetchUsers(page, searchTerm)
$: group = $groups.find(x => x._id === groupId) $: group = $groups.find(x => x._id === groupId)
$: filtered = $users.data $: 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) { if (loaded && !group?._id) {
$goto("./") $goto("./")

View File

@ -35,13 +35,16 @@
}) })
let assignmentModal let assignmentModal
let appGroups = [] let appGroups
let appUsers = [] let appUsers
$: fixedAppId = apps.getProdAppID(app.devId) $: fixedAppId = apps.getProdAppID(app.devId)
$: appUsers = $usersFetch.rows $: appUsers = $usersFetch.rows
$: appGroups = $groups.filter(x => { $: appGroups = $groups.filter(group => {
return x.apps.includes(app.appId) if (!group.roles) {
return false
}
return Object.keys(group.roles).includes(fixedAppId)
}) })
async function removeUser(user) { async function removeUser(user) {
@ -58,16 +61,8 @@
} }
async function removeGroup(group) { async function removeGroup(group) {
const filteredApps = group.apps.filter( await groups.actions.removeApp(group._id, fixedAppId)
x => apps.extractAppId(x) !== app.appId await groups.actions.init()
)
const filteredRoles = { ...group.roles }
delete filteredRoles[fixedAppId]
await groups.actions.save({
...group,
apps: filteredApps,
roles: { ...filteredRoles },
})
await usersFetch.refresh() await usersFetch.refresh()
} }
@ -77,8 +72,8 @@
} }
async function updateGroupRole(role, group) { async function updateGroupRole(role, group) {
group.roles[fixedAppId] = role await groups.actions.addApp(group._id, fixedAppId, role)
await groups.actions.save(group) await usersFetch.refresh()
} }
onMount(async () => { onMount(async () => {
@ -99,10 +94,10 @@
<Heading>Access</Heading> <Heading>Access</Heading>
<div class="subtitle"> <div class="subtitle">
<Body size="S"> <Body size="S">
Assign users to your app and define their access here Assign users/groups to your app and define their access here
</Body> </Body>
<Button on:click={assignmentModal.show} icon="User" cta> <Button on:click={assignmentModal.show} icon="User" cta>
Assign users Assign access
</Button> </Button>
</div> </div>
</div> </div>
@ -173,7 +168,7 @@
<Heading>No users assigned</Heading> <Heading>No users assigned</Heading>
<div class="opacity"> <div class="opacity">
<Body size="S"> <Body size="S">
Assign users to your app and set their access here Assign users/groups to your app and set their access here
</Body> </Body>
</div> </div>
<div class="padding"> <div class="padding">
@ -182,7 +177,7 @@
cta cta
icon="UserArrow" icon="UserArrow"
> >
Assign Users Assign access
</Button> </Button>
</div> </div>
</Layout> </Layout>

View File

@ -72,14 +72,7 @@
if (!group) { if (!group) {
continue continue
} }
await groups.actions.save({ await groups.actions.addApp(group._id, fixedAppId, data.role)
...group,
apps: [...group.apps, app.appId],
roles: {
...group.roles,
[fixedAppId]: data.role,
},
})
} }
// Assign user // Assign user
else if (data.id.startsWith(us_prefix)) { else if (data.id.startsWith(us_prefix)) {
@ -96,7 +89,6 @@
// Refresh data when completed // Refresh data when completed
await usersFetch.refresh() await usersFetch.refresh()
await groups.actions.init()
dispatch("update") dispatch("update")
} }
@ -121,7 +113,8 @@
search = search?.toLowerCase() search = search?.toLowerCase()
return (allGroups || []).filter(group => { return (allGroups || []).filter(group => {
// Filter out assigned groups // Filter out assigned groups
if (group.apps.includes(appId)) { const appIds = Object.keys(group.roles || {})
if (appIds.includes(appId)) {
return false return false
} }

View File

@ -64,6 +64,16 @@ export function createGroupsStore() {
// refresh the group enrichment // refresh the group enrichment
await getGroup(groupId) 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 { return {

View File

@ -73,19 +73,19 @@ export const buildGroupsEndpoints = API => {
/** /**
* Adds apps to a group * Adds apps to a group
* @param groupId The group to update * @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) => { addAppsToGroup: async (groupId, appArray) => {
return updateGroupResource(groupId, "apps", "add", appIds) return updateGroupResource(groupId, "apps", "add", appArray)
}, },
/** /**
* Removes apps from a group * Removes apps from a group
* @param groupId The group to update * @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) => { removeAppsFromGroup: async (groupId, appArray) => {
return updateGroupResource(groupId, "apps", "remove", appIds) return updateGroupResource(groupId, "apps", "remove", appArray)
}, },
} }
} }

View File

@ -89,7 +89,7 @@ export const buildUserEndpoints = API => ({
* @param groups the array of group ids to add all users to * @param groups the array of group ids to add all users to
*/ */
createUsers: async ({ users, groups }) => { createUsers: async ({ users, groups }) => {
return await API.post({ const res = await API.post({
url: "/api/global/users/bulk", url: "/api/global/users/bulk",
body: { body: {
create: { 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 * @param userIds the ID of the user to delete
*/ */
deleteUsers: async userIds => { deleteUsers: async userIds => {
return await API.post({ const res = await API.post({
url: `/api/global/users/bulk`, url: `/api/global/users/bulk`,
body: { body: {
delete: { delete: {
@ -123,6 +125,7 @@ export const buildUserEndpoints = API => ({
}, },
}, },
}) })
return res.deleted
}, },
/** /**

View File

@ -17,6 +17,7 @@ import {
tenancy, tenancy,
} from "@budibase/backend-core" } from "@budibase/backend-core"
import { checkAnyUserExists } from "../../../utilities/users" import { checkAnyUserExists } from "../../../utilities/users"
import { groups } from "@budibase/pro"
const MAX_USERS_UPLOAD_LIMIT = 1000 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." "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) => { export const bulkUpdate = async (ctx: any) => {