-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_password.py
More file actions
44 lines (34 loc) · 1.18 KB
/
Copy pathreset_password.py
File metadata and controls
44 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
密码重置脚本
运行: python3 reset_password.py
"""
import bcrypt
import sqlite3
import os
# 数据库路径
DB_PATH = os.path.join(os.path.dirname(__file__), 'data', 'quantagent.db')
def reset_password(username: str, new_password: str):
"""重置用户密码"""
if not os.path.exists(DB_PATH):
print(f"❌ 数据库不存在: {DB_PATH}")
return False
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# 检查用户是否存在
cursor.execute("SELECT id, username FROM users WHERE username = ?", (username,))
user = cursor.fetchone()
if not user:
print(f"❌ 用户 '{username}' 不存在")
conn.close()
return False
# 生成新密码哈希
hashed = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
# 更新密码
cursor.execute("UPDATE users SET hashed_password = ? WHERE username = ?", (hashed, username))
conn.commit()
conn.close()
print(f"✅ 用户 '{username}' 密码已重置为: {new_password}")
return True
if __name__ == "__main__":
# 重置 shelld 用户密码为 123456
reset_password("shelld", "123456")