Skip to content

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

@Beesh1

Description

@Beesh1

Issue: Support for selective field updates in update_user methods

Description

The current update_user() methods in both sync (user.py) and async (async_main.py) implementations do not support the columns parameter, which prevents selective field updates. This is particularly problematic when trying to update specific fields like roles without affecting other user attributes.

Current Behavior

# Current implementation only allows updating all fields
sdk.update_user(user)

This sends all user fields to the API, which can:

  • Overwrite unintended fields
  • Cause synchronization issues when multiple processes update different fields
  • Not leverage Casdoor's API support for selective updates via the columns parameter

Expected Behavior

# Should support selective field updates
sdk.update_user(user, columns=['roles'])
sdk.update_user(user, columns=['roles', 'email'])

This would send ?columns=roles or ?columns=roles,email as query parameters to the Casdoor API endpoint /api/update-user, allowing granular control over which fields are updated.

Use Case

When implementing role auto-assignment for new users:

  1. Fetch user from Casdoor
  2. Add role to user.roles list
  3. Update only the roles field without touching other fields like email, phone, etc.

Currently, this workflow fails or causes unintended side effects because all fields are sent in the update request.

Proposed Solution

Add an optional columns parameter to:

  • update_user(user: User, columns: Optional[List[str]] = None)
  • update_user_by_id(id: str, user: User, columns: Optional[List[str]] = None)
  • modify_user(method: str, user: User, columns: Optional[List[str]] = None)
  • modify_user_by_id(method: str, id: str, user: User, columns: Optional[List[str]] = None)

Implementation

I have already implemented this feature in a fork and tested it. The implementation:

  • ✅ Maintains backward compatibility (defaults to None = update all fields)
  • ✅ Works with both sync and async versions
  • ✅ Includes comprehensive docstrings
  • ✅ Properly formats the columns as comma-separated query parameter

Pull Request

I will submit a PR with the implementation shortly.

Environment

  • Python SDK version: Latest master
  • Python version: 3.12+
  • Casdoor server version: Compatible with columns parameter

Additional Context

The Casdoor API already supports the columns parameter for selective updates (documented in the Go SDK and API), but the Python SDK doesn't expose this functionality to users.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions