fix: tenant switch auto-copies global admin into tenant users

isSetupComplete() now checks public.global_admins — if a global admin
exists but tenant has no local users, copies admin into tenant's users
table and marks setup complete. Prevents setup wizard on tenant switch.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mitchell R 2026-05-27 02:10:56 +02:00
parent 65de42d495
commit 1dbb56752c

View file

@ -260,7 +260,24 @@ export class Repository {
}
async isSetupComplete(): Promise<boolean> {
return (await this.getSetupState()).is_complete && (await this.countUsers()) > 0;
const state = await this.getSetupState();
if (state.is_complete) return true;
if ((await this.countUsers()) > 0) return true;
// No local users — copy global admin into tenant if one exists.
const ga = await this._get<{ id: string; username: string; password_hash: string }>(
"SELECT id, username, password_hash FROM public.global_admins WHERE is_active = true LIMIT 1",
).catch(() => undefined);
if (ga) {
await this._run(
`INSERT INTO users (id, username, password_hash, role, is_active)
VALUES (?, ?, ?, 'admin', true)
ON CONFLICT (id) DO NOTHING`,
[ga.id, ga.username, ga.password_hash],
);
await this.markSetupComplete();
return true;
}
return false;
}
async markSetupComplete(): Promise<void> {