-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Roles 15 - permission checks back end changes part 1 (#33347)
* test: add test cases for permission list check functions * test: update tests * feat: add helper functions to check lists of permissions * style: improve code style * feat: add course roles checks in the contentstore app * feat: add course roles checks in the student app * feat: add course roles checks in the lms discussion app * feat: add course roles checks in the lms instructor app * feat: add course roles checks in the Learning Sequences package * style: fix code style * fix: course_permission_check calls * feat: add validation for AnonymousUser in course permission check helper functions * fix: disable some pylint warnings * test: update number of querys asserted in has_course_author_access * feat: add helper functions to check course or organization permissions * test: update course_roles tests * feat: replace course or organization helper functions in auth * docs: update course_roles docstrings
- Loading branch information
1 parent
675006e
commit 29e7975
Showing
10 changed files
with
497 additions
and
19 deletions.
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.