57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""检查用户角色信息"""
|
|
|
|
import requests
|
|
|
|
def check_user_role():
|
|
"""检查用户角色"""
|
|
base_url = "http://localhost:8001/api/v1"
|
|
|
|
# 登录
|
|
print("步骤1: 登录")
|
|
login_data = {
|
|
"username": "admin",
|
|
"password": "admin123"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(f"{base_url}/users/login", json=login_data)
|
|
print(f"状态码: {response.status_code}")
|
|
|
|
if response.status_code != 200:
|
|
print(f"登录失败: {response.text}")
|
|
return
|
|
|
|
data = response.json()
|
|
access_token = data.get('access_token')
|
|
print(f"登录成功!")
|
|
|
|
# 获取用户信息
|
|
print("\n步骤2: 获取用户信息")
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
|
user_response = requests.get(f"{base_url}/users/me", headers=headers)
|
|
print(f"状态码: {user_response.status_code}")
|
|
|
|
if user_response.status_code == 200:
|
|
user_data = user_response.json()
|
|
print(f"\n用户信息:")
|
|
print(f" 用户名: {user_data.get('username', 'N/A')}")
|
|
print(f" 邮箱: {user_data.get('email', 'N/A')}")
|
|
print(f" 角色ID: {user_data.get('role_id', 'N/A')}")
|
|
print(f" 角色名称: {user_data.get('role_name', 'N/A')}")
|
|
print(f" 角色对象: {user_data.get('role', 'N/A')}")
|
|
|
|
# 检查是否是管理员
|
|
role_name = user_data.get('role_name')
|
|
if role_name == 'admin':
|
|
print(f"\n✅ 用户是管理员,应该显示后台管理页面")
|
|
else:
|
|
print(f"\n❌ 用户不是管理员,角色名称是: {role_name}")
|
|
else:
|
|
print(f"获取用户信息失败: {user_response.text}")
|
|
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
check_user_role() |