Display inherit option

This commit is contained in:
Adria Navarro 2023-09-01 10:52:06 +02:00
parent bdaf179f20
commit 466f1e6132
4 changed files with 53 additions and 10 deletions

View File

@ -11,7 +11,7 @@
let resourcePermissions let resourcePermissions
async function openModal() { async function openModal() {
resourcePermissions = await permissions.forResource(resourceId) resourcePermissions = await permissions.forResourceDetailed(resourceId)
modal.show() modal.show()
} }
</script> </script>

View File

@ -11,6 +11,7 @@
Tag, Tag,
} from "@budibase/bbui" } from "@budibase/bbui"
import { capitalise } from "helpers" import { capitalise } from "helpers"
import { get } from "svelte/store"
export let resourceId export let resourceId
export let permissions export let permissions
@ -18,19 +19,53 @@
async function changePermission(level, role) { async function changePermission(level, role) {
try { try {
if (role === "inherited") {
await permissionsStore.remove({
level,
role,
resource: resourceId,
})
} else {
await permissionsStore.save({ await permissionsStore.save({
level, level,
role, role,
resource: resourceId, resource: resourceId,
}) })
}
// Show updated permissions in UI: REMOVE // Show updated permissions in UI: REMOVE
permissions = await permissionsStore.forResource(resourceId) permissions = await permissionsStore.forResourceDetailed(resourceId)
notifications.success("Updated permissions") notifications.success("Updated permissions")
} catch (error) { } catch (error) {
notifications.error("Error updating permissions") notifications.error("Error updating permissions")
} }
} }
$: computedPermissions = Object.keys(permissions.permissions).reduce(
(p, c) => {
p[c] = {
selected:
permissions.permissionType[c] === "INHERITED"
? "inherited"
: permissions.permissions[c],
options: [...get(roles)],
}
if (permissions.inheritablePermissions) {
p[c].inheritOption = permissions.inheritablePermissions[c]
p[c].options.unshift({
_id: "inherited",
name: `Inherit (${
get(roles).find(
x => x._id === permissions.inheritablePermissions[c]
).name
})`,
})
}
return p
},
{}
)
</script> </script>
<ModalContent showCancelButton={false} confirmText="Done"> <ModalContent showCancelButton={false} confirmText="Done">
@ -51,12 +86,13 @@
<div class="row"> <div class="row">
<Label extraSmall grey>Level</Label> <Label extraSmall grey>Level</Label>
<Label extraSmall grey>Role</Label> <Label extraSmall grey>Role</Label>
{#each Object.keys(permissions) as level} {#each Object.keys(computedPermissions) as level}
<Input value={capitalise(level)} disabled /> <Input value={capitalise(level)} disabled />
<Select <Select
placeholder={false} placeholder={false}
value={computedPermissions[level].selected}
on:change={e => changePermission(level, e.detail)} on:change={e => changePermission(level, e.detail)}
options={$roles} options={computedPermissions[level].options}
getOptionLabel={x => x.name} getOptionLabel={x => x.name}
getOptionValue={x => x._id} getOptionValue={x => x._id}
/> />

View File

@ -23,6 +23,9 @@ export function createPermissionStore() {
forResource: async resourceId => { forResource: async resourceId => {
return (await API.getPermissionForResource(resourceId)).permissions return (await API.getPermissionForResource(resourceId)).permissions
}, },
forResourceDetailed: async resourceId => {
return await API.getPermissionForResource(resourceId)
},
} }
} }

View File

@ -91,7 +91,11 @@ export async function getResourcePerms(
role: roles.getExternalRoleID(role._id!, role.version), role: roles.getExternalRoleID(role._id!, role.version),
type: PermissionSource.EXPLICIT, type: PermissionSource.EXPLICIT,
} }
} else if (permsToInherit && permsToInherit[level]) { } else if (
!permissions[level] &&
permsToInherit &&
permsToInherit[level]
) {
permissions[level] = { permissions[level] = {
role: permsToInherit[level].role, role: permsToInherit[level].role,
type: PermissionSource.INHERITED, type: PermissionSource.INHERITED,