57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
|
|
"""
|
||
|
|
创建管理员用户脚本
|
||
|
|
"""
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
|
||
|
|
# 添加项目根目录到 Python 路径
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
|
||
|
|
from app.core.database import SessionLocal
|
||
|
|
from app.models.user import User, UserRole, UserStatus
|
||
|
|
from app.core.security import get_password_hash
|
||
|
|
|
||
|
|
|
||
|
|
def create_admin_user():
|
||
|
|
"""创建管理员用户"""
|
||
|
|
db = SessionLocal()
|
||
|
|
try:
|
||
|
|
# 检查是否已存在admin用户
|
||
|
|
existing_user = db.query(User).filter(User.username == "admin").first()
|
||
|
|
if existing_user:
|
||
|
|
print(f"Admin user already exists with ID: {existing_user.id}")
|
||
|
|
return
|
||
|
|
|
||
|
|
# 使用与系统相同的密码哈希方法
|
||
|
|
password = "admin123"
|
||
|
|
hashed_password = get_password_hash(password)
|
||
|
|
|
||
|
|
admin_user = User(
|
||
|
|
username="admin",
|
||
|
|
email="admin@example.com",
|
||
|
|
hashed_password=hashed_password,
|
||
|
|
role=UserRole.ADMIN,
|
||
|
|
nickname="系统管理员",
|
||
|
|
status=UserStatus.ACTIVE,
|
||
|
|
balance=1000000, # 10000元
|
||
|
|
is_active=1,
|
||
|
|
)
|
||
|
|
|
||
|
|
db.add(admin_user)
|
||
|
|
db.commit()
|
||
|
|
db.refresh(admin_user)
|
||
|
|
|
||
|
|
print(f"Admin user created successfully with ID: {admin_user.id}")
|
||
|
|
print(f"Username: admin")
|
||
|
|
print(f"Password: admin123")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
print(f"Error creating admin user: {e}")
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
create_admin_user()
|