From 45120c60ba364fdca13d81ae1b42cea194bb7573 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 21 Jun 2023 17:08:54 +0100 Subject: [PATCH 1/7] Getting basic regex in place to make sure role names can be IDs. --- .../backend/DataTable/modals/EditRoles.svelte | 23 +++++++++++++++---- .../server/src/api/routes/utils/validators.ts | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/EditRoles.svelte b/packages/builder/src/components/backend/DataTable/modals/EditRoles.svelte index 600e331d3e..3f976b40f2 100644 --- a/packages/builder/src/components/backend/DataTable/modals/EditRoles.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/EditRoles.svelte @@ -12,15 +12,14 @@ let selectedRole = BASE_ROLE let errors = [] let builtInRoles = ["Admin", "Power", "Basic", "Public"] + 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 === "" - $: hasUniqueRoleName = !otherRoles - ?.map(role => role.name) - ?.includes(selectedRole.name) + $: roleNameError = getRoleNameError(selectedRole.name) $: valid = selectedRole.name && @@ -101,6 +100,20 @@ } } + const getRoleNameError = name => { + 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" + } else { + return null + } + } + onMount(fetchBasePermissions) @@ -108,7 +121,7 @@ title="Edit Roles" confirmText={isCreating ? "Create" : "Save"} onConfirm={saveRole} - disabled={!valid || !hasUniqueRoleName} + disabled={!valid || roleNameError} > {#if errors.length} @@ -129,7 +142,7 @@ label="Name" bind:value={selectedRole.name} disabled={shouldDisableRoleInput} - error={!hasUniqueRoleName ? "Select a unique role name." : null} + error={roleNameError} />