-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make impersonate_user local to a course #670
base: main
Are you sure you want to change the base?
Changes from 1 commit
dfde126
a7f9942
f6d4c83
04c38ae
97cdba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,8 +59,9 @@ | |
user_status, | ||
participation_status, | ||
participation_permission as pperm, | ||
COURSE_ID_REGEX, | ||
) | ||
from course.models import Participation, ParticipationRole, AuthenticationToken # noqa | ||
from course.models import Participation, ParticipationRole, AuthenticationToken, Course # noqa | ||
from accounts.models import User | ||
from course.utils import render_course_page, course_view | ||
|
||
|
@@ -82,14 +83,15 @@ def get_pre_impersonation_user(request): | |
return None | ||
|
||
|
||
def get_impersonable_user_qset(impersonator): | ||
def get_impersonable_user_qset(impersonator, course_identifier): | ||
# type: (User) -> query.QuerySet | ||
if impersonator.is_superuser: | ||
return User.objects.exclude(pk=impersonator.pk) | ||
|
||
course = Course.objects.get(identifier=course_identifier) | ||
|
||
my_participations = Participation.objects.filter( | ||
user=impersonator, | ||
status=participation_status.active) | ||
status=participation_status.active, | ||
course=course) | ||
|
||
impersonable_user_qset = User.objects.none() | ||
for part in my_participations: | ||
|
@@ -120,13 +122,23 @@ def get_impersonable_user_qset(impersonator): | |
return impersonable_user_qset | ||
|
||
|
||
def _get_current_course_from_request(request): | ||
course_match = re.match("^/course/"+COURSE_ID_REGEX+"/", request.get_full_path()) | ||
if course_match is None: | ||
return None | ||
else: | ||
return course_match.group("course_identifier") | ||
|
||
|
||
class ImpersonateMiddleware(object): | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
if 'impersonate_id' in request.session: | ||
imp_id = request.session['impersonate_id'] | ||
imp_course = request.session['impersonate_course'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you change this variable to be end in |
||
cur_course = _get_current_course_from_request(request) | ||
impersonee = None | ||
|
||
try: | ||
|
@@ -140,13 +152,20 @@ def __call__(self, request): | |
if request.user.is_superuser: | ||
may_impersonate = True | ||
else: | ||
qset = get_impersonable_user_qset(cast(User, request.user)) | ||
if cur_course is not None: | ||
qset = get_impersonable_user_qset(cast(User, request.user), | ||
course_identifier=cur_course) | ||
else: | ||
qset = get_impersonable_user_qset(cast(User, request.user), | ||
course_identifier=imp_course) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the impersonation is course-scoped, no impersonation should happen if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have to selectively allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I don't think that's necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for sign-out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. But if they're already impersonating someone, it's fine to assume that they were (at some point) permitted to do that. |
||
if qset.filter(pk=cast(User, impersonee).pk).count(): | ||
may_impersonate = True | ||
|
||
if may_impersonate: | ||
request.relate_impersonate_original_user = request.user | ||
request.user = impersonee | ||
elif cur_course is not None and cur_course != imp_course: | ||
raise PermissionDenied() | ||
else: | ||
messages.add_message(request, messages.ERROR, | ||
_("Error while impersonating.")) | ||
|
@@ -209,12 +228,17 @@ def __init__(self, *args, **kwargs): | |
self.helper.add_input(Submit("submit", _("Impersonate"))) | ||
|
||
|
||
def impersonate(request): | ||
def impersonate(request, course_identifier): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should have a non-course entry point (URL) for the (global) impersonation. (Not for me, but I'm thinking of other folks serving as admins of Relate instances.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would they be superusers? Or users with a relate permission level? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep. |
||
# type: (http.HttpRequest) -> http.HttpResponse | ||
if not request.user.is_authenticated: | ||
raise PermissionDenied() | ||
|
||
impersonable_user_qset = get_impersonable_user_qset(cast(User, request.user)) | ||
impersonator = cast(User, request.user) | ||
if impersonator.is_superuser: | ||
impersonable_user_qset = User.objects.exclude(pk=impersonator.pk) | ||
else: | ||
impersonable_user_qset = get_impersonable_user_qset(impersonator, | ||
course_identifier) | ||
if not impersonable_user_qset.count(): | ||
raise PermissionDenied() | ||
|
||
|
@@ -234,6 +258,7 @@ def impersonate(request): | |
impersonee = form.cleaned_data["user"] | ||
|
||
request.session['impersonate_id'] = impersonee.id | ||
request.session['impersonate_course'] = course_identifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be useful to store the primary key and not the identifier. That's cheaper to use (no database roundtrip). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you filter by the primary key?
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to check that the impersonate_course_id is equal to the current course which requires a database call. I'll change the filter to following which saves a database call.
|
||
request.session['relate_impersonation_header'] = form.cleaned_data[ | ||
"add_impersonation_header"] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only consider the participation in the course to which the impersonation is scoped.