feat: 初始化考培练系统项目
- 从服务器拉取完整代码 - 按框架规范整理项目结构 - 配置 Drone CI 测试环境部署 - 包含后端(FastAPI)、前端(Vue3)、管理端 技术栈: Vue3 + TypeScript + FastAPI + MySQL
This commit is contained in:
146
backend/create_system_accounts.py
Normal file
146
backend/create_system_accounts.py
Normal file
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
创建系统测试账户脚本
|
||||
"""
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到Python路径
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# 加载本地配置
|
||||
try:
|
||||
import local_config
|
||||
print("✅ 本地配置已加载")
|
||||
except ImportError:
|
||||
print("⚠️ 未找到local_config.py")
|
||||
|
||||
# 要创建的系统账户
|
||||
SYSTEM_USERS = [
|
||||
{
|
||||
"username": "superadmin",
|
||||
"password": "Superadmin123!",
|
||||
"email": "superadmin@kaopeilian.com",
|
||||
"full_name": "超级管理员",
|
||||
"role": "admin", # 使用现有的admin角色
|
||||
"is_superuser": 1,
|
||||
},
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "Admin123!",
|
||||
"email": "admin@kaopeilian.com",
|
||||
"full_name": "系统管理员",
|
||||
"role": "admin",
|
||||
"is_superuser": 0,
|
||||
},
|
||||
{
|
||||
"username": "testuser",
|
||||
"password": "TestPass123!",
|
||||
"email": "testuser@kaopeilian.com",
|
||||
"full_name": "测试学员",
|
||||
"role": "trainee", # 学员角色
|
||||
"is_superuser": 0,
|
||||
},
|
||||
]
|
||||
|
||||
async def create_system_accounts():
|
||||
"""创建系统测试账户"""
|
||||
try:
|
||||
from app.core.database import AsyncSessionLocal
|
||||
from app.core.security import get_password_hash
|
||||
from sqlalchemy import text
|
||||
|
||||
print("🔧 开始创建系统测试账户...\n")
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
created_count = 0
|
||||
updated_count = 0
|
||||
|
||||
for user_data in SYSTEM_USERS:
|
||||
username = user_data["username"]
|
||||
password = user_data["password"]
|
||||
hashed_password = get_password_hash(password)
|
||||
|
||||
# 检查用户是否存在
|
||||
result = await session.execute(
|
||||
text("SELECT id FROM users WHERE username = :username"),
|
||||
{"username": username}
|
||||
)
|
||||
existing = result.fetchone()
|
||||
|
||||
if existing:
|
||||
# 更新现有用户
|
||||
await session.execute(
|
||||
text("""
|
||||
UPDATE users
|
||||
SET password_hash = :password_hash,
|
||||
email = :email,
|
||||
full_name = :full_name,
|
||||
role = :role,
|
||||
is_superuser = :is_superuser,
|
||||
is_active = 1,
|
||||
updated_at = NOW()
|
||||
WHERE username = :username
|
||||
"""),
|
||||
{
|
||||
"username": username,
|
||||
"password_hash": hashed_password,
|
||||
"email": user_data["email"],
|
||||
"full_name": user_data["full_name"],
|
||||
"role": user_data["role"],
|
||||
"is_superuser": user_data["is_superuser"]
|
||||
}
|
||||
)
|
||||
await session.commit()
|
||||
updated_count += 1
|
||||
print(f"✓ 更新用户: {username} ({user_data['full_name']})")
|
||||
else:
|
||||
# 创建新用户
|
||||
await session.execute(
|
||||
text("""
|
||||
INSERT INTO users (
|
||||
username, email, password_hash, role,
|
||||
is_active, full_name, is_superuser,
|
||||
created_at, updated_at
|
||||
)
|
||||
VALUES (
|
||||
:username, :email, :password_hash, :role,
|
||||
1, :full_name, :is_superuser,
|
||||
NOW(), NOW()
|
||||
)
|
||||
"""),
|
||||
{
|
||||
"username": username,
|
||||
"email": user_data["email"],
|
||||
"password_hash": hashed_password,
|
||||
"role": user_data["role"],
|
||||
"full_name": user_data["full_name"],
|
||||
"is_superuser": user_data["is_superuser"]
|
||||
}
|
||||
)
|
||||
await session.commit()
|
||||
created_count += 1
|
||||
print(f"✓ 创建用户: {username} ({user_data['full_name']})")
|
||||
|
||||
# 打印总结
|
||||
print("\n" + "="*50)
|
||||
print("✅ 系统用户创建/更新完成!")
|
||||
print(f"新创建用户数: {created_count}")
|
||||
print(f"更新用户数: {updated_count}")
|
||||
print("\n系统账户信息:")
|
||||
print("-"*50)
|
||||
print("| 角色 | 用户名 | 密码 | 权限说明 |")
|
||||
print("| ---------- | ---------- | -------------- | ---------------------------- |")
|
||||
print("| 超级管理员 | superadmin | Superadmin123! | 系统最高权限,可管理所有功能 |")
|
||||
print("| 系统管理员 | admin | Admin123! | 可管理除“系统管理”模块外的全部功能(管理员仪表盘、用户管理、岗位管理、系统日志) |")
|
||||
print("| 测试学员 | testuser | TestPass123! | 可学习课程、参加考试和训练 |")
|
||||
print("-"*50)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 创建用户失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(create_system_accounts())
|
||||
Reference in New Issue
Block a user