fix(frontend): resolve latest value for high-risk settings

Reused setting-row controls can close over a stale item after save.
Look up the current row by key for auth.registration_mode as well so
cancel and persist use the last saved value, not the first-loaded one.
main
wizardchen 2026-09-01 10:58:09 +08:00 committed by lyingbug
parent 02ac3e794e
commit bf53cacf75
3 changed files with 73 additions and 15 deletions

View File

@ -496,6 +496,7 @@ import {
type SystemSettingItem,
} from '@/api/system'
import { useAuthStore } from '@/stores/auth'
import { isSettingValueDirty, resolveCurrentSetting } from './systemSettingsEdit'
const authStore = useAuthStore()
const currentUserId = computed(() => authStore.currentUserId)
@ -866,16 +867,7 @@ async function snapSsrfWhitelistToSaved(item: SystemSettingItem) {
}
function isDirty(item: SystemSettingItem): boolean {
const cur = editValues[item.key]
const orig = item.value
if (Array.isArray(cur) && Array.isArray(orig)) {
if (cur.length !== orig.length) return true
for (let i = 0; i < cur.length; i++) {
if (cur[i] !== orig[i]) return true
}
return false
}
return cur !== orig
return isSettingValueDirty(editValues[item.key], item.value)
}
function formatDate(isoString: string): string {
@ -929,7 +921,7 @@ async function loadSettings() {
// onChange persists non-SSRF settings. SSRF whitelist and system admins
// have dedicated handlers with inline popconfirm.
async function onChange(item: SystemSettingItem) {
const currentItem = settingsByKey.value.get(item.key)
const currentItem = resolveCurrentSetting(settingsByKey.value, item.key)
if (!currentItem) return
if (!isDirty(currentItem)) return
@ -943,15 +935,17 @@ async function onChange(item: SystemSettingItem) {
}
async function onHighRiskSelectChange(item: SystemSettingItem) {
const currentItem = resolveCurrentSetting(settingsByKey.value, item.key)
if (!currentItem) return
const newValue = editValues[item.key]
if (newValue === item.value) return
if (!isDirty(currentItem)) return
// Revert the select immediately so cancel leaves the saved value
// visible; re-apply only after the inline popconfirm is confirmed.
editValues[item.key] = item.value
editValues[item.key] = currentItem.value
const ok = await highRiskPopconfirm.ask({
content: highRiskConfirmBody(item, newValue),
content: highRiskConfirmBody(currentItem, newValue),
theme: 'danger',
confirmBtn: {
content: t('system.globalSettings.confirm.confirmBtn'),
@ -961,7 +955,7 @@ async function onHighRiskSelectChange(item: SystemSettingItem) {
if (!ok) return
editValues[item.key] = newValue
await persistSetting(item)
await persistSetting(currentItem)
}
function confirmSsrfListEntryChange(

View File

@ -0,0 +1,41 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { isSettingValueDirty, resolveCurrentSetting } from './systemSettingsEdit.ts'
test('treats scalar edits as dirty', () => {
assert.equal(isSettingValueDirty(true, false), true)
assert.equal(isSettingValueDirty(false, false), false)
assert.equal(isSettingValueDirty('invite', 'open'), true)
})
test('treats list edits as dirty by value, not by reference', () => {
assert.equal(isSettingValueDirty(['a'], ['a']), false)
assert.equal(isSettingValueDirty(['a', 'b'], ['a']), true)
assert.equal(isSettingValueDirty(['a'], ['b']), true)
})
test('resolves the latest setting so a reused row does not keep the pre-save value', () => {
const stale = { key: 'tenant.auto_create_api_key', value: false }
const current = { key: 'tenant.auto_create_api_key', value: true }
const settingsByKey = new Map([[current.key, current]])
const resolved = resolveCurrentSetting(settingsByKey, stale.key)
assert.equal(resolved, current)
// User toggled back to the original loaded value. The stale object looks
// clean; the canonical post-save object is dirty and must PUT.
const editValue = false
assert.equal(isSettingValueDirty(editValue, stale.value), false)
assert.equal(isSettingValueDirty(editValue, resolved?.value), true)
})
test('high-risk cancel restores the latest saved value, not the first-loaded object', () => {
const stale = { key: 'auth.registration_mode', value: 'open' }
const current = { key: 'auth.registration_mode', value: 'invite' }
const settingsByKey = new Map([[current.key, current]])
const resolved = resolveCurrentSetting(settingsByKey, stale.key)
assert.equal(resolved?.value, 'invite')
assert.notEqual(stale.value, resolved?.value)
})

View File

@ -0,0 +1,23 @@
// Setting rows keep a stable Vue key (`item.key`) while persistSetting
// replaces the object in `settings`. Reused TDesign controls can therefore
// fire @change/@blur with a stale item whose `.value` is the pre-save
// snapshot. Event handlers must resolve by key from the latest map before
// dirty-checking or reverting the control.
export function resolveCurrentSetting<T extends { key: string }>(
settingsByKey: ReadonlyMap<string, T>,
key: string,
): T | undefined {
return settingsByKey.get(key)
}
export function isSettingValueDirty(current: unknown, original: unknown): boolean {
if (Array.isArray(current) && Array.isArray(original)) {
if (current.length !== original.length) return true
for (let i = 0; i < current.length; i++) {
if (current[i] !== original[i]) return true
}
return false
}
return current !== original
}