Hamdy005 commited on
Commit
05596ae
·
1 Parent(s): 34d7bbf

feat: enforce user ownership verification for material access across all API routes

Browse files
materials/routes.py CHANGED
@@ -57,6 +57,8 @@ def get_material_by_id(
57
  mat = get_material(material_id)
58
  if not mat:
59
  raise HTTPException(404, "Material not found")
 
 
60
  return mat
61
 
62
  async def _process_pdf_background(material_id: str, file_content: bytes):
 
57
  mat = get_material(material_id)
58
  if not mat:
59
  raise HTTPException(404, "Material not found")
60
+ if mat.get("user_id") != user_id:
61
+ raise HTTPException(403, "Access denied")
62
  return mat
63
 
64
  async def _process_pdf_background(material_id: str, file_content: bytes):
quiz_generator/routes.py CHANGED
@@ -62,6 +62,8 @@ async def generate_quiz(
62
  if not topic_title and body.material_id:
63
  mat = get_material(body.material_id)
64
  if mat:
 
 
65
  topic_title = mat.get("title")
66
 
67
  if not topic_title:
@@ -82,6 +84,8 @@ async def generate_quiz(
82
  mat = get_material(body.material_id) if body.material_id else None
83
  if not mat:
84
  raise HTTPException(400, f"No {body.source_type} material found")
 
 
85
 
86
  chunks_list = get_chunks(body.material_id)
87
  chunks_texts = [c["content"] for c in chunks_list] if chunks_list else []
 
62
  if not topic_title and body.material_id:
63
  mat = get_material(body.material_id)
64
  if mat:
65
+ if mat.get("user_id") != user_id:
66
+ raise HTTPException(403, "Access denied")
67
  topic_title = mat.get("title")
68
 
69
  if not topic_title:
 
84
  mat = get_material(body.material_id) if body.material_id else None
85
  if not mat:
86
  raise HTTPException(400, f"No {body.source_type} material found")
87
+ if mat.get("user_id") != user_id:
88
+ raise HTTPException(403, "Access denied")
89
 
90
  chunks_list = get_chunks(body.material_id)
91
  chunks_texts = [c["content"] for c in chunks_list] if chunks_list else []
rag/routes.py CHANGED
@@ -42,6 +42,7 @@ class TutorResponse(BaseModel):
42
  @router.post("/ask", response_model=TutorResponse)
43
  async def ask_tutor(
44
  body: TutorQuery,
 
45
  current_user=Depends(get_current_user),
46
  ):
47
  if not body.query.strip():
@@ -73,6 +74,8 @@ async def ask_tutor(
73
  mat = get_material(body.material_id) if body.material_id else None
74
  if not mat:
75
  raise HTTPException(400, f"No {body.source_type} material found. Upload one first.")
 
 
76
 
77
  material_id = body.material_id
78
 
 
42
  @router.post("/ask", response_model=TutorResponse)
43
  async def ask_tutor(
44
  body: TutorQuery,
45
+ user_id: str = Depends(get_current_user_id),
46
  current_user=Depends(get_current_user),
47
  ):
48
  if not body.query.strip():
 
74
  mat = get_material(body.material_id) if body.material_id else None
75
  if not mat:
76
  raise HTTPException(400, f"No {body.source_type} material found. Upload one first.")
77
+ if mat.get("user_id") != user_id:
78
+ raise HTTPException(403, "Access denied")
79
 
80
  material_id = body.material_id
81
 
summary_generator/routes.py CHANGED
@@ -36,6 +36,8 @@ async def generate_summary(
36
  mat = get_material(body.material_id)
37
  if not mat:
38
  raise HTTPException(404, "Material not found")
 
 
39
 
40
  try:
41
  start = time.time()
@@ -74,6 +76,10 @@ async def get_material_summary(
74
  user_id: str = Depends(get_current_user_id),
75
  current_user=Depends(get_current_user),
76
  ):
 
 
 
 
77
  summary = get_stored_summary(material_id)
78
  if not summary:
79
  logger.info("no summary found")
 
36
  mat = get_material(body.material_id)
37
  if not mat:
38
  raise HTTPException(404, "Material not found")
39
+ if mat.get("user_id") != user_id:
40
+ raise HTTPException(403, "Access denied")
41
 
42
  try:
43
  start = time.time()
 
76
  user_id: str = Depends(get_current_user_id),
77
  current_user=Depends(get_current_user),
78
  ):
79
+ mat = get_material(material_id)
80
+ if not mat or mat.get("user_id") != user_id:
81
+ raise HTTPException(403, "Access denied")
82
+
83
  summary = get_stored_summary(material_id)
84
  if not summary:
85
  logger.info("no summary found")