-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Roles 15 - permission checks back end changes part 1 #33347
Merged
julianpalmerio
merged 18 commits into
ROLES-2-course_roles_setup
from
julianpalmerio/ROLES-15-permission-checks-back-end-changes-part-1
Sep 29, 2023
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ddcccf1
test: add test cases for permission list check functions
julianpalmerio 6c79a3d
test: update tests
julianpalmerio 8341962
feat: add helper functions to check lists of permissions
julianpalmerio ee11185
style: improve code style
julianpalmerio 883d15b
feat: add course roles checks in the contentstore app
julianpalmerio 4f887d3
feat: add course roles checks in the student app
julianpalmerio 6e5de16
feat: add course roles checks in the lms discussion app
julianpalmerio 56ed879
feat: add course roles checks in the lms instructor app
julianpalmerio 64c4146
feat: add course roles checks in the Learning Sequences package
julianpalmerio e511114
style: fix code style
julianpalmerio 19c0039
fix: course_permission_check calls
julianpalmerio 570d12d
feat: add validation for AnonymousUser in course permission check hel…
julianpalmerio d977e37
fix: disable some pylint warnings
julianpalmerio 782c056
test: update number of querys asserted in has_course_author_access
julianpalmerio e64f1f6
feat: add helper functions to check course or organization permissions
julianpalmerio b2d6932
test: update course_roles tests
julianpalmerio 3a6d2eb
feat: replace course or organization helper functions in auth
julianpalmerio 2233cb0
docs: update course_roles docstrings
julianpalmerio 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,30 +1,80 @@ | ||
""" | ||
Helpers for the course roles app. | ||
""" | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
from openedx.core.djangoapps.course_roles.models import CourseRolesUserRole | ||
from openedx.core.lib.cache_utils import request_cached | ||
from xmodule.modulestore.django import modulestore | ||
|
||
|
||
@request_cached() | ||
def course_permission_check(user, permission_name, course_id): | ||
""" | ||
Check if a user has a permission in a course. | ||
""" | ||
if isinstance(user, AnonymousUser): | ||
return False | ||
return CourseRolesUserRole.objects.filter( | ||
user=user, | ||
role__permissions__name=permission_name, | ||
course=course_id, | ||
).exists() | ||
|
||
|
||
@request_cached() | ||
def course_permissions_list_check(user, permission_names, course_id): | ||
""" | ||
Check if a user has all of the given permissions in a course. | ||
""" | ||
return all(course_permission_check(user, permission_name, course_id) for permission_name in permission_names) | ||
|
||
|
||
@request_cached() | ||
def organization_permission_check(user, permission_name, organization_name): | ||
""" | ||
Check if a user has a permission in an organization. | ||
""" | ||
if isinstance(user, AnonymousUser): | ||
return False | ||
return CourseRolesUserRole.objects.filter( | ||
user=user, | ||
role__permissions__name=permission_name, | ||
course__isnull=True, | ||
org__name=organization_name, | ||
).exists() | ||
|
||
|
||
@request_cached() | ||
def organization_permissions_list_check(user, permission_names, organization_name): | ||
""" | ||
Check if a user has all of the given permissions in an organization. | ||
""" | ||
return all( | ||
organization_permission_check(user, permission_name, organization_name) for permission_name in permission_names | ||
) | ||
|
||
|
||
@request_cached() | ||
def course_or_organization_permission_check(user, permission_name, course_id, organization_name=None): | ||
""" | ||
Check if a user has a permission in an organization or a course. | ||
""" | ||
if isinstance(user, AnonymousUser): | ||
return False | ||
if organization_name is None: | ||
organization_name = modulestore().get_course(course_id).org | ||
return (course_permission_check(user, permission_name, course_id) or | ||
organization_permission_check(user, permission_name, organization_name) | ||
) | ||
|
||
|
||
@request_cached() | ||
def course_or_organization_permission_list_check(user, permission_names, course_id, organization_name=None): | ||
""" | ||
Check if a user has all of the given permissions in an organization or a course. | ||
""" | ||
return all( | ||
course_or_organization_permission_check(user, permission_name, course_id, organization_name) | ||
for permission_name in permission_names | ||
) |
Oops, something went wrong.
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.
Consider adding clarity by explaining which type of course roles you mean (student_courseaccessrole, django_comment_client_role, or course_roles_roles)
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 wanted to refer to the course_roles Django App . What you think about changing the comment to: "# TODO: course roles: Remove this validation when implementing course_roles django app."?
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 that is a positive change.
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.
done