-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-19232] Implement externalId handling in PatchUserCommand with validation #6998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a62d5ef
255daa1
5905907
33ef792
ca4d73d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using Bit.Core.Repositories; | ||
| using Bit.Scim.Models; | ||
| using Bit.Scim.Users.Interfaces; | ||
| using Bit.Scim.Utilities; | ||
|
|
||
| namespace Bit.Scim.Users; | ||
|
|
||
|
|
@@ -38,7 +39,7 @@ public async Task PatchUserAsync(Guid organizationId, Guid id, ScimPatchModel mo | |
| foreach (var operation in model.Operations) | ||
| { | ||
| // Replace operations | ||
| if (operation.Op?.ToLowerInvariant() == "replace") | ||
| if (operation.Op?.ToLowerInvariant() == PatchOps.Replace) | ||
| { | ||
| // Active from path | ||
| if (operation.Path?.ToLowerInvariant() == "active") | ||
|
|
@@ -60,6 +61,21 @@ public async Task PatchUserAsync(Guid organizationId, Guid id, ScimPatchModel mo | |
| operationHandled = handled; | ||
| } | ||
| } | ||
| // ExternalId from path | ||
| else if (operation.Path?.ToLowerInvariant() == PatchPaths.ExternalId) | ||
| { | ||
| var newExternalId = operation.Value.GetString(); | ||
| await HandleExternalIdOperationAsync(orgUser, newExternalId); | ||
| operationHandled = true; | ||
| } | ||
| // ExternalId from value object | ||
| else if (string.IsNullOrWhiteSpace(operation.Path) && | ||
| operation.Value.TryGetProperty("externalId", out var externalIdProperty)) | ||
| { | ||
| var newExternalId = externalIdProperty.GetString(); | ||
| await HandleExternalIdOperationAsync(orgUser, newExternalId); | ||
| operationHandled = true; | ||
| } | ||
|
Comment on lines
+64
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Example request that triggers this: {
"op": "replace",
"value": { "active": false, "externalId": "new-id" }
}This is valid per SCIM RFC 7644 Section 3.5.2.2 (replace with no path targets the resource itself with all sub-attributes in the value). SCIM providers like EntraID could reasonably send combined property updates in a single operation. Suggested fix: For the value-object branches (no path specified), check for each property independently rather than using // Value object with no path - check for each supported property
if (string.IsNullOrWhiteSpace(operation.Path))
{
if (operation.Value.TryGetProperty("active", out var activeProperty))
{
var handled = await HandleActiveOperationAsync(orgUser, activeProperty.GetBoolean());
if (!operationHandled)
{
operationHandled = handled;
}
}
if (operation.Value.TryGetProperty("externalId", out var externalIdProperty))
{
var newExternalId = externalIdProperty.GetString();
await HandleExternalIdOperationAsync(orgUser, newExternalId);
operationHandled = true;
}
}This preserves the existing behavior for path-based operations while correctly handling combined value objects. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -84,4 +100,28 @@ private async Task<bool> HandleActiveOperationAsync(Core.Entities.OrganizationUs | |
| } | ||
| return false; | ||
| } | ||
|
|
||
| private async Task HandleExternalIdOperationAsync(Core.Entities.OrganizationUser orgUser, string? newExternalId) | ||
| { | ||
| // Validate max length (300 chars per OrganizationUser.cs line 59) | ||
| if (!string.IsNullOrWhiteSpace(newExternalId) && newExternalId.Length > 300) | ||
| { | ||
| throw new BadRequestException("ExternalId cannot exceed 300 characters."); | ||
| } | ||
|
|
||
| // Check for duplicate externalId (same validation as PostUserCommand.cs) | ||
| if (!string.IsNullOrWhiteSpace(newExternalId)) | ||
| { | ||
| var existingUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(orgUser.OrganizationId); | ||
| if (existingUsers.Any(u => u.Id != orgUser.Id && | ||
| !string.IsNullOrWhiteSpace(u.ExternalId) && | ||
| u.ExternalId.Equals(newExternalId, StringComparison.OrdinalIgnoreCase))) | ||
| { | ||
| throw new ConflictException("ExternalId already exists for another user."); | ||
| } | ||
| } | ||
|
|
||
| orgUser.ExternalId = newExternalId; | ||
| await _organizationUserRepository.ReplaceAsync(orgUser); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocker: I know you're just following the existing pattern, so the code as is good, but consider abstracting this logic into its own method and using the early return pattern to reduce nested conditionals when possible.
Methods are normally used for DRY, but I think the ability to keep the scope small and let the methodβs name provide a quick summary of what this group of code is doing makes it easier to understand at a glance. Also, the early return pattern makes it easier to follow.