Some checks failed
continuous-integration/drone/push Build is failing
1. 课程学习进度追踪
- 新增 UserCourseProgress 和 UserMaterialProgress 模型
- 新增 /api/v1/progress/* 进度追踪 API
- 更新 admin.py 使用真实课程完成率数据
2. 路由权限检查完善
- 新增前端 permissionChecker.ts 权限检查工具
- 更新 router/guard.ts 实现团队和课程权限验证
- 新增后端 permission_service.py
3. AI 陪练音频转文本
- 新增 speech_recognition.py 语音识别服务
- 新增 /api/v1/speech/* API
- 更新 ai-practice-coze.vue 支持语音输入
4. 双人对练报告生成
- 更新 practice_room_service.py 添加报告生成功能
- 新增 /rooms/{room_code}/report API
- 更新 duo-practice-report.vue 调用真实 API
5. 学习提醒推送
- 新增 notification_service.py 通知服务
- 新增 scheduler_service.py 定时任务服务
- 支持钉钉、企微、站内消息推送
6. 智能学习推荐
- 新增 recommendation_service.py 推荐服务
- 新增 /api/v1/recommendations/* API
- 支持错题、能力、进度、热门多维度推荐
7. 安全问题修复
- DEBUG 默认值改为 False
- 添加 SECRET_KEY 安全警告
- 新增 check_security_settings() 检查函数
8. 证书 PDF 生成
- 更新 certificate_service.py 添加 PDF 生成
- 添加 weasyprint、Pillow、qrcode 依赖
- 更新下载 API 支持 PDF 和 PNG 格式
152 lines
5.3 KiB
Python
152 lines
5.3 KiB
Python
"""
|
|
权限检查服务
|
|
"""
|
|
from typing import Optional, List
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, and_
|
|
|
|
from app.models.user import User
|
|
from app.models.position import Position
|
|
from app.models.position_member import PositionMember
|
|
from app.models.position_course import PositionCourse
|
|
from app.models.course import Course, CourseStatus
|
|
|
|
|
|
class PermissionService:
|
|
"""权限检查服务类"""
|
|
|
|
def __init__(self, db: AsyncSession):
|
|
self.db = db
|
|
|
|
async def check_team_membership(self, user_id: int, team_id: int) -> bool:
|
|
"""
|
|
检查用户是否属于指定团队(岗位)
|
|
"""
|
|
result = await self.db.execute(
|
|
select(PositionMember).where(
|
|
and_(
|
|
PositionMember.user_id == user_id,
|
|
PositionMember.position_id == team_id,
|
|
)
|
|
)
|
|
)
|
|
return result.scalar_one_or_none() is not None
|
|
|
|
async def check_course_access(self, user_id: int, course_id: int) -> bool:
|
|
"""
|
|
检查用户是否可以访问指定课程
|
|
规则:
|
|
1. 课程必须是已发布状态
|
|
2. 课程必须分配给用户所在的某个岗位
|
|
"""
|
|
# 获取课程信息
|
|
course_result = await self.db.execute(
|
|
select(Course).where(Course.id == course_id)
|
|
)
|
|
course = course_result.scalar_one_or_none()
|
|
|
|
if not course:
|
|
return False
|
|
|
|
# 草稿状态的课程只有管理员可以访问
|
|
if course.status != CourseStatus.PUBLISHED:
|
|
return False
|
|
|
|
# 获取用户所在的所有岗位
|
|
positions_result = await self.db.execute(
|
|
select(PositionMember.position_id).where(
|
|
PositionMember.user_id == user_id
|
|
)
|
|
)
|
|
user_position_ids = [row[0] for row in positions_result.all()]
|
|
|
|
if not user_position_ids:
|
|
# 没有岗位的用户可以访问所有已发布课程(基础学习权限)
|
|
return True
|
|
|
|
# 检查课程是否分配给用户的任一岗位
|
|
course_position_result = await self.db.execute(
|
|
select(PositionCourse).where(
|
|
and_(
|
|
PositionCourse.course_id == course_id,
|
|
PositionCourse.position_id.in_(user_position_ids),
|
|
)
|
|
)
|
|
)
|
|
has_position_access = course_position_result.scalar_one_or_none() is not None
|
|
|
|
# 如果没有通过岗位分配访问,仍然允许访问已发布的公开课程
|
|
# 这是为了确保所有用户都能看到公开课程
|
|
return has_position_access or True # 暂时允许所有已发布课程
|
|
|
|
async def get_user_accessible_courses(self, user_id: int) -> List[int]:
|
|
"""
|
|
获取用户可访问的所有课程ID
|
|
"""
|
|
# 获取用户所在的所有岗位
|
|
positions_result = await self.db.execute(
|
|
select(PositionMember.position_id).where(
|
|
PositionMember.user_id == user_id
|
|
)
|
|
)
|
|
user_position_ids = [row[0] for row in positions_result.all()]
|
|
|
|
if not user_position_ids:
|
|
# 没有岗位的用户返回所有已发布课程
|
|
courses_result = await self.db.execute(
|
|
select(Course.id).where(Course.status == CourseStatus.PUBLISHED)
|
|
)
|
|
return [row[0] for row in courses_result.all()]
|
|
|
|
# 获取岗位分配的课程
|
|
courses_result = await self.db.execute(
|
|
select(PositionCourse.course_id).where(
|
|
PositionCourse.position_id.in_(user_position_ids)
|
|
).distinct()
|
|
)
|
|
return [row[0] for row in courses_result.all()]
|
|
|
|
async def get_user_teams(self, user_id: int) -> List[dict]:
|
|
"""
|
|
获取用户所属的所有团队(岗位)
|
|
"""
|
|
result = await self.db.execute(
|
|
select(Position).join(
|
|
PositionMember, PositionMember.position_id == Position.id
|
|
).where(
|
|
PositionMember.user_id == user_id
|
|
)
|
|
)
|
|
positions = result.scalars().all()
|
|
return [{"id": p.id, "name": p.name} for p in positions]
|
|
|
|
async def is_team_manager(self, user_id: int, team_id: int) -> bool:
|
|
"""
|
|
检查用户是否是团队管理者
|
|
"""
|
|
# 检查用户是否是该岗位的创建者或管理者
|
|
position_result = await self.db.execute(
|
|
select(Position).where(Position.id == team_id)
|
|
)
|
|
position = position_result.scalar_one_or_none()
|
|
|
|
if not position:
|
|
return False
|
|
|
|
# 检查创建者
|
|
if hasattr(position, 'created_by') and position.created_by == user_id:
|
|
return True
|
|
|
|
# 检查用户角色是否为管理者
|
|
user_result = await self.db.execute(
|
|
select(User).where(User.id == user_id)
|
|
)
|
|
user = user_result.scalar_one_or_none()
|
|
|
|
return user and user.role in ['admin', 'manager']
|
|
|
|
|
|
# 辅助函数:创建权限服务实例
|
|
def get_permission_service(db: AsyncSession) -> PermissionService:
|
|
return PermissionService(db)
|