删除服务端材质渲染功能及system_config表,转为环境变量配置,初步配置管理员功能
This commit is contained in:
@@ -257,33 +257,222 @@ curl -X GET http://localhost:8080/api/v1/profile/{profile_info['profile_uuid']}
|
||||
return output
|
||||
|
||||
|
||||
def set_user_role(admin_token, user_id, role):
|
||||
"""设置用户角色"""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {admin_token}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.put(
|
||||
f"{BASE_URL}/admin/users/role",
|
||||
json={"user_id": user_id, "role": role},
|
||||
headers=headers,
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if result.get("code") == 200:
|
||||
print_success(f"用户角色设置为: {role}")
|
||||
return True
|
||||
else:
|
||||
print_error(f"设置角色失败: {result.get('message', '未知错误')}")
|
||||
return False
|
||||
except requests.exceptions.RequestException as e:
|
||||
print_error(f"设置角色失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def login_user(username, password):
|
||||
"""登录用户"""
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/auth/login",
|
||||
json={"username": username, "password": password},
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if result.get("code") == 200:
|
||||
return result["data"]["token"]
|
||||
return None
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
||||
|
||||
def create_admin_user():
|
||||
"""创建管理员用户"""
|
||||
print_step("创建管理员用户")
|
||||
|
||||
random_num = random.randint(10000, 99999)
|
||||
username = f"admin{random_num}"
|
||||
email = f"admin{random_num}@example.com"
|
||||
login_password = "admin123456"
|
||||
verification_code = "123456"
|
||||
|
||||
print_info(f"用户名: {username}")
|
||||
print_info(f"邮箱: {email}")
|
||||
print_info(f"密码: {login_password}")
|
||||
|
||||
register_data = {
|
||||
"username": username,
|
||||
"email": email,
|
||||
"password": login_password,
|
||||
"verification_code": verification_code
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/auth/register",
|
||||
json=register_data,
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if result.get("code") == 200:
|
||||
jwt_token = result["data"]["token"]
|
||||
user_id = result["data"]["user_info"]["id"]
|
||||
|
||||
print_success("用户注册成功!")
|
||||
print_info(f"用户ID: {user_id}")
|
||||
|
||||
# 使用默认管理员账户提升权限
|
||||
print_info("尝试使用默认管理员账户提升权限...")
|
||||
default_admin_token = login_user("admin", "admin123456")
|
||||
|
||||
if default_admin_token:
|
||||
if set_user_role(default_admin_token, user_id, "admin"):
|
||||
print_success("管理员权限设置成功!")
|
||||
return {
|
||||
"username": username,
|
||||
"email": email,
|
||||
"password": login_password,
|
||||
"jwt_token": jwt_token,
|
||||
"user_id": user_id,
|
||||
"role": "admin"
|
||||
}
|
||||
|
||||
print_error("无法提升权限,请手动设置")
|
||||
return {
|
||||
"username": username,
|
||||
"email": email,
|
||||
"password": login_password,
|
||||
"jwt_token": jwt_token,
|
||||
"user_id": user_id,
|
||||
"role": "user"
|
||||
}
|
||||
else:
|
||||
print_error(f"注册失败: {result.get('message', '未知错误')}")
|
||||
return None
|
||||
except requests.exceptions.RequestException as e:
|
||||
print_error(f"注册失败: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
def generate_admin_output(admin_info):
|
||||
"""生成管理员账户输出信息"""
|
||||
output = f"""========================================
|
||||
CarrotSkin 管理员账户信息
|
||||
========================================
|
||||
|
||||
=== 账户信息 ===
|
||||
用户名: {admin_info['username']}
|
||||
邮箱: {admin_info['email']}
|
||||
密码: {admin_info['password']}
|
||||
用户ID: {admin_info['user_id']}
|
||||
角色: {admin_info['role']}
|
||||
|
||||
=== JWT Token (API认证) ===
|
||||
Token: {admin_info['jwt_token']}
|
||||
|
||||
=== 管理员API示例 ===
|
||||
|
||||
# 1. 获取用户列表
|
||||
curl -X GET "{BASE_URL}/admin/users" \\
|
||||
-H "Authorization: Bearer {admin_info['jwt_token']}"
|
||||
|
||||
# 2. 设置用户角色为管理员
|
||||
curl -X PUT "{BASE_URL}/admin/users/role" \\
|
||||
-H "Content-Type: application/json" \\
|
||||
-H "Authorization: Bearer {admin_info['jwt_token']}" \\
|
||||
-d '{{"user_id": 1, "role": "admin"}}'
|
||||
|
||||
# 3. 获取材质列表(审核)
|
||||
curl -X GET "{BASE_URL}/admin/textures" \\
|
||||
-H "Authorization: Bearer {admin_info['jwt_token']}"
|
||||
|
||||
# 4. 删除材质
|
||||
curl -X DELETE "{BASE_URL}/admin/textures/1" \\
|
||||
-H "Authorization: Bearer {admin_info['jwt_token']}"
|
||||
|
||||
========================================
|
||||
"""
|
||||
return output
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print_header("CarrotSkin 测试账户生成器")
|
||||
|
||||
# 步骤1: 注册用户
|
||||
user_info = register_user()
|
||||
# 选择模式
|
||||
print("请选择操作:")
|
||||
print(" 1. 创建普通测试用户")
|
||||
print(" 2. 创建管理员用户")
|
||||
print(" 3. 创建两者")
|
||||
|
||||
# 步骤2: 创建角色
|
||||
profile_info = create_profile(user_info["jwt_token"])
|
||||
choice = input("\n请输入选项 (1/2/3) [默认: 1]: ").strip() or "1"
|
||||
|
||||
# 步骤3: 重置Yggdrasil密码
|
||||
yggdrasil_password = reset_yggdrasil_password(user_info["jwt_token"])
|
||||
if choice in ["1", "3"]:
|
||||
# 创建普通用户
|
||||
print_header("创建普通测试用户")
|
||||
|
||||
# 步骤1: 注册用户
|
||||
user_info = register_user()
|
||||
|
||||
# 步骤2: 创建角色
|
||||
profile_info = create_profile(user_info["jwt_token"])
|
||||
|
||||
# 步骤3: 重置Yggdrasil密码
|
||||
yggdrasil_password = reset_yggdrasil_password(user_info["jwt_token"])
|
||||
|
||||
# 输出信息
|
||||
print_header("普通用户信息汇总")
|
||||
output = generate_output(user_info, profile_info, yggdrasil_password)
|
||||
print(output)
|
||||
|
||||
# 保存到文件
|
||||
output_file = f"test_account_{user_info['username']}.txt"
|
||||
try:
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(output)
|
||||
print_success(f"信息已保存到文件: {output_file}")
|
||||
except Exception as e:
|
||||
print_error(f"保存文件失败: {str(e)}")
|
||||
|
||||
# 步骤4: 输出所有信息
|
||||
print_header("测试账户信息汇总")
|
||||
|
||||
output = generate_output(user_info, profile_info, yggdrasil_password)
|
||||
print(output)
|
||||
|
||||
# 保存到文件
|
||||
output_file = f"test_account_{user_info['username']}.txt"
|
||||
try:
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(output)
|
||||
print_success(f"信息已保存到文件: {output_file}")
|
||||
except Exception as e:
|
||||
print_error(f"保存文件失败: {str(e)}")
|
||||
if choice in ["2", "3"]:
|
||||
# 创建管理员用户
|
||||
print_header("创建管理员用户")
|
||||
admin_info = create_admin_user()
|
||||
|
||||
if admin_info:
|
||||
print_header("管理员账户信息汇总")
|
||||
admin_output = generate_admin_output(admin_info)
|
||||
print(admin_output)
|
||||
|
||||
# 保存到文件
|
||||
admin_output_file = f"admin_account_{admin_info['username']}.txt"
|
||||
try:
|
||||
with open(admin_output_file, "w", encoding="utf-8") as f:
|
||||
f.write(admin_output)
|
||||
print_success(f"管理员信息已保存到文件: {admin_output_file}")
|
||||
except Exception as e:
|
||||
print_error(f"保存文件失败: {str(e)}")
|
||||
|
||||
print_header("测试完成!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user