-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix: authz compat layer was failing on libraries v2 keys #38131
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
Open
rodmgwgu
wants to merge
3
commits into
openedx:master
Choose a base branch
from
WGU-Open-edX:rod/fix-compat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+28
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,11 @@ | |
| from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user | ||
| from common.djangoapps.student.signals.signals import emit_course_access_role_added, emit_course_access_role_removed | ||
| from opaque_keys.edx.django.models import CourseKeyField | ||
| from opaque_keys import InvalidKeyError | ||
| from opaque_keys.edx.keys import CourseKey | ||
| from opaque_keys.edx.locator import CourseLocator | ||
| from opaque_keys.edx.locator import CourseLocator, LibraryLocatorV2 | ||
| from openedx_authz.api import users as authz_api | ||
| from openedx_authz.api.data import RoleAssignmentData | ||
| from openedx_authz.constants import roles as authz_roles | ||
|
|
||
| from common.djangoapps.student.models import CourseAccessRole | ||
|
|
@@ -73,6 +75,24 @@ def authz_add_role(user: User, authz_role: str, course_key: str): | |
| legacy_role = get_legacy_role_from_authz_role(authz_role) | ||
| emit_course_access_role_added(user, course_locator, course_locator.org, legacy_role) | ||
|
|
||
| def authz_get_all_course_assignments_for_user(user: User) -> list[RoleAssignmentData]: | ||
| """ | ||
| Get all course assignments for a user. | ||
| """ | ||
| assignments = authz_api.get_user_role_assignments(user_external_key=user.username) | ||
| # filter courses only | ||
| filtered_assignments = [assignment for assignment in assignments if assignment.scope.NAMESPACE == 'course-v1'] | ||
| return filtered_assignments | ||
|
|
||
| def get_org_from_key(key: str) -> str: | ||
|
Contributor
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. Since we filter by the |
||
| """ | ||
| Get the org from a course or library key. | ||
| """ | ||
| try: | ||
| parsed_key = CourseKey.from_string(key) | ||
| except InvalidKeyError: | ||
| parsed_key = LibraryLocatorV2.from_string(key) | ||
| return parsed_key.org | ||
|
|
||
| def register_access_role(cls): | ||
| """ | ||
|
|
@@ -136,13 +156,12 @@ def get_authz_compat_course_access_roles_for_user(user: User) -> set[AuthzCompat | |
| Retrieve all CourseAccessRole objects for a given user and convert them to AuthzCompatCourseAccessRole objects. | ||
| """ | ||
| compat_role_assignments = set() | ||
| assignments = authz_api.get_user_role_assignments(user_external_key=user.username) | ||
| assignments = authz_get_all_course_assignments_for_user(user) | ||
| for assignment in assignments: | ||
| for role in assignment.roles: | ||
| legacy_role = get_legacy_role_from_authz_role(authz_role=role.external_key) | ||
| course_key = assignment.scope.external_key | ||
| parsed_key = CourseKey.from_string(course_key) | ||
| org = parsed_key.org | ||
| org = get_org_from_key(course_key) | ||
| compat_role = AuthzCompatCourseAccessRole( | ||
| user_id=user.id, | ||
| username=user.username, | ||
|
|
@@ -825,9 +844,7 @@ def courses_with_role(self) -> set[AuthzCompatCourseAccessRole]: | |
|
|
||
| # Get all assignments for a user to a role | ||
| new_authz_roles = [get_authz_role_from_legacy_role(role) for role in roles] | ||
| all_authz_user_assignments = authz_api.get_user_role_assignments( | ||
| user_external_key=self.user.username | ||
| ) | ||
| all_authz_user_assignments = authz_get_all_course_assignments_for_user(self.user) | ||
|
|
||
| all_assignments = set() | ||
|
|
||
|
|
@@ -847,8 +864,7 @@ def courses_with_role(self) -> set[AuthzCompatCourseAccessRole]: | |
| continue | ||
| legacy_role = get_legacy_role_from_authz_role(authz_role=role.external_key) | ||
| course_key = assignment.scope.external_key | ||
| parsed_key = CourseKey.from_string(course_key) | ||
| org = parsed_key.org | ||
| org = get_org_from_key(course_key) | ||
| all_assignments.add(AuthzCompatCourseAccessRole( | ||
| user_id=self.user.id, | ||
| username=self.user.username, | ||
|
|
@@ -880,9 +896,7 @@ def has_courses_with_role(self, org: str | None = None) -> bool: | |
|
|
||
| # Then check for authz assignments | ||
| new_authz_roles = [get_authz_role_from_legacy_role(role) for role in roles] | ||
| all_authz_user_assignments = authz_api.get_user_role_assignments( | ||
| user_external_key=self.user.username | ||
| ) | ||
| all_authz_user_assignments = authz_get_all_course_assignments_for_user(self.user) | ||
|
|
||
| for assignment in all_authz_user_assignments: | ||
| for role in assignment.roles: | ||
|
|
@@ -892,7 +906,7 @@ def has_courses_with_role(self, org: str | None = None) -> bool: | |
| # There is at least one assignment, short circuit | ||
| return True | ||
| course_key = assignment.scope.external_key | ||
| parsed_key = CourseKey.from_string(course_key) | ||
| if org == parsed_key.org: | ||
| parsed_org = get_org_from_key(course_key) | ||
| if org == parsed_org: | ||
| return True | ||
| return False | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think I would lean towards checking the class instead. What do you think?