注册服务

This commit is contained in:
2026-02-09 23:59:25 +08:00
parent f145df4fa6
commit 3c03777b97
11 changed files with 865 additions and 456 deletions

View File

@@ -39,8 +39,14 @@ async def get_current_active_user(db: Session = Depends(get_db), token: str = De
)
# 使用UserService获取用户信息避免直接使用User模型
print(f"尝试通过用户名获取用户: {username}")
user = UserService.get_user_by_username(db, username)
print(f"获取用户结果: {user.id if user else 'None'}")
if not user:
# 尝试直接查询数据库
from app.models.models import User as UserModel
direct_user = db.query(UserModel).filter(UserModel.username == username).first()
print(f"直接查询数据库结果: {direct_user.id if direct_user else 'None'}")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
@@ -51,19 +57,38 @@ async def get_current_active_user(db: Session = Depends(get_db), token: str = De
if user.status != "active":
raise HTTPException(status_code=400, detail="Inactive user")
# 使用UserService获取角色信息
role = UserService.get_role_by_id(db, user.role_id)
# 构建角色响应
role_response = None
if role:
role_response = RoleResponse(
id=role.id,
name=role.name,
description=role.description,
created_at=role.created_at,
updated_at=role.updated_at
)
role_name = None
# 尝试获取角色信息
try:
# 先尝试使用预加载的角色
if hasattr(user, 'role') and user.role:
role = user.role
role_response = RoleResponse(
id=role.id,
name=role.name,
description=role.description,
created_at=role.created_at,
updated_at=role.updated_at
)
role_name = role.name
else:
# 如果没有预加载角色尝试通过role_id获取
role = UserService.get_role_by_id(db, user.role_id)
if role:
role_response = RoleResponse(
id=role.id,
name=role.name,
description=role.description,
created_at=role.created_at,
updated_at=role.updated_at
)
role_name = role.name
except Exception as e:
# 角色获取失败不影响用户认证
print(f"获取角色信息失败: {e}")
# 构建用户响应
user_response = UserResponse(
@@ -75,7 +100,7 @@ async def get_current_active_user(db: Session = Depends(get_db), token: str = De
created_at=user.created_at,
updated_at=user.updated_at,
role=role_response,
role_name=role.name if role else None
role_name=role_name
)
return user_response
@@ -85,6 +110,13 @@ async def get_current_active_user(db: Session = Depends(get_db), token: str = De
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
except Exception as e:
print(f"获取当前用户失败: {e}")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
from app.schemas.user import LoginRequest
@@ -151,6 +183,61 @@ async def get_users(
return {"users": users, "total": len(users)}
# 角色管理API
@router.post("/roles", response_model=RoleResponse)
async def create_role(
role: RoleCreate,
current_user: UserResponse = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""创建角色"""
# 只有管理员可以创建角色
if current_user.role_name != "admin":
raise HTTPException(status_code=403, detail="Not enough permissions")
# 检查角色名称是否已存在
if UserService.get_role_by_name(db, role.name):
raise HTTPException(status_code=400, detail="Role name already exists")
# 创建角色
db_role = UserService.create_role(db, role)
return db_role
@router.get("/roles", response_model=List[RoleResponse])
async def get_roles(
current_user: UserResponse = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""获取角色列表"""
# 只有管理员可以查看所有角色
if current_user.role_name != "admin":
raise HTTPException(status_code=403, detail="Not enough permissions")
roles = UserService.get_roles(db)
return roles
@router.get("/roles/{role_id}", response_model=RoleResponse)
async def get_role(
role_id: str,
current_user: UserResponse = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""获取角色详情"""
# 只有管理员可以查看角色详情
if current_user.role_name != "admin":
raise HTTPException(status_code=403, detail="Not enough permissions")
role = UserService.get_role_by_id(db, role_id)
if not role:
raise HTTPException(status_code=404, detail="Role not found")
return role
@router.get("/{user_id}", response_model=UserResponse)
async def get_user(
user_id: str,
@@ -218,58 +305,3 @@ async def delete_user(
db.commit()
return {"message": "User deleted successfully"}
# 角色管理API
@router.post("/roles", response_model=RoleResponse)
async def create_role(
role: RoleCreate,
current_user: UserResponse = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""创建角色"""
# 只有管理员可以创建角色
if current_user.role_name != "admin":
raise HTTPException(status_code=403, detail="Not enough permissions")
# 检查角色名称是否已存在
if UserService.get_role_by_name(db, role.name):
raise HTTPException(status_code=400, detail="Role name already exists")
# 创建角色
db_role = UserService.create_role(db, role)
return db_role
@router.get("/roles", response_model=List[RoleResponse])
async def get_roles(
current_user: UserResponse = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""获取角色列表"""
# 只有管理员可以查看所有角色
if current_user.role_name != "admin":
raise HTTPException(status_code=403, detail="Not enough permissions")
roles = UserService.get_roles(db)
return roles
@router.get("/roles/{role_id}", response_model=RoleResponse)
async def get_role(
role_id: str,
current_user: UserResponse = Depends(get_current_active_user),
db: Session = Depends(get_db)
):
"""获取角色详情"""
# 只有管理员可以查看角色详情
if current_user.role_name != "admin":
raise HTTPException(status_code=403, detail="Not enough permissions")
role = UserService.get_role_by_id(db, role_id)
if not role:
raise HTTPException(status_code=404, detail="Role not found")
return role