-
Notifications
You must be signed in to change notification settings - Fork 14
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
Enforce dashboard admin access for assignments and users #6475
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,16 @@ | |
|
||
|
||
class DashboardService: | ||
def __init__(self, db, assignment_service, course_service, organization_service): | ||
self._db = db | ||
def __init__( | ||
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. In this module we check access to individual elements. |
||
self, request, assignment_service, course_service, organization_service | ||
): | ||
self._db = request.db | ||
|
||
self._assignment_service = assignment_service | ||
self._course_service = course_service | ||
self._organization_service = organization_service | ||
|
||
def get_request_assignment(self, request): | ||
def get_request_assignment(self, request, admin_organizations: list[Organization]): | ||
"""Get and authorize an assignment for the given request.""" | ||
assigment_id = request.matchdict.get( | ||
"assignment_id" | ||
|
@@ -28,12 +30,20 @@ def get_request_assignment(self, request): | |
# STAFF members in our admin pages can access all assignments | ||
return assignment | ||
|
||
if ( | ||
admin_organizations | ||
and assignment.course.application_instance.organization | ||
in admin_organizations | ||
): | ||
# Organization admins have access to all the assignments in their organizations | ||
return assignment | ||
|
||
if not self._assignment_service.is_member(assignment, request.user.h_userid): | ||
raise HTTPUnauthorized() | ||
|
||
return assignment | ||
|
||
def get_request_course(self, request): | ||
def get_request_course(self, request, admin_organizations: list[Organization]): | ||
"""Get and authorize a course for the given request.""" | ||
course = self._course_service.get_by_id(request.matchdict["course_id"]) | ||
if not course: | ||
|
@@ -43,6 +53,13 @@ def get_request_course(self, request): | |
# STAFF members in our admin pages can access all courses | ||
return course | ||
|
||
if ( | ||
admin_organizations | ||
and course.application_instance.organization in admin_organizations | ||
): | ||
# Organization admins have access to all the courses in their organizations | ||
return course | ||
|
||
if not self._course_service.is_member(course, request.user.h_userid): | ||
raise HTTPUnauthorized() | ||
|
||
|
@@ -74,7 +91,7 @@ def delete_dashboard_admin(self, dashboard_admin_id: int) -> None: | |
|
||
def factory(_context, request): | ||
return DashboardService( | ||
db=request.db, | ||
request=request, | ||
assignment_service=request.find_service(name="assignment"), | ||
course_service=request.find_service(name="course"), | ||
organization_service=request.find_service(OrganizationService), | ||
|
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,14 +1,17 @@ | ||
from functools import lru_cache | ||
from typing import cast | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy import BinaryExpression, false, or_, select | ||
from sqlalchemy.exc import NoResultFound | ||
from sqlalchemy.sql import Select | ||
|
||
from lms.models import ( | ||
ApplicationInstance, | ||
AssignmentGrouping, | ||
AssignmentMembership, | ||
LTIRole, | ||
LTIUser, | ||
Organization, | ||
RoleScope, | ||
RoleType, | ||
User, | ||
|
@@ -107,6 +110,7 @@ def get_users( # noqa: PLR0913, PLR0917 | |
role_scope: RoleScope, | ||
role_type: RoleType, | ||
instructor_h_userid: str | None = None, | ||
admin_organization_ids: list[int] | None = None, | ||
course_ids: list[int] | None = None, | ||
h_userids: list[str] | None = None, | ||
assignment_ids: list[int] | None = None, | ||
|
@@ -117,6 +121,7 @@ def get_users( # noqa: PLR0913, PLR0917 | |
:param role_scope: return only users with this LTI role scope. | ||
:param role_type: return only users with this LTI role type. | ||
:param instructor_h_userid: return only users that belongs to courses/assignments where the user instructor_h_userid is an instructor. | ||
:param admin_organization_ids: organizations where the current user is an admin. | ||
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. Fetch users from courses where the users is an instructor or an admin. |
||
:param h_userids: return only users with a h_userid in this list. | ||
:param course_ids: return only users that belong to these courses. | ||
:param assignment_ids: return only users that belong these assignments. | ||
|
@@ -128,20 +133,36 @@ def get_users( # noqa: PLR0913, PLR0917 | |
.where(LTIRole.scope == role_scope, LTIRole.type == role_type) | ||
) | ||
|
||
# Let's crate no op clauses by default to avoid having to check the presence of these filters | ||
instructor_h_userid_clause = cast(BinaryExpression, false()) | ||
admin_organization_ids_clause = cast(BinaryExpression, false()) | ||
|
||
if instructor_h_userid: | ||
query = query.where( | ||
AssignmentMembership.assignment_id.in_( | ||
select(AssignmentMembership.assignment_id) | ||
.join(User) | ||
.join(LTIRole) | ||
.where( | ||
User.h_userid == instructor_h_userid, | ||
LTIRole.scope == RoleScope.COURSE, | ||
LTIRole.type == RoleType.INSTRUCTOR, | ||
) | ||
instructor_h_userid_clause = AssignmentMembership.assignment_id.in_( | ||
select(AssignmentMembership.assignment_id) | ||
.join(User) | ||
.join(LTIRole) | ||
.where( | ||
User.h_userid == instructor_h_userid, | ||
LTIRole.scope == RoleScope.COURSE, | ||
LTIRole.type == RoleType.INSTRUCTOR, | ||
) | ||
) | ||
|
||
if admin_organization_ids: | ||
admin_organization_ids_clause = User.id.in_( | ||
select(User.id) | ||
.join(ApplicationInstance) | ||
.join(Organization) | ||
.where(Organization.id.in_(admin_organization_ids)) | ||
) | ||
|
||
# instructor_h_userid and admin_organization_ids are about access rather than filtering. | ||
# we apply them both as an or to fetch users where the users is either an instructor or an admin | ||
query = query.where( | ||
or_(instructor_h_userid_clause, admin_organization_ids_clause) | ||
) | ||
|
||
if h_userids: | ||
query = query.where(User.h_userid.in_(h_userids)) | ||
|
||
|
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
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.
Same pattern as for courses, fetch assignments where the users is an instructor or an admin.