You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
"""
|
|
初始化管理员账号脚本
|
|
运行方式: 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|