1
0
Fork 0
mirror of synced 2024-06-23 08:30:31 +12:00

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)
$: 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("./")

View file

@ -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 @@
<Heading>Access</Heading>
<div class="subtitle">
<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>
<Button on:click={assignmentModal.show} icon="User" cta>
Assign users
Assign access
</Button>
</div>
</div>
@ -173,7 +168,7 @@
<Heading>No users assigned</Heading>
<div class="opacity">
<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>
</div>
<div class="padding">
@ -182,7 +177,7 @@
cta
icon="UserArrow"
>
Assign Users
Assign access
</Button>
</div>
</Layout>

View file

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

View file

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

View file

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

View file

@ -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
},
/**

View file

@ -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) => {