Skip to content

feat: add api function for get user role assignments for role#240

Open
BryanttV wants to merge 9 commits intoopenedx:mainfrom
eduNEXT:bav/get-orgs-for-user
Open

feat: add api function for get user role assignments for role#240
BryanttV wants to merge 9 commits intoopenedx:mainfrom
eduNEXT:bav/get-orgs-for-user

Conversation

@BryanttV
Copy link
Copy Markdown
Contributor

@BryanttV BryanttV commented Mar 25, 2026

Description

This PR creates a new api function: get_user_role_assignments_for_role to fetch a subject's role assignments filtered by a specific role.

Additionally, it adds the org property to ContentLibraryData and CourseOverviewData. This is necessary to get the specific organization for each scope's data, not only for glob scopes.

Related Issues

Testing Instructions

  1. Add different roles to a user in different scopes.
  2. Now, use the get_user_role_assignments_for_role function:
from openedx_authz.api import assign_role_to_user_in_scope, get_user_role_assignments_for_role

assign_role_to_user_in_scope("marie", "course_staff", "course-v1:OpenedX+DemoX+DemoCourse")
assign_role_to_user_in_scope("marie", "course_staff", "course-v1:WGU+Python+2020")
assign_role_to_user_in_scope("marie", "course_staff", "course-v1:OpenedX+RBAC+2026")
assign_role_to_user_in_scope("marie", "course_auditor", "course-v1:XBlocks.org+XBlocks+2026")
assign_role_to_user_in_scope("marie", "course_auditor", "course-v1:WGUv2+Python+2025")
assign_role_to_user_in_scope("marie", "course_auditor", "course-v1:AXIM+*")

>>> get_user_role_assignments_for_role("marie", "course_staff")
[
    user^marie => [role^course_staff] @ course-v1^course-v1:OpenedX+DemoX+DemoCourse,
    user^marie => [role^course_staff] @ course-v1^course-v1:WGU+AdvancedPython+2026,
    user^marie => [role^course_staff] @ course-v1^course-v1:OpenedX+RBAC+2026,
]

>>> get_user_role_assignments_for_role("marie", "course_auditor") 
[
    user^marie => [role^course_auditor] @ course-v1^course-v1:XBlocks.org+XBlocks+2026,
    user^marie => [role^course_auditor] @ course-v1^course-v1:WGUv2+Python+2025,
    user^marie => [role^course_auditor] @ course-v1^course-v1:AXIM+*
]

Merge checklist

Check off if complete or not applicable:

  • Version bumped
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Mar 25, 2026
@openedx-webhooks
Copy link
Copy Markdown

openedx-webhooks commented Mar 25, 2026

Thanks for the pull request, @BryanttV!

This repository is currently maintained by @openedx/committers-openedx-authz.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@BryanttV BryanttV changed the title feat: add api function for get user role assignment for role feat: add api function for get user role assignments for role Mar 27, 2026
Comment on lines +327 to +341
def get_subject_role_assignments_for_role(subject: SubjectData, role: RoleData) -> list[RoleAssignmentData]:
"""Get role assignments for a subject filtered by a specific role across all scopes.

Args:
subject: The SubjectData object representing the subject.
role: The RoleData object representing the role to filter by.

Returns:
list[RoleAssignmentData]: A list of assignments where the subject has the specified role.
"""
return [
assignment
for assignment in get_subject_role_assignments(subject)
if any(assigned_role.namespaced_key == role.namespaced_key for assigned_role in assignment.roles)
]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what'd be better performance-wise, but instead of doing two passes on the return of the enforcer, we could implement a get_subect_role_assignments_filtered_by filtered by role key:

def get_subject_role_assignments(subject: SubjectData) -> list[RoleAssignmentData]:
"""Get all the roles for a subject across all scopes.
Args:
subject: The SubjectData object representing the subject (e.g., SubjectData(external_key='john_doe')).
Returns:
list[RoleAssignmentData]: A list of role assignments for the subject.
"""
enforcer = AuthzEnforcer.get_enforcer()
role_assignments = []
for policy in enforcer.get_filtered_grouping_policy(GroupingPolicyIndex.SUBJECT.value, subject.namespaced_key):
role = RoleData(namespaced_key=policy[GroupingPolicyIndex.ROLE.value])
role.permissions = get_permissions_for_single_role(role)
role_assignments.append(
RoleAssignmentData(
subject=subject,
roles=[role],
scope=ScopeData(namespaced_key=policy[GroupingPolicyIndex.SCOPE.value]),
)
)
return role_assignments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @mariajgrimaldi! Based on your suggestion, I’ve been thinking about implementing a method to retrieve role assignments filtered by subject, role, and/or scope. I think this could be quite useful for different use cases.

I reviewed the Casbin Management API, specifically the get_filtered_grouping_policy method, which we already use in our API functions, but it offers more fine-grained filtering that we can take advantage of for our needs.

This led me to implement a function called get_role_assignments(), which takes three optional arguments (subject, role, and scope). This allows us to perform queries to retrieve assignments by:

  1. Only user, only role, or only scope.
  2. Combinations like user + role, user + scope, role + scope, etc.

What do you think?

@BryanttV BryanttV marked this pull request as ready for review March 27, 2026 15:19

NAMESPACE: ClassVar[str] = "lib"

@property
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be cached?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could cache it. Although it might be better to cache the library_key/course_key property, since that’s what handles the parsing. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, that sounds good!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@BryanttV BryanttV force-pushed the bav/get-orgs-for-user branch from 86a0a56 to fece3a3 Compare March 27, 2026 15:22
@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Mar 27, 2026
@BryanttV BryanttV force-pushed the bav/get-orgs-for-user branch from c79a426 to 85b4f8b Compare March 27, 2026 22:26
@BryanttV BryanttV force-pushed the bav/get-orgs-for-user branch from 85b4f8b to 242a962 Compare March 27, 2026 22:29
@BryanttV BryanttV requested a review from mariajgrimaldi March 28, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: In Eng Review

Development

Successfully merging this pull request may close these issues.

4 participants