avoid having empty strings in accounts
Massimo Melina committed
Apr 3, 2022 at 21:47 UTC
e8260746362c9c84da70dba8e9d689a0e8cb7048
1 file changed
+11
-7
server/src/perm.ts
+11
-7
@@ -131,23 +131,27 @@ export function renameAccount(from: string, to: string) {
131
}
132
}
133
134
-const assignableProps = ['redirect','ignore_limits','belongs','admin']
134
+// we consider all the following fields, when falsy, as equivalent to be missing. If this changes in the future, please adjust addAccount and setAccount
135
+const assignableProps: (keyof Account)[] = ['redirect','ignore_limits','belongs','admin']
136
137
export function addAccount(username: string, props: Partial<Account>) {
138
if (!username || accounts[username])
139
return
139
- const copy = { username, ..._.pick(props, assignableProps) }
140
+ const copy = _.pickBy(_.pick(props, assignableProps), Boolean)
141
setHidden(copy, { username })
141
- accounts[username] = copy
142
+ accounts[username] = copy as typeof copy & { username: string }
143
saveAccountsAsap()
144
return copy
145
}
146
147
export function setAccount(username: string, changes: Partial<Account>) {
147
- const { username: newU, ...rest } = changes
148
- if (newU)
149
- renameAccount(username, newU)
150
- Object.assign(getAccount(newU || username), _.pick(rest, assignableProps))
148
+ const rest = _.pick(changes, assignableProps)
149
+ for (const [k,v] of Object.entries(rest))
150
+ if (!v)
151
+ rest[k as keyof Account] = undefined
152
+ Object.assign(getAccount(username), rest)
153
+ if (changes.username)
154
+ renameAccount(username, changes.username)
155
saveAccountsAsap()
156
return true
157
}