claim default campaign for the admin on startup so setup wizard works
Some checks failed
Build and Push / build (push) Has been cancelled

This commit is contained in:
KansaiGaijin
2026-07-30 12:55:36 +12:00
parent 677c30ff7d
commit 146da58e7d

View File

@@ -90,11 +90,19 @@ def get_user_from_token(token: str) -> Optional[dict]:
def ensure_admin_exists():
"""Called on startup — creates the admin user if no users exist."""
"""Called on startup — creates the admin user if no users exist,
and ensures the 'default' campaign is owned by the admin."""
conn = db.get_conn()
row = conn.execute("SELECT COUNT(*) as cnt FROM users").fetchone()
if row["cnt"] > 0:
return
pw = ADMIN_PASSWORD or os.urandom(8).hex()
db.create_user(ADMIN_USERNAME, hash_password(pw), is_admin=True)
print(f"[auth] Created admin user '{ADMIN_USERNAME}' (password: {pw})")
if row["cnt"] == 0:
pw = ADMIN_PASSWORD or os.urandom(8).hex()
db.create_user(ADMIN_USERNAME, hash_password(pw), is_admin=True)
print(f"[auth] Created admin user '{ADMIN_USERNAME}' (password: {pw})")
# Claim the "default" campaign for the admin so setup wizard works
admin = db.get_user_by_username(ADMIN_USERNAME)
if admin:
conn.execute(
"UPDATE campaigns SET created_by = ? WHERE id = 'default' AND created_by IS NULL",
(admin["id"],),
)