Update other usages of roles to use new metadata

This commit is contained in:
Andrew Kingston 2024-09-11 16:51:36 +01:00
parent dc0f93e314
commit 6419829484
No known key found for this signature in database
4 changed files with 3 additions and 187 deletions

View File

@ -185,13 +185,6 @@
{#if dependantsInfoMessage}
<InfoDisplay info body={dependantsInfoMessage} />
{/if}
<div class="buttons">
<EditRolesButton
on:show={() => (showPopover = false)}
on:hide={() => (showPopover = true)}
/>
</div>
</DetailPopover>
<style>

View File

@ -125,7 +125,7 @@
label="Role"
bind:value={row.roleId}
options={$roles}
getOptionLabel={role => role.name}
getOptionLabel={role => role.displayName}
getOptionValue={role => role._id}
disabled={!creating}
/>

View File

@ -1,177 +0,0 @@
<script>
import {
keepOpen,
ModalContent,
Select,
Input,
Button,
notifications,
} from "@budibase/bbui"
import { onMount } from "svelte"
import { API } from "api"
import ErrorsBox from "components/common/ErrorsBox.svelte"
import { roles } from "stores/builder"
const BASE_ROLE = { _id: "", inherits: "BASIC", permissionId: "write" }
let basePermissions = []
let selectedRole = BASE_ROLE
let errors = []
let builtInRoles = ["App admin", "App power user", "App user", "Public user"]
let validRegex = /^[a-zA-Z0-9_]*$/
// Don't allow editing of public role
$: editableRoles = $roles.filter(role => role._id !== "PUBLIC")
$: selectedRoleId = selectedRole._id
$: otherRoles = editableRoles.filter(role => role._id !== selectedRoleId)
$: isCreating = selectedRoleId == null || selectedRoleId === ""
$: roleNameError = getRoleNameError(selectedRole.name)
$: valid =
selectedRole.name &&
selectedRole.inherits &&
selectedRole.permissionId &&
!builtInRoles.includes(selectedRole.name)
$: shouldDisableRoleInput =
builtInRoles.includes(selectedRole.name) &&
selectedRole.name?.toLowerCase() === selectedRoleId?.toLowerCase()
const fetchBasePermissions = async () => {
try {
basePermissions = await API.getBasePermissions()
} catch (error) {
notifications.error("Error fetching base permission options")
basePermissions = []
}
}
// Changes the selected role
const changeRole = event => {
const id = event?.detail
const role = $roles.find(role => role._id === id)
if (role) {
selectedRole = {
...role,
inherits: role.inherits ?? "",
permissionId: role.permissionId ?? "",
}
} else {
selectedRole = BASE_ROLE
}
errors = []
}
// Saves or creates the selected role
const saveRole = async () => {
errors = []
// Clean up empty strings
const keys = ["_id", "inherits", "permissionId"]
keys.forEach(key => {
if (selectedRole[key] === "") {
delete selectedRole[key]
}
})
// Validation
if (!selectedRole.name || selectedRole.name.trim() === "") {
errors.push({ message: "Please enter a role name" })
}
if (!selectedRole.permissionId) {
errors.push({ message: "Please choose permissions" })
}
if (errors.length) {
return keepOpen
}
// Save/create the role
try {
await roles.save(selectedRole)
notifications.success("Role saved successfully")
} catch (error) {
notifications.error(`Error saving role - ${error.message}`)
return keepOpen
}
}
// Deletes the selected role
const deleteRole = async () => {
try {
await roles.delete(selectedRole)
changeRole()
notifications.success("Role deleted successfully")
} catch (error) {
notifications.error(`Error deleting role - ${error.message}`)
return false
}
}
const getRoleNameError = name => {
if (builtInRoles.includes(name)) {
return null
}
const hasUniqueRoleName = !otherRoles
?.map(role => role.name)
?.includes(name)
const invalidRoleName = !validRegex.test(name)
if (!hasUniqueRoleName) {
return "Select a unique role name."
} else if (invalidRoleName) {
return "Please enter a role name consisting of only alphanumeric symbols and underscores"
}
}
onMount(fetchBasePermissions)
</script>
<ModalContent
title="Edit Roles"
confirmText={isCreating ? "Create" : "Save"}
onConfirm={saveRole}
disabled={!valid || roleNameError}
>
{#if errors.length}
<ErrorsBox {errors} />
{/if}
<Select
thin
secondary
label="Role"
value={selectedRoleId}
on:change={changeRole}
options={editableRoles}
placeholder="Create new role"
getOptionValue={role => role._id}
getOptionLabel={role => role.name}
/>
{#if selectedRole}
<Input
label="Name"
bind:value={selectedRole.name}
disabled={!!selectedRoleId}
error={roleNameError}
/>
<Select
label="Inherits Role"
bind:value={selectedRole.inherits}
options={selectedRole._id === "BASIC" ? $roles : otherRoles}
getOptionValue={role => role._id}
getOptionLabel={role => role.name}
disabled={shouldDisableRoleInput}
/>
<Select
label="Base Permissions"
bind:value={selectedRole.permissionId}
options={basePermissions}
getOptionValue={x => x._id}
getOptionLabel={x => x.name}
disabled={shouldDisableRoleInput}
/>
{/if}
<div slot="footer">
{#if !isCreating && !builtInRoles.includes(selectedRole.name)}
<Button warning on:click={deleteRole}>Delete</Button>
{/if}
</div>
</ModalContent>

View File

@ -725,10 +725,10 @@ const getRoleBindings = () => {
return {
type: "context",
runtimeBinding: `'${role._id}'`,
readableBinding: `Role.${role.name}`,
readableBinding: `Role.${role.displayName}`,
category: "Role",
icon: "UserGroup",
display: { type: "string", name: role.name },
display: { type: "string", name: role.displayName },
}
})
}