Skip to content

Conversation

@Beesh1
Copy link
Contributor

@Beesh1 Beesh1 commented Oct 30, 2025

Add columns parameter for selective field updates in user operations

Overview

This PR adds support for the columns parameter in user update operations, enabling selective field updates. This addresses issue #112.

Changes

  • Add columns parameter to update_user() method in both sync and async implementations
  • Add columns parameter to update_user_by_id() method in sync implementation
  • Add columns parameter to internal modify_user() and modify_user_by_id() methods
  • Add comprehensive docstrings documenting parameter usage
  • Maintain backward compatibility (defaults to updating all fields when columns=None)

Implementation

When the columns parameter is provided, it is formatted as a comma-separated string and added to the query parameters:

POST /api/update-user?id=org/user&columns=roles,email

This leverages the existing Casdoor API support for selective field updates.

Usage Example

Sync version:

from casdoor import CasdoorSDK, User

sdk = CasdoorSDK(...)
user = sdk.get_user('john')
user.roles.append(role_object)
sdk.update_user(user, columns=['roles'])

Async version:

from casdoor import AsyncCasdoorSDK, User

sdk = AsyncCasdoorSDK(...)
user_data = await sdk.get_user('john')
user = User.from_dict(user_data)
user.email = 'newemail@example.com'
await sdk.update_user(user, columns=['email'])

Testing

Tested against live Casdoor instance:

  • Sync SDK with single and multiple column updates
  • Async SDK with single and multiple column updates
  • Backward compatibility without columns parameter

Related Issue

Fixes #112

…d updates

Add support for the 'columns' parameter in user update operations to enable
selective field updates, which is particularly useful for updating specific
fields like roles without affecting other user attributes.

Changes:
- Add 'columns' parameter to update_user() method (sync and async)
- Add 'columns' parameter to update_user_by_id() method (sync)
- Add 'columns' parameter to modify_user() and modify_user_by_id() methods
- Update method signatures to include Optional[List[str]] for columns
- Add comprehensive docstrings explaining the columns parameter usage

The columns parameter accepts a list of field names to update:
  sdk.update_user(user, columns=['roles', 'email'])

This sends 'columns=roles,email' as a query parameter to the Casdoor API,
allowing for more granular control over user updates and reducing the risk
of unintended field modifications.

Implements selective field update pattern supported by Casdoor API.
Maintains backward compatibility - when columns=None, all fields are updated.
@Beesh1
Copy link
Contributor Author

Beesh1 commented Oct 30, 2025

Thank you for the clarification @hsluoyz. You're absolutely correct - I've now implemented the proper solution:

Correct approach for role assignment:

  1. Fetch the role object via /api/get-role
  2. Add the user ID to the role's users array
  3. Update the role via /api/update-role

This works perfectly and roles are now properly assigned. The columns parameter enhancement in this PR is still useful for other fields, but you're right that roles/permissions must be managed through the role/permission APIs, not through user updates.

Should I:

  1. Keep this PR for the columns parameter (useful for other fields like email, phone, etc.)
  2. Or close it since the primary use case (roles) doesn't apply?

Let me know your preference.

@hsluoyz hsluoyz closed this Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add support for selective field updates via columns parameter in update_user methods

2 participants