Skip to content

Conversation

@mariajgrimaldi
Copy link
Member

@mariajgrimaldi mariajgrimaldi commented Sep 30, 2025

Description

This PR introduces the public Python APIs for Open edX AuthZ. These modules expose role, permission, and user-assignment operations as an abstraction on top of the Casbin engine. The design follows a bottom-up approach: a type-safe data layer, low-level APIs for roles and permissions, and a user-facing convenience API (user-assignment).

Data Layer (data.py)

The foundation of the system. Provides type-safe data classes for users, subjects, scopes, actions, permissions, roles, and role assignments.

  • Handles auto-prefixing (user^, role^, act^).
  • Uses attrs for validation.
  • Defines enums for navigating Casbin policies safely.

This layer abstracts our internal naming conventions #65 which should not be exposed to API consumers. It validates inputs before they reach the Casbin engine, but they could also be useful to future serialization/deserialization operations.

Core APIs

Roles API (roles.py)

  • Base abstraction over the Casbin engine.
  • Manages role definitions, assignments, and queries.
  • Enforces one-role-per-subject-per-scope.
  • Answers questions like:
    • “What permissions does this role grant?”
    • “What roles are active in this scope?”
    • “Who has this role in this scope?”

Permissions API (permissions.py)

  • Works directly with Casbin policies.
  • Converts raw engine data into structured PermissionData.
  • Provides scope-based discovery (get_all_permissions_in_scope).
  • Implements the authorization check (has_permission).

Both APIs consume the data layer and interact with the engine directly. They build the shared vocabulary used throughout the library. These APIs shouldn't be directly consumed for authorization enforcement because of their abstraction. Further up layers with friendlier interfaces should be used instead.

Users API (users.py)

  • Developer-friendly interface for services.
  • Accepts plain strings ("alice", "admin", "lib:123") and handles namespacing internally.
  • Delegates to the roles and permissions APIs.
  • Exposes has_permission so services can check access without going down to lower layers.

This is the recommended entry point for most applications.

Layers & Dependencies

public-api-workflow

Each layer has a single responsibility. Services can stay simple with users.py, or use the lower APIs when more control is needed.

How To Test

A full test suite is included. It covers:

  • Role assignment and removal
  • Permission queries and scope matching
  • End-to-end authorization flows with the Casbin engine - WIP

You can run it by:

make requirements
make test

@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 Sep 30, 2025
@openedx-webhooks
Copy link

openedx-webhooks commented Sep 30, 2025

Thanks for the pull request, @mariajgrimaldi!

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.

@github-project-automation github-project-automation bot moved this to Needs Triage in Contributions Sep 30, 2025
@mariajgrimaldi mariajgrimaldi force-pushed the MJG/public-api branch 2 times, most recently from 8d8b893 to e06e4bd Compare September 30, 2025 14:13
@mariajgrimaldi mariajgrimaldi changed the title Mjg/public api feat: add public API to interact with roles and permissions Sep 30, 2025
Copy link
Contributor

@BryanttV BryanttV left a comment

Choose a reason for hiding this comment

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

I need to do one last review, but here are some initial comments.

# in this case, ALL the policies, but that might not be the case


def get_permissions_for_roles(
Copy link
Contributor

Choose a reason for hiding this comment

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

I was testing this function, but it is not working correctly. The implicit permissions are missing. For example, the library admin role returns these permissions:

{
    "permissions": [
        "delete_library",
        "publish_library",
        "manage_library_team",
        "manage_library_tags",
        "delete_library_content",
        "publish_library_content",
        "delete_library_collection",
        "create_library",
        "create_library_collection"
    ]
}

They are the same ones assigned directly to the role in authz.policy file, but the implicit ones do not appear.

What is strange is that the enforcer does work correctly.

Copy link
Member Author

@mariajgrimaldi mariajgrimaldi Oct 8, 2025

Choose a reason for hiding this comment

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

🤔 So we're on the same page, what should be the correct output?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the correct output should be:

{
    "permissions": [
        # Direct permissions
        "delete_library",
        "publish_library",
        "manage_library_team",
        "manage_library_tags",
        "delete_library_content",
        "publish_library_content",
        "delete_library_collection",
        "create_library",
        "create_library_collection"
        # Implicit permissions
        "view_library",
        "view_library_team",
        "view_library_tags",
        "edit_library_content",
        "view_library_content",
        "edit_library_collection",
    ]
}

Copy link
Contributor

Choose a reason for hiding this comment

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

If it helps, for the MVP, we'll have all the permissions explicitly assigned to a role. We won't rely on permission inheritance. Perhaps this issue is something we can address later.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's good to know, thanks for the info!

I created the issue here so we can follow-up later: #91

return user_role_assignments


def get_all_user_role_assignments_in_scope(scope_external_key: str) -> list[RoleAssignmentData]:
Copy link
Contributor

Choose a reason for hiding this comment

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

I will try, thank you!

Copy link
Contributor

@BryanttV BryanttV left a comment

Choose a reason for hiding this comment

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

These are just minor corrections. Thank you very much for all your hard work!

@bmtcril
Copy link
Contributor

bmtcril commented Oct 7, 2025

I think from my perspective this is good once the other reviewers' comments are addressed. Thanks for all of the work!

@mariajgrimaldi mariajgrimaldi changed the title feat: [FC-0099] add public API to interact with roles and permissions [FC-0099] feat: add public API to interact with roles and permissions Oct 7, 2025
@@ -1,11 +1,60 @@
# ===== ACTION GROUPING (g2) =====
############################################
Copy link
Member Author

@mariajgrimaldi mariajgrimaldi Oct 8, 2025

Choose a reason for hiding this comment

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

⚠️ Although these policy defaults are closer to reality, it's not the end state that should be reviewed as part of milestone 3 in a different PR to not overcomplicate this even further.

Copy link
Contributor

@bmtcril bmtcril left a comment

Choose a reason for hiding this comment

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

The new comments are awesome! 🚀

Copy link
Contributor

@MaferMazu MaferMazu left a comment

Choose a reason for hiding this comment

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

From my perspective, this is good to go. I tested it and it works as expected. When we address the rest of the comment, we can merge this. Thanks, @mariajgrimaldi, for all this work ✨

Copy link
Contributor

@bmtcril bmtcril left a comment

Choose a reason for hiding this comment

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

Awesome work on a huge task!

Copy link
Contributor

@BryanttV BryanttV left a comment

Choose a reason for hiding this comment

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

Thank you! This looks great! 🚀

@mariajgrimaldi mariajgrimaldi merged commit cb10b9a into main Oct 10, 2025
14 checks passed
@mariajgrimaldi mariajgrimaldi deleted the MJG/public-api branch October 10, 2025 09:38
@github-project-automation github-project-automation bot moved this from In Eng Review to Done in Contributions Oct 10, 2025
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). FC Relates to an Axim Funded Contribution project open-source-contribution PR author is not from Axim or 2U

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Develop APIs as the main interface to be used by services (api.py)

7 participants