ERP/test_py/reset_password.py
2025-11-25 10:35:02 +08:00

48 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
密码重置脚本
用法: python3 reset_password.py <用户名> <新密码>
"""
import sys
import sqlite3
from werkzeug.security import generate_password_hash
if len(sys.argv) != 3:
print("用法: python3 reset_password.py <用户名> <新密码>")
sys.exit(1)
username = sys.argv[1]
new_password = sys.argv[2]
DB_PATH = 'server/data.db'
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
# 检查用户是否存在
user = c.execute('SELECT id FROM users WHERE username = ?', (username,)).fetchone()
if not user:
print(f"❌ 用户 '{username}' 不存在")
print("\n现有用户:")
users = c.execute('SELECT username FROM users').fetchall()
for u in users:
print(f" - {u[0]}")
sys.exit(1)
# 更新密码
password_hash = generate_password_hash(new_password)
c.execute('UPDATE users SET password_hash = ? WHERE username = ?', (password_hash, username))
conn.commit()
print(f"✅ 用户 '{username}' 的密码已重置")
print(f" 新密码: {new_password}")
conn.close()
except Exception as e:
print(f"❌ 错误: {e}")
sys.exit(1)