-
Notifications
You must be signed in to change notification settings - Fork 71
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
refactor: modify REST API endpoints for LR MFE #1760
Conversation
ff92630
to
1b7da27
Compare
Allows access if the requesting user is the owner of the record, a staff member, or a superuser. | ||
""" | ||
|
||
def has_permission(self, request, view): |
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.
How does this check if the requesting user is the owner of the record? I can see that it works in the unit tests, but I don't understand the mechanism.
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.
Oh nice catch! It actually doesn't. I need to change the documentation. I initially thought we'd allow the user to do this but now I'm under the impression that a learner/normal user should never need to use this username
query parameter.
programs = get_user_program_data( | ||
searched_user, request.site, include_empty_programs=False, include_retired_programs=True | ||
) | ||
else: | ||
programs = get_user_program_data( | ||
request.user.username, request.site, include_empty_programs=False, include_retired_programs=True |
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.
A couple of things...
-
I think the information we're passing to
get_user_program_data
here differs in yourif
vs. yourelse
. In theif
, you are sending the entire User object toget_user_program_data
. In yourelse
, you are sending just the username. Which should it be? -
I think we want to code a bit more defensively. I think this code has a chance to throw an exception and maybe not return any data back to the caller. It's not guaranteed a user will exist with the username passed in to the API call. A call to
{Model}.objects.get(blah=blah)
will throw aDoesNotExist
exception if the object doesn't exist. We will likely want to do something like...
username = request.user.username
query_param_username = request.query_params.get("username", "")
if query_param_username:
try:
other_user = User.objects.get(username=query_param_username)
except User.DoesNotExist:
# Log something here
return Response({"enrolled_programs": []}) # empty list, string, dictionary, whatever we would pass back when there is no data
else:
(else section only executes if the "try" didn't throw an exception)
username = other_user.username
programs = get_user_program_data(
username, request.site, include_empty_programs=False, include_retired_programs=True
)
This way we also cut down on code duplication of the call to get_user_program_data
.
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.
I had a feeling there may be a better way to do this. Thanks for the example!
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.
An interesting note about the first question you had: It is supposed to be a username, however, it was still working by passing in the User
. Is this some Django magic perhaps?
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.
Let me know if you have any questions regarding my comments/suggestions!
1b7da27
to
ffb09ea
Compare
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.
I had a few minor requests to update logs and comments but I don't need to re-review afterwards. Thanks, Max!
54679b9
to
cad54de
Compare
cad54de
to
35f1392
Compare
This is a PR to modify the existing REST API endpoints used by the LR MFE. This modification adds a
username
query param that is employed the the Support Tools MFE to fetch the same data learners see.Custom permissions classes were necessary here to block access from unauthorized users. We needed to ensure that learners (or other non-staff, non-superusers) couldn't view the records of others.
Here is a truth table that attempts to illustrate how access is handled with the addition of this query parameter.