@samitouri / QOSami-HFS / commits / d910f083

admin/accounts: avoid deleting current account

Massimo Melina committed Dec 24, 2023 at 13:51 UTC d910f0838900cc4c1d0a0581fd0031cae8c7af42
2 files changed +8 -4
admin/src/AccountForm.ts
+7 -3
@@ -11,9 +11,11 @@ import { Account } from './AccountsPage'
11 import { createVerifierAndSalt, SRPParameters, SRPRoutines } from 'tssrp6a'
12 import { AutoDelete, Delete } from '@mui/icons-material'
13 import { isMobile } from './misc'
14 +import { state, useSnapState } from './state'
15
16 interface FormProps { account: Account, groups: string[], done: (username: string)=>void, reload: ()=>void, addToBar: ReactNode }
17 export default function AccountForm({ account, done, groups, addToBar, reload }: FormProps) {
18 + const { username } = useSnapState()
19 const [values, setValues] = useState<Account & { password?: string, password2?: string }>(account)
20 const [belongsOptions, setBelongOptions] = useState<string[]>([])
21 useEffect(() => {
@@ -38,6 +40,7 @@ export default function AccountForm({ account, done, groups, addToBar, reload }:
40 icon: Delete,
41 title: "Delete",
42 confirm: "Delete?",
43 + ...username === account.username && { disabled: true, title: "Cannot delete current account" },
44 onClick: () => apiCall('del_account', { username: account.username }).then(reload)
45 }),
46 h(IconBtn, {
@@ -81,13 +84,12 @@ export default function AccountForm({ account, done, groups, addToBar, reload }:
84 sx: modifiedSx( !isEqualLax(values, account)),
85 async onClick() {
86 const { password='', password2, adminActualAccess, hasPassword, ...withoutPassword } = values
84 - const { username } = values
87 if (add) {
88 const got = await apiCall('add_account', withoutPassword)
89 if (password)
88 - try { await apiNewPassword(username, password) }
90 + try { await apiNewPassword(values.username, password) }
91 catch(e) {
90 - apiCall('del_account', { username }).then() // best effort, don't wait
92 + apiCall('del_account', { username: values.username }).then() // best effort, don't wait
93 throw e
94 }
95 done(got?.username)
@@ -100,6 +102,8 @@ export default function AccountForm({ account, done, groups, addToBar, reload }:
102 })
103 if (password)
104 await apiNewPassword(username, password)
105 + if (account.username === username)
106 + state.username = values.username
107 setTimeout(() => toast("Account modified", 'success'), 1) // workaround: showing a dialog at this point is causing a crash if we are in a dialog
108 done(got?.username) // username may have been changed, so we pass it back
109 }
admin/src/AccountsPage.ts
+1 -1
@@ -142,7 +142,7 @@ export default function AccountsPage() {
142
143 async function deleteAccounts() {
144 if (sel.length > _.pull(sel, username).length)
145 - await alertDialog("Won't delete current account", 'warning')
145 + if (!await confirmDialog(`Will delete the rest but not current account (${username})`)) return
146 if (!sel.length) return
147 if (!await confirmDialog(`Delete ${sel.length} item(s)?`)) return
148 try {