-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathreset_admin.py
More file actions
89 lines (70 loc) · 2.43 KB
/
Copy pathreset_admin.py
File metadata and controls
89 lines (70 loc) · 2.43 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
通过 Python 命令行重置管理员账号密码。
用法示例:
python reset_admin.py --username admin --password NewPass123
python reset_admin.py --email admin@example.com --password NewPass123
python reset_admin.py --list-admins
"""
import argparse
import sys
from app import app
from models import db, User
def build_parser():
parser = argparse.ArgumentParser(description="重置管理员账号密码")
parser.add_argument("--username", help="管理员用户名")
parser.add_argument("--email", help="管理员邮箱")
parser.add_argument("--password", help="新的管理员密码")
parser.add_argument(
"--list-admins",
action="store_true",
help="列出当前所有管理员账号",
)
return parser
def list_admins():
admins = User.query.filter(User.is_admin.is_(True)).order_by(User.id.asc()).all()
if not admins:
print("当前没有管理员账号。")
return 1
print("当前管理员账号:")
for admin in admins:
print(f"- id={admin.id} username={admin.username} email={admin.email} active={admin.is_active}")
return 0
def reset_admin_password(username=None, email=None, password=None):
if not password:
print("错误:必须提供 --password")
return 1
if len(password) < 6:
print("错误:新密码至少需要 6 个字符")
return 1
query = User.query.filter(User.is_admin.is_(True))
if username:
query = query.filter(User.username == username)
elif email:
query = query.filter(User.email == email)
else:
print("错误:请使用 --username 或 --email 指定管理员账号")
return 1
admin = query.first()
if not admin:
print("错误:未找到匹配的管理员账号")
return 1
admin.set_password(password)
admin.is_admin = True
db.session.commit()
print(f"已重置管理员密码:username={admin.username} email={admin.email}")
return 0
def main():
parser = build_parser()
args = parser.parse_args()
with app.app_context():
if args.list_admins:
return list_admins()
return reset_admin_password(
username=(args.username or "").strip() or None,
email=(args.email or "").strip() or None,
password=args.password,
)
if __name__ == "__main__":
sys.exit(main())