@samitouri / QOSami-HFS / commits / a6976deb

validate accounts to avoid loops

Massimo Melina committed Jan 1, 2026 at 21:39 UTC a6976debccb089dd5ed713052d1d181d67a2209c
1 file changed +24
src/perm.ts
+24
@@ -131,7 +131,31 @@ accounts.sub(_.debounce(obj => {
131 setHidden(rec, { username: norm })
132 }
133 void updateAccount(rec, {}) // work fields
134 + removeLoops(norm)
135 })
136 +
137 + function removeLoops(normalizedUsername: string, visiting = new Set<string>()) {
138 + if (visiting.has(normalizedUsername))
139 + return
140 + visiting.add(normalizedUsername)
141 + const account = obj[normalizedUsername]
142 + const removed = _.remove(account.belongs, parent => {
143 + const k = normalizeUsername(parent)
144 + return obj[k] && visiting.has(k)
145 + })
146 + if (removed.length)
147 + saveAccountsAsap()
148 + if (account?.belongs?.length) {
149 + for (const parent of account.belongs) {
150 + const k = normalizeUsername(parent)
151 + if (obj[k])
152 + removeLoops(k, visiting)
153 + }
154 + if (!account.belongs.length)
155 + delete account.belongs
156 + }
157 + visiting.delete(normalizedUsername)
158 + }
159 })) // don't trigger in the middle of a series of deletion, as we may have an inconsistent state
160
161 export function normalizeUsername(username: string) {