|
20 | 20 | from courseware.models import StudentModule |
21 | 21 | from courseware.views import get_module_for_descriptor, save_child_position, get_current_child |
22 | 22 | from django_comment_common.models import FORUM_ROLE_MODERATOR |
23 | | -from instructor.access import allow_access, revoke_access, update_forum_role |
| 23 | +from instructor.access import revoke_access, update_forum_role |
24 | 24 | from lang_pref import LANGUAGE_KEY |
25 | 25 | from lms.lib.comment_client.user import User as CommentUser |
26 | 26 | from lms.lib.comment_client.utils import CommentClientRequestError |
27 | 27 | from student.models import CourseEnrollment, PasswordHistory, UserProfile |
28 | | -from student.roles import CourseInstructorRole, CourseObserverRole, CourseStaffRole, UserBasedRole |
| 28 | +from student.roles import CourseAccessRole, CourseInstructorRole, CourseObserverRole, CourseStaffRole, UserBasedRole |
29 | 29 | from user_api.models import UserPreference |
30 | 30 | from util.bad_request_rate_limiter import BadRequestRateLimiter |
31 | 31 | from util.password_policy_validators import ( |
@@ -98,24 +98,30 @@ def _manage_role(course_descriptor, user, role, action): |
98 | 98 | """ |
99 | 99 | Helper method for managing course/forum roles |
100 | 100 | """ |
| 101 | + supported_roles = ('instructor', 'staff', 'observer') |
101 | 102 | forum_moderator_roles = ('instructor', 'staff') |
| 103 | + if role not in supported_roles: |
| 104 | + raise ValueError |
102 | 105 | if action is 'allow': |
103 | | - allow_access(course_descriptor, user, role) |
| 106 | + existing_role = CourseAccessRole.objects.filter(user=user, role=role, course_id=course_descriptor.id, org=course_descriptor.org) |
| 107 | + if not existing_role: |
| 108 | + new_role = CourseAccessRole(user=user, role=role, course_id=course_descriptor.id, org=course_descriptor.org) |
| 109 | + new_role.save() |
104 | 110 | if role in forum_moderator_roles: |
105 | 111 | update_forum_role(course_descriptor.id, user, FORUM_ROLE_MODERATOR, 'allow') |
106 | 112 | elif action is 'revoke': |
| 113 | + revoke_access(course_descriptor, user, role) |
107 | 114 | if role in forum_moderator_roles: |
108 | 115 | # There's a possibilty that the user may play more than one role in a course |
109 | 116 | # And that more than one of these roles allow for forum moderation |
110 | | - # So we need to confirm the current role is the only one for this user for this course |
| 117 | + # So we need to confirm the removed role was the only role for this user for this course |
111 | 118 | # Before we can safely remove the corresponding forum moderator role |
112 | 119 | user_instructor_courses = UserBasedRole(user, CourseInstructorRole.ROLE).courses_with_role() |
113 | 120 | user_staff_courses = UserBasedRole(user, CourseStaffRole.ROLE).courses_with_role() |
114 | 121 | queryset = user_instructor_courses | user_staff_courses |
115 | 122 | queryset = queryset.filter(course_id=course_descriptor.id) |
116 | | - if len(queryset) == 1: |
117 | | - update_forum_role(course_descriptor.id, user, FORUM_ROLE_MODERATOR, 'allow') |
118 | | - revoke_access(course_descriptor, user, role) |
| 123 | + if len(queryset) == 0: |
| 124 | + update_forum_role(course_descriptor.id, user, FORUM_ROLE_MODERATOR, 'revoke') |
119 | 125 |
|
120 | 126 |
|
121 | 127 | class UsersList(SecureListAPIView): |
@@ -1140,6 +1146,10 @@ def get_queryset(self): |
1140 | 1146 | raise Http404 |
1141 | 1147 | queryset = queryset.filter(course_id=course_key) |
1142 | 1148 |
|
| 1149 | + role = self.request.QUERY_PARAMS.get('role', None) |
| 1150 | + if role: |
| 1151 | + queryset = queryset.filter(role=role) |
| 1152 | + |
1143 | 1153 | return queryset |
1144 | 1154 |
|
1145 | 1155 | def post(self, request, user_id): |
@@ -1217,9 +1227,7 @@ def delete(self, request, user_id, role, course_id): # pylint: disable=W0613 |
1217 | 1227 | return Response({}, status=status.HTTP_404_NOT_FOUND) |
1218 | 1228 |
|
1219 | 1229 | try: |
1220 | | - revoke_access(course_descriptor, user, role) |
1221 | | - if role in ('instructor', 'staff'): |
1222 | | - update_forum_role(course_key, user, FORUM_ROLE_MODERATOR, 'revoke') |
| 1230 | + _manage_role(course_descriptor, user, role, 'revoke') |
1223 | 1231 | except ValueError: |
1224 | 1232 | return Response({}, status=status.HTTP_404_NOT_FOUND) |
1225 | 1233 |
|
|
0 commit comments