Skip to content
This repository was archived by the owner on Apr 13, 2026. It is now read-only.

Commit f28d401

Browse files
committed
Ensure bootstrap admin on every init
1 parent 087416c commit f28d401

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

‎src/server/init/ensure-initialized.ts‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ export function ensureInitialized(db: PrismaClient): Promise<void> {
1414
if (initPromise) return initPromise;
1515

1616
initPromise = (async () => {
17-
// 1) Ensure admin exists (if no users at all)
18-
const userCount = await db.user.count();
19-
if (userCount === 0) {
20-
const defaultEmail = process.env.INITIAL_ADMIN_EMAIL?.trim().toLowerCase() ?? "admin@example.com";
21-
22-
await db.user.upsert({
23-
where: { email: defaultEmail },
24-
update: { role: UserRole.ADMIN },
25-
create: {
26-
email: defaultEmail,
27-
name: "Admin User",
28-
role: UserRole.ADMIN,
29-
},
30-
});
31-
}
17+
// 1) Ensure the bootstrap admin exists (every init, no auto-provisioning outside env)
18+
const defaultEmail = process.env.INITIAL_ADMIN_EMAIL?.trim().toLowerCase() ?? "admin@example.com";
19+
await db.user.upsert({
20+
where: { email: defaultEmail },
21+
update: { role: UserRole.ADMIN },
22+
create: {
23+
email: defaultEmail,
24+
name: "Admin User",
25+
role: UserRole.ADMIN,
26+
},
27+
});
3228

3329
// 2) Ensure MITRE data exists
3430
const tacticCount = await db.mitreTactic.count();

‎src/test/init.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ describe("ensureInitialized", () => {
1515
delete process.env.INITIAL_ADMIN_EMAIL;
1616
});
1717

18-
it("creates admin when no users and seeds MITRE when empty", async () => {
19-
const user = { count: vi.fn().mockResolvedValue(0), upsert: vi.fn() };
18+
it("creates admin and seeds MITRE when empty", async () => {
19+
const user = { upsert: vi.fn() };
2020
const mitreTactic = { count: vi.fn().mockResolvedValue(0), upsert: vi.fn() };
2121
const mitreTechnique = { upsert: vi.fn() };
2222
const mitreSubTechnique = { upsert: vi.fn() };

0 commit comments

Comments
 (0)