Skip to content

Commit 9b95742

Browse files
committed
fixed the permissions so that there is separation of concerns, + fixedthe update
1 parent 07c9310 commit 9b95742

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

csm_web/scheduler/views/user.py

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,36 @@ def has_permission(request_user, target_user):
3232
"""
3333
Returns True if the user has permission to access or edit the target user's profile
3434
"""
35-
# Does not account for users who are both mentors and students of different courses
36-
# need separation of concerns:
37-
# coordinators/mentor should only have coordinator/mentor access for their course
38-
# Check if request_user is a mentor
35+
36+
# if request_user.is_superuser: return True
37+
if request_user == target_user:
38+
return True
39+
40+
# if requestor is a student, get all the sections they are in
41+
# if the target user is a student in any of those sections, return True
42+
if Student.objects.filter(user=request_user).exists():
43+
if Student.objects.filter(user=target_user).exists():
44+
request_user_sections = Student.objects.filter(
45+
user=request_user
46+
).values_list("section", flat=True)
47+
target_user_sections = Student.objects.filter(user=target_user).values_list(
48+
"section", flat=True
49+
)
50+
if set(request_user_sections) & set(target_user_sections):
51+
return True
52+
53+
# if the target user is a mentor in any of the courses the student is in, return True
54+
if Mentor.objects.filter(user=target_user).exists():
55+
target_user_courses = Mentor.objects.filter(user=target_user).values_list(
56+
"course", flat=True
57+
)
58+
if Student.objects.filter(
59+
user=request_user, course__in=target_user_courses
60+
).exists():
61+
return True
62+
63+
# if requestor is a mentor, get all the courses they mentor
64+
# if the target user is a student or mentor in any of those courses, return True
3965
if Mentor.objects.filter(user=request_user).exists():
4066
mentor_courses = Mentor.objects.filter(user=request_user).values_list(
4167
"course", flat=True
@@ -46,31 +72,24 @@ def has_permission(request_user, target_user):
4672
if Mentor.objects.filter(user=target_user, course__in=mentor_courses).exists():
4773
return True
4874

49-
# Check if request_user is a student in the same course as target_user
50-
# Students in the same section can see each other
51-
if (
52-
Student.objects.filter(user=request_user).exists()
53-
and Student.objects.filter(user=target_user).exists()
54-
):
55-
request_user_courses = Student.objects.filter(user=request_user).values_list(
75+
# if requestor is a coordinator, get all the courses they coordinate
76+
# if the target user is a student or mentor in any of those courses, return True
77+
if Coordinator.objects.filter(user=request_user).exists():
78+
coordinator_courses = Coordinator.objects.filter(user=request_user).values_list(
5679
"course", flat=True
5780
)
58-
target_user_courses = Student.objects.filter(user=target_user).values_list(
59-
"course", flat=True
60-
)
61-
62-
if set(request_user_courses) & set(target_user_courses):
81+
if Student.objects.filter(
82+
user=target_user, course__in=coordinator_courses
83+
).exists():
84+
return True
85+
if Mentor.objects.filter(
86+
user=target_user, course__in=coordinator_courses
87+
).exists():
88+
return True
89+
if Coordinator.objects.filter(
90+
user=target_user, course__in=coordinator_courses
91+
).exists():
6392
return True
64-
65-
# Coordinator access
66-
if Coordinator.objects.filter(
67-
user=request_user
68-
).exists(): # or if request_user.is_superuser
69-
return True
70-
71-
# Request user accessing their own profile
72-
if request_user == target_user:
73-
return True
7493

7594
return False
7695

@@ -97,14 +116,22 @@ def user_update(request, pk):
97116
"""
98117
Update user profile. Only accessible by Coordinators and the user themselves.
99118
"""
119+
# raise PermissionDenied("You do not have permission to edit this profile")
100120
try:
101121
user = User.objects.get(pk=pk)
102122
except User.DoesNotExist:
103123
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)
104124

105-
if not (
106-
(request.user == user) or Coordinator.objects.filter(user=request.user).exists()
107-
):
125+
if not request.user == user:
126+
raise PermissionDenied("You do not have permission to edit this profile")
127+
128+
coordinator_courses = Coordinator.objects.filter(user=request.user).values_list(
129+
"course", flat=True
130+
)
131+
132+
if not Coordinator.objects.filter(
133+
user=request.user, course_in=coordinator_courses
134+
).exists():
108135
raise PermissionDenied("You do not have permission to edit this profile")
109136

110137
serializer = UserSerializer(user, data=request.data, partial=True)

0 commit comments

Comments
 (0)