Don't allow duplicate roles

This commit is contained in:
Mel O'Hagan 2022-10-06 19:22:18 +01:00
parent 49117d8595
commit c9f3dc7fe4
1 changed files with 14 additions and 4 deletions

View File

@ -17,12 +17,21 @@
$: selectedRoleId = selectedRole._id $: selectedRoleId = selectedRole._id
$: otherRoles = editableRoles.filter(role => role._id !== selectedRoleId) $: otherRoles = editableRoles.filter(role => role._id !== selectedRoleId)
$: isCreating = selectedRoleId == null || selectedRoleId === "" $: isCreating = selectedRoleId == null || selectedRoleId === ""
$: hasUniqueRoleName = !otherRoles
?.map(role => role.name)
?.includes(selectedRole.name)
$: valid = $: valid =
selectedRole.name && selectedRole.name &&
selectedRole.inherits && selectedRole.inherits &&
selectedRole.permissionId && selectedRole.permissionId &&
!builtInRoles.includes(selectedRole.name) !builtInRoles.includes(selectedRole.name)
$: shouldDisableRoleInput =
builtInRoles.includes(selectedRole.name) &&
selectedRole.name?.toLowerCase() === selectedRoleId?.toLowerCase()
const fetchBasePermissions = async () => { const fetchBasePermissions = async () => {
try { try {
basePermissions = await API.getBasePermissions() basePermissions = await API.getBasePermissions()
@ -99,7 +108,7 @@
title="Edit Roles" title="Edit Roles"
confirmText={isCreating ? "Create" : "Save"} confirmText={isCreating ? "Create" : "Save"}
onConfirm={saveRole} onConfirm={saveRole}
disabled={!valid} disabled={!valid || !hasUniqueRoleName}
> >
{#if errors.length} {#if errors.length}
<ErrorsBox {errors} /> <ErrorsBox {errors} />
@ -119,7 +128,8 @@
<Input <Input
label="Name" label="Name"
bind:value={selectedRole.name} bind:value={selectedRole.name}
disabled={builtInRoles.includes(selectedRole.name)} disabled={shouldDisableRoleInput}
error={!hasUniqueRoleName ? "Select a unique role name." : null}
/> />
<Select <Select
label="Inherits Role" label="Inherits Role"
@ -127,7 +137,7 @@
options={selectedRole._id === "BASIC" ? $roles : otherRoles} options={selectedRole._id === "BASIC" ? $roles : otherRoles}
getOptionValue={role => role._id} getOptionValue={role => role._id}
getOptionLabel={role => role.name} getOptionLabel={role => role.name}
disabled={builtInRoles.includes(selectedRole.name)} disabled={shouldDisableRoleInput}
/> />
<Select <Select
label="Base Permissions" label="Base Permissions"
@ -135,7 +145,7 @@
options={basePermissions} options={basePermissions}
getOptionValue={x => x._id} getOptionValue={x => x._id}
getOptionLabel={x => x.name} getOptionLabel={x => x.name}
disabled={builtInRoles.includes(selectedRole.name)} disabled={shouldDisableRoleInput}
/> />
{/if} {/if}
<div slot="footer"> <div slot="footer">