Files
qihuo/reset_admin.py
T
2026-06-15 11:29:12 +08:00

25 lines
903 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""从 .env 重置管理员账号(服务器上忘记密码时使用)"""
import os
import sys
from dotenv import load_dotenv
from werkzeug.security import generate_password_hash
BASE = os.path.dirname(os.path.abspath(__file__))
load_dotenv(os.path.join(BASE, ".env"))
sys.path.insert(0, BASE)
from app import set_setting, get_setting # noqa: E402
username = os.getenv("ADMIN_USERNAME", "admin").strip() or "admin"
password = os.getenv("ADMIN_PASSWORD", "").strip()
if not password or password == "change-me-on-first-login":
print("请在 .env 中设置 ADMIN_PASSWORD 后再运行此脚本")
sys.exit(1)
old_username = get_setting("admin_username")
set_setting("admin_username", username)
set_setting("admin_password_hash", generate_password_hash(password))
print(f"已重置管理员: {username}(原账号: {old_username or ''}")