70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
|
|
"""
|
||
|
|
Script to create an admin user for the AI Pet Companion Platform
|
||
|
|
"""
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
# 添加backend目录到路径
|
||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
|
||
|
|
|
||
|
|
from models.database import init_db, User, AsyncSessionFactory
|
||
|
|
from utils.security import hash_password
|
||
|
|
from utils.db_utils import UserManager
|
||
|
|
|
||
|
|
|
||
|
|
async def create_admin_user():
|
||
|
|
"""Create an admin user with specified credentials"""
|
||
|
|
|
||
|
|
# 初始化数据库表
|
||
|
|
print("Initializing database...")
|
||
|
|
await init_db()
|
||
|
|
|
||
|
|
# 创建数据库会话
|
||
|
|
async with AsyncSessionFactory() as session:
|
||
|
|
user_manager = UserManager(session)
|
||
|
|
|
||
|
|
# 管理员用户信息
|
||
|
|
admin_phone = "13800138888" # 管理员手机号
|
||
|
|
admin_password = "admin123" # 管理员密码
|
||
|
|
admin_nickname = "管理员"
|
||
|
|
admin_uid = "admin_user_001"
|
||
|
|
|
||
|
|
print(f"Creating admin user with phone: {admin_phone}")
|
||
|
|
|
||
|
|
# 检查用户是否已存在
|
||
|
|
existing_user = await user_manager.get_by_phone(admin_phone)
|
||
|
|
if existing_user:
|
||
|
|
print(f"Admin user with phone {admin_phone} already exists!")
|
||
|
|
print(f"Updating existing user to admin role...")
|
||
|
|
|
||
|
|
# 更新现有用户为管理员
|
||
|
|
existing_user.role = 2 # 管理员角色
|
||
|
|
existing_user.nickname = admin_nickname
|
||
|
|
|
||
|
|
updated_user = await user_manager.update(existing_user)
|
||
|
|
print(f"Updated user {updated_user.phone} to admin role")
|
||
|
|
else:
|
||
|
|
# 创建新管理员用户
|
||
|
|
admin_user = User(
|
||
|
|
phone=admin_phone,
|
||
|
|
password_hash=hash_password(admin_password),
|
||
|
|
nickname=admin_nickname,
|
||
|
|
uid=admin_uid,
|
||
|
|
status=1, # 正常状态
|
||
|
|
role=2 # 管理员角色
|
||
|
|
)
|
||
|
|
|
||
|
|
created_user = await user_manager.create(admin_user)
|
||
|
|
print(f"Created admin user: {created_user.phone}")
|
||
|
|
|
||
|
|
print(f"Admin user setup completed!")
|
||
|
|
print(f"Phone: {admin_phone}")
|
||
|
|
print(f"Password: {admin_password}")
|
||
|
|
print(f"Role: Administrator")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
asyncio.run(create_admin_user())
|