Skip to content

Commit bba656b

Browse files
Merge pull request #58 from sustech-cs304/backend
Backend
2 parents ef33106 + e4aa38c commit bba656b

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

backend/app/coding/__init__.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,14 @@ async def create_environment_file(
218218
if not env:
219219
raise HTTPException(status_code=404, detail="Environment not found")
220220

221-
if env.user_id != current_user.user_id:
222-
raise HTTPException(status_code=403, detail="No privilege for creating the file.")
221+
if env.is_collaborative:
222+
group_id = env.group_id
223+
group = db.query(Group).filter(Group.group_id == group_id).first()
224+
if not group or current_user.user_id not in group.users:
225+
raise HTTPException(status_code=403, detail="You are not a member of this group")
226+
else:
227+
if env.user_id != current_user.user_id:
228+
raise HTTPException(status_code=403, detail="You are not the owner of this environment")
223229

224230
env_path = f"/app/data/{env_id}/{file_path}"
225231
os.makedirs(os.path.dirname(env_path), exist_ok=True)
@@ -240,8 +246,14 @@ async def create_environment_directory(
240246
if not env:
241247
raise HTTPException(status_code=404, detail="Environment not found")
242248

243-
if current_user.user_id != env.user_id:
244-
raise HTTPException(status_code=403, detail="No privilege for creating the directory.")
249+
if env.is_collaborative:
250+
group_id = env.group_id
251+
group = db.query(Group).filter(Group.group_id == group_id).first()
252+
if not group or current_user.user_id not in group.users:
253+
raise HTTPException(status_code=403, detail="You are not a member of this group")
254+
else:
255+
if env.user_id != current_user.user_id:
256+
raise HTTPException(status_code=403, detail="You are not the owner of this environment")
245257

246258
env_path = f"/app/data/{env_id}"
247259
directory_path = os.path.join(env_path, directory_path.lstrip('/'))
@@ -264,9 +276,15 @@ async def update_environment_path(
264276
env = db.query(Environment).filter(Environment.environment_id == env_id).first()
265277
if not env:
266278
raise HTTPException(status_code=404, detail="Environment not found")
267-
268-
if current_user.user_id != env.user_id:
269-
raise HTTPException(status_code=403, detail="No privilege for modifying the file.")
279+
280+
if env.is_collaborative:
281+
group_id = env.group_id
282+
group = db.query(Group).filter(Group.group_id == group_id).first()
283+
if not group or current_user.user_id not in group.users:
284+
raise HTTPException(status_code=403, detail="You are not a member of this group")
285+
else:
286+
if env.user_id != current_user.user_id:
287+
raise HTTPException(status_code=403, detail="You are not the owner of this environment")
270288

271289
env_path = f"/app/data/{env_id}"
272290
origin_path = os.path.join(env_path, from_uri.lstrip('/'))
@@ -304,8 +322,14 @@ async def delete_environment_path(
304322
if not env:
305323
raise HTTPException(status_code=404, detail="Environment not found")
306324

307-
if current_user.user_id != env.user_id:
308-
raise HTTPException(status_code=403, detail="No privilege for deleting the file.")
325+
if env.is_collaborative:
326+
group_id = env.group_id
327+
group = db.query(Group).filter(Group.group_id == group_id).first()
328+
if not group or current_user.user_id not in group.users:
329+
raise HTTPException(status_code=403, detail="You are not a member of this group")
330+
else:
331+
if env.user_id != current_user.user_id:
332+
raise HTTPException(status_code=403, detail="You are not the owner of this environment")
309333

310334
env_path = f"/app/data/{env_id}"
311335
file_path = os.path.join(env_path, uri.lstrip('/'))

0 commit comments

Comments
 (0)