Quick fix to stop custom role names from being updated.

This commit is contained in:
mike12345567 2023-06-28 17:17:24 +01:00
parent d8fae3a348
commit 81c8bee81e
2 changed files with 19 additions and 13 deletions

View File

@ -84,7 +84,7 @@
await roles.save(selectedRole) await roles.save(selectedRole)
notifications.success("Role saved successfully") notifications.success("Role saved successfully")
} catch (error) { } catch (error) {
notifications.error("Error saving role") notifications.error(`Error deleting role - ${error.message}`)
return false return false
} }
} }
@ -96,7 +96,8 @@
changeRole() changeRole()
notifications.success("Role deleted successfully") notifications.success("Role deleted successfully")
} catch (error) { } catch (error) {
notifications.error("Error deleting role") notifications.error(`Error deleting role - ${error.message}`)
return false
} }
} }
@ -139,7 +140,7 @@
<Input <Input
label="Name" label="Name"
bind:value={selectedRole.name} bind:value={selectedRole.name}
disabled={shouldDisableRoleInput} disabled={!!selectedRoleId}
error={roleNameError} error={roleNameError}
/> />
<Select <Select

View File

@ -1,9 +1,5 @@
import { roles, context, events } from "@budibase/backend-core" import { roles, context, events, db as dbCore } from "@budibase/backend-core"
import { import { getUserMetadataParams, InternalTables } from "../../db/utils"
generateRoleID,
getUserMetadataParams,
InternalTables,
} from "../../db/utils"
import { UserCtx, Database } from "@budibase/types" import { UserCtx, Database } from "@budibase/types"
const UpdateRolesOptions = { const UpdateRolesOptions = {
@ -55,6 +51,7 @@ export async function save(ctx: UserCtx) {
const db = context.getAppDB() const db = context.getAppDB()
let { _id, name, inherits, permissionId, version } = ctx.request.body let { _id, name, inherits, permissionId, version } = ctx.request.body
let isCreate = false let isCreate = false
const isNewVersion = version === roles.RoleIDVersion.NAME
if (_id && roles.isBuiltin(_id)) { if (_id && roles.isBuiltin(_id)) {
ctx.throw(400, "Cannot update builtin roles.") ctx.throw(400, "Cannot update builtin roles.")
@ -62,12 +59,20 @@ export async function save(ctx: UserCtx) {
// if not id found, then its creation // if not id found, then its creation
if (!_id) { if (!_id) {
_id = generateRoleID(name) _id = dbCore.generateRoleID(name)
isCreate = true isCreate = true
} }
// version 2 roles need updated to add back role_ // version 2 roles need updated to add back role_
else if (version === roles.RoleIDVersion.NAME) { else if (isNewVersion) {
_id = generateRoleID(name) _id = dbCore.prefixRoleID(_id)
}
let dbRole
if (!isCreate) {
dbRole = await db.get(_id)
}
if (dbRole && dbRole.name !== name && isNewVersion) {
ctx.throw(400, "Cannot change custom role name")
} }
const role = new roles.Role(_id, name, permissionId).addInheritance(inherits) const role = new roles.Role(_id, name, permissionId).addInheritance(inherits)
@ -98,7 +103,7 @@ export async function destroy(ctx: UserCtx) {
ctx.throw(400, "Cannot delete builtin role.") ctx.throw(400, "Cannot delete builtin role.")
} else { } else {
// make sure has the prefix (if it has it then it won't be added) // make sure has the prefix (if it has it then it won't be added)
roleId = generateRoleID(roleId) roleId = dbCore.generateRoleID(roleId)
} }
const role = await db.get(roleId) const role = await db.get(roleId)
// first check no users actively attached to role // first check no users actively attached to role