parent
4249aae99a
commit
2ce5f7d866
@ -0,0 +1,52 @@
|
||||
"""
|
||||
初始化管理员账号脚本
|
||||
运行方式: python init_admin.py
|
||||
"""
|
||||
from app import create_app
|
||||
from models import User, db
|
||||
from utils.auth_utils import hash_password
|
||||
|
||||
def init_admin():
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
username = input('请输入管理员用户名 (默认: admin): ').strip() or 'admin'
|
||||
password = input('请输入管理员密码: ').strip()
|
||||
|
||||
if not password:
|
||||
print('密码不能为空')
|
||||
return
|
||||
|
||||
# 检查用户名是否已存在
|
||||
existing_user = User.query.filter_by(username=username, deleted_at=None).first()
|
||||
if existing_user:
|
||||
choice = input(f'用户 {username} 已存在,是否要修改为管理员?(y/n): ').strip().lower()
|
||||
if choice == 'y':
|
||||
existing_user.password = hash_password(password)
|
||||
existing_user.authority = 1
|
||||
db.session.commit()
|
||||
print(f'用户 {username} 已设置为管理员')
|
||||
else:
|
||||
print('操作已取消')
|
||||
return
|
||||
|
||||
# 创建管理员
|
||||
admin = User(
|
||||
username=username,
|
||||
password=hash_password(password),
|
||||
authority=1
|
||||
)
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
print(f'管理员账号 {username} 创建成功!')
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_admin()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue