Skip to content

feat: add role management helper methods#114

Merged
hsluoyz merged 1 commit intocasdoor:masterfrom
Beesh1:feat/add-role-management-methods
Nov 4, 2025
Merged

feat: add role management helper methods#114
hsluoyz merged 1 commit intocasdoor:masterfrom
Beesh1:feat/add-role-management-methods

Conversation

@Beesh1
Copy link
Contributor

@Beesh1 Beesh1 commented Oct 30, 2025

Summary

Adds helper methods for managing user role assignments through the Casdoor role API, following the workflow described in issue #4360.

Changes

New Methods:

  • get_roles() - Retrieve all roles in the organization
  • get_role(role_name) - Retrieve a specific role
  • update_role(role) - Update role configuration
  • assign_role_to_user(username, role_name) - Add user to role
  • remove_role_from_user(username, role_name) - Remove user from role
  • get_user_roles(username) - List all roles for a user

All methods available in both sync (CasdoorSDK) and async (AsyncCasdoorSDK) versions.

Implementation

These methods handle role assignment through the proper API workflow:

  1. Fetch the target role via get-role endpoint
  2. Modify the role's users array
  3. Update the role via update-role endpoint

This approach correctly manages the extended roles field as outlined in the maintainer response to #4360.

Usage Example

from casdoor import CasdoorSDK

sdk = CasdoorSDK(
    endpoint="https://door.example.com",
    client_id="your_client_id",
    client_secret="your_client_secret",
    certificate="",
    org_name="your_org",
    application_name="your_app"
)

# Assign role
sdk.assign_role_to_user('john', 'admin')

# Verify assignment
roles = sdk.get_user_roles('john')

# Remove role
sdk.remove_role_from_user('john', 'admin')

Testing

Validated against production Casdoor instance with real role assignments and removals.

Related

Implements the solution discussed in #4360.

Add convenient methods for managing user role assignments:
- assign_role_to_user(username, role_name): Assign a role to a user
- remove_role_from_user(username, role_name): Remove a role from a user
- get_user_roles(username): Get all roles assigned to a user
- get_roles(): Get all roles in the organization
- get_role(role_name): Get a specific role
- update_role(role): Update a role

Both sync and async versions included.

These methods simplify the common task of role assignment by handling
the workflow of fetching the role, modifying the users list, and
updating the role in a single call.
@Beesh1
Copy link
Contributor Author

Beesh1 commented Oct 30, 2025

@hsluoyz this provides an easy solution using the SDK

@hsluoyz hsluoyz merged commit f3873e9 into casdoor:master Nov 4, 2025
3 checks passed
github-actions bot pushed a commit that referenced this pull request Nov 4, 2025
# [1.40.0](v1.39.0...v1.40.0) (2025-11-04)

### Features

* add role management helper methods ([#114](#114)) ([f3873e9](f3873e9))
@github-actions
Copy link

github-actions bot commented Nov 4, 2025

🎉 This PR is included in version 1.40.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants