Add admin recovery option to bootstrap flow
This commit is contained in:
@@ -2796,6 +2796,11 @@ def run() -> None:
|
||||
"Password rules: "
|
||||
f"{auth_service.password_min_length}-{auth_service.password_max_length} chars."
|
||||
)
|
||||
if auth_service.has_admin():
|
||||
print("An admin account already exists.")
|
||||
return
|
||||
|
||||
def prompt_create_admin() -> bool:
|
||||
while True:
|
||||
username = input("Admin username: ").strip()
|
||||
normalized_username = auth_service._normalize_username(username)
|
||||
@@ -2821,9 +2826,53 @@ def run() -> None:
|
||||
try:
|
||||
created = auth_service.bootstrap_admin(normalized_username, password, email=email)
|
||||
print(f"Admin created: {created.username}")
|
||||
break
|
||||
return True
|
||||
except AuthError as exc:
|
||||
print(f"Could not create admin: {exc}")
|
||||
if auth_service.has_admin():
|
||||
return False
|
||||
|
||||
def prompt_promote_existing_admin() -> bool:
|
||||
users = auth_service.list_users_for_admin_menu()
|
||||
if not users:
|
||||
print("No existing users found; create a new admin instead.")
|
||||
return False
|
||||
print("Existing users:")
|
||||
for user in users:
|
||||
print(f" - {user['username']} ({user['role']}, {user['status']})")
|
||||
while True:
|
||||
username = input("Existing username to promote: ").strip()
|
||||
if not username:
|
||||
print("Username is required.")
|
||||
continue
|
||||
try:
|
||||
normalized = auth_service._normalize_username(username)
|
||||
auth_service.set_user_role(normalized, "admin")
|
||||
print(f"Admin promoted: {normalized}")
|
||||
return True
|
||||
except AuthError as exc:
|
||||
print(f"Could not promote user: {exc}")
|
||||
|
||||
if auth_service.list_users_for_admin_menu():
|
||||
print("No admin account found. Choose bootstrap mode:")
|
||||
print(" 1) Promote existing account to admin")
|
||||
print(" 2) Create new admin account")
|
||||
while True:
|
||||
choice = input("Select [1/2]: ").strip()
|
||||
if choice == "1":
|
||||
if prompt_promote_existing_admin():
|
||||
break
|
||||
print("Falling back to new admin creation.")
|
||||
if prompt_create_admin():
|
||||
break
|
||||
continue
|
||||
if choice == "2":
|
||||
if prompt_create_admin():
|
||||
break
|
||||
continue
|
||||
print("Please select 1 or 2.")
|
||||
else:
|
||||
prompt_create_admin()
|
||||
finally:
|
||||
auth_service.close()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user