Add cross-organization support to user query functions#221
Add cross-organization support to user query functions#221bclermont wants to merge 2 commits intocasdoor:masterfrom
Conversation
Enable user queries across multiple organizations while maintaining
backward compatibility with existing single-organization usage.
Changes:
- GetUser: Auto-detect org-prefixed names ("org/user" format)
If name contains "/", use as-is; otherwise prepend configured org
- GetUsers: Add optional organization variadic parameter
- GetUserByEmail: Add optional organization variadic parameter
- GetUserByPhone: Add optional organization variadic parameter
- GetSortedUsers: Add optional organization variadic parameter
Backward compatibility:
- All existing code continues to work without modification
- Optional parameters default to client's configured organization
- New code can specify organization explicitly when needed
Fixes issue where multi-org Casdoor instances couldn't query users
from organizations other than the SDK client's configured org.
|
Bruno Clermont seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
There was a problem hiding this comment.
Pull request overview
This PR adds cross-organization support to user query functions in the Casdoor SDK, enabling multi-tenant deployments while maintaining backward compatibility with existing single-organization usage.
Changes:
- Modified
GetUser()to auto-detect organization-prefixed names inorg/userformat - Added optional
organizationparameter toGetUsers(),GetSortedUsers(),GetUserByEmail(), andGetUserByPhone()functions - All changes maintain backward compatibility through optional parameters and format detection
Comments suppressed due to low confidence (1)
casdoorsdk/user.go:1
- The global wrapper functions in user_global.go don't expose the new optional organization parameter. The wrapper for GetUsers(), GetSortedUsers(), GetUserByEmail(), and GetUserByPhone() should be updated to accept and pass through the variadic organization parameter to maintain API consistency.
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Support optional organization parameter | ||
| // If not provided, use the client's configured organization for backward compatibility | ||
| owner := c.OrganizationName | ||
| if len(organization) > 0 && organization[0] != "" { | ||
| owner = organization[0] | ||
| } |
There was a problem hiding this comment.
The pattern for handling the optional organization parameter is duplicated across four functions (GetUsers, GetSortedUsers, GetUserByEmail, GetUserByPhone). Consider extracting this logic into a helper method like resolveOrganization(organization ...string) string to reduce duplication and improve maintainability.
| // If name already contains "/", it's a fully qualified name | ||
| // Otherwise, prepend the client's organization for backward compatibility | ||
| id := name | ||
| if !strings.Contains(name, "/") { |
There was a problem hiding this comment.
The slash detection for organization-qualified names could incorrectly treat usernames containing '/' as already qualified. Consider using strings.Count(name, \"/\") == 0 or better yet, validate that the format matches 'org/user' with exactly one slash. This prevents ambiguity if usernames themselves can contain slashes.
Summary
Enable user queries across multiple organizations in multi-tenant Casdoor deployments while maintaining backward compatibility with existing single-organization usage.
Problem
The SDK client is initialized with a single organization and automatically prepends it to all user queries. This prevents querying users from other organizations in multi-tenant Casdoor instances.
Before:
GetUser("robotinfra/mcp-test-user")"built-in/robotinfra/mcp-test-user"❌Solution
GetUser
Auto-detects org-prefixed names in
"org/user"format:/, uses as-is (explicit organization)Other Query Functions
Added optional variadic
organization ...stringparameter:GetUsers(organization ...string)GetUserByEmail(email string, organization ...string)GetUserByPhone(phone string, organization ...string)GetSortedUsers(sorter string, limit int, organization ...string)Backward Compatibility
✅ All existing code continues to work without modification
✅ Optional parameters default to client's configured organization
✅ New code can specify organization explicitly when needed
Testing
Tested with mcp-casdoor integration:
Use Case
This enables MCP servers and other SDK clients to manage users across multiple organizations without requiring separate SDK client instances per organization.