Updates to use new backend API for adding/removing users from groups.

This commit is contained in:
mike12345567 2022-09-21 16:58:47 +01:00
parent 115a0c146b
commit 4d9de7f658
2 changed files with 115 additions and 78 deletions

View File

@ -1,13 +1,30 @@
import { writable, get } from "svelte/store" import { writable, get } from "svelte/store"
import { API } from "api" import { API } from "api"
import { auth, users } from "stores/portal" import { auth } from "stores/portal"
export function createGroupsStore() { export function createGroupsStore() {
const store = writable([]) const store = writable([])
const updateStore = group => {
store.update(state => {
const currentIdx = state.findIndex(gr => gr._id === group._id)
if (currentIdx >= 0) {
state.splice(currentIdx, 1, group)
} else {
state.push(group)
}
return state
})
}
const getGroup = async groupId => {
const group = await API.getGroup(groupId)
updateStore(group)
}
const actions = { const actions = {
init: async () => { init: async () => {
// only init if these is a groups license, just to be sure but the feature will be blocked // only init if there is a groups license, just to be sure but the feature will be blocked
// on the backend anyway // on the backend anyway
if (get(auth).groupsEnabled) { if (get(auth).groupsEnabled) {
const groups = await API.getGroups() const groups = await API.getGroups()
@ -15,19 +32,13 @@ export function createGroupsStore() {
} }
}, },
get: getGroup,
save: async group => { save: async group => {
const response = await API.saveGroup(group) const response = await API.saveGroup(group)
group._id = response._id group._id = response._id
group._rev = response._rev group._rev = response._rev
store.update(state => { updateStore(group)
const currentIdx = state.findIndex(gr => gr._id === response._id)
if (currentIdx >= 0) {
state.splice(currentIdx, 1, group)
} else {
state.push(group)
}
return state
})
return group return group
}, },
@ -43,40 +54,15 @@ export function createGroupsStore() {
}, },
addUser: async (groupId, userId) => { addUser: async (groupId, userId) => {
// Sanity check await API.addUsersToGroup(groupId, userId)
const user = await users.get(userId) // refresh the group enrichment
const group = get(store).find(x => x._id === groupId) await getGroup(groupId)
if (!group?._id || !user?._id) {
return
}
// Check we haven't already been added
if (group.users?.find(x => x._id === userId)) {
return
}
// Update user
let userGroups = user.userGroups || []
userGroups.push(groupId)
await users.save({
...user,
userGroups,
})
}, },
removeUser: async (groupId, userId) => { removeUser: async (groupId, userId) => {
// Sanity check await API.removeUsersFromGroup(groupId, userId)
const user = await users.get(userId) // refresh the group enrichment
const group = get(store).find(x => x._id === groupId) await getGroup(groupId)
if (!group?._id || !user?._id) {
return
}
// Update user
await users.save({
...user,
userGroups: user.userGroups.filter(x => x !== groupId),
})
}, },
} }

View File

@ -1,40 +1,91 @@
export const buildGroupsEndpoints = API => ({ export const buildGroupsEndpoints = API => {
/** // underlying functionality of adding/removing users/apps to groups
* Creates a user group. async function updateGroupResource(groupId, resource, operation, ids) {
* @param user the new group to create if (!Array.isArray(ids)) {
*/ ids = [ids]
saveGroup: async group => { }
return await API.post({ return await API.post({
url: "/api/global/groups", url: `/api/global/groups/${groupId}/${resource}`,
body: group, body: {
[operation]: ids,
},
}) })
}, }
/**
* Gets all of the user groups
*/
getGroups: async () => {
return await API.get({
url: "/api/global/groups",
})
},
/** return {
* Gets a group by ID /**
*/ * Creates a user group.
getGroup: async id => { * @param group the new group to create
return await API.get({ */
url: `/api/global/groups/${id}`, saveGroup: async group => {
}) return await API.post({
}, url: "/api/global/groups",
body: group,
})
},
/**
* Gets all the user groups
*/
getGroups: async () => {
return await API.get({
url: "/api/global/groups",
})
},
/** /**
* Deletes a user group * Gets a group by ID
* @param id the id of the config to delete */
* @param rev the revision of the config to delete getGroup: async id => {
*/ return await API.get({
deleteGroup: async ({ id, rev }) => { url: `/api/global/groups/${id}`,
return await API.delete({ })
url: `/api/global/groups/${id}/${rev}`, },
})
}, /**
}) * Deletes a user group
* @param id the id of the config to delete
* @param rev the revision of the config to delete
*/
deleteGroup: async ({ id, rev }) => {
return await API.delete({
url: `/api/global/groups/${id}/${rev}`,
})
},
/**
* Adds users to a group
* @param groupId The group to update
* @param userIds The user IDs to be added
*/
addUsersToGroup: async (groupId, userIds) => {
return updateGroupResource(groupId, "users", "add", userIds)
},
/**
* Removes users from a group
* @param groupId The group to update
* @param userIds The user IDs to be removed
*/
removeUsersFromGroup: async (groupId, userIds) => {
return updateGroupResource(groupId, "users", "remove", userIds)
},
/**
* Adds apps to a group
* @param groupId The group to update
* @param appIds The app IDs to be added
*/
addAppsToGroup: async (groupId, appIds) => {
return updateGroupResource(groupId, "apps", "add", appIds)
},
/**
* Removes apps from a group
* @param groupId The group to update
* @param appIds The app IDs to be removed
*/
removeAppsFromGroup: async (groupId, appIds) => {
return updateGroupResource(groupId, "apps", "remove", appIds)
},
}
}