[PM-39554] Create default collection for demoted admin#7918
Conversation
🤖 Bitwarden Claude Code ReviewOverall Assessment: REQUEST CHANGES Reviewed the change that creates a default ("My Items") collection when an Admin/Owner is demoted to a lower role in Code Review Details
|
| var isDemotedFromPrivilegedRole = existingUserType is OrganizationUserType.Admin or OrganizationUserType.Owner | ||
| && organizationUser.Type is not (OrganizationUserType.Admin or OrganizationUserType.Owner); | ||
| if (isDemotedFromPrivilegedRole && !string.IsNullOrWhiteSpace(defaultUserCollectionName)) | ||
| { | ||
| await _collectionRepository.CreateDefaultCollectionsAsync( | ||
| organizationUser.OrganizationId, | ||
| [organizationUser.Id], | ||
| defaultUserCollectionName); | ||
| } |
There was a problem hiding this comment.
organization.UseMyItems or the Organization Data Ownership policy, unlike every other call site.
Details and fix
The three other places that call CreateDefaultCollectionsAsync all gate creation on both organization.UseMyItems and the OrganizationDataOwnershipPolicyRequirement, not just a non-empty name:
ConfirmOrganizationUserCommand.CreateManyDefaultCollectionsAsync(checksorganization.UseMyItems+ShouldCreateDefaultCollection)RestoreOrganizationUserCommand(checksorganization.UseMyItems+ policyState == Enabled)BulkAutomaticallyConfirmOrganizationUsersCommand.CreateDefaultCollectionsForManyAsync(checks!organization.UseMyItems+ShouldCreateDefaultCollection)
Here, only !string.IsNullOrWhiteSpace(defaultUserCollectionName) is checked. Because the name comes from the client request, a demotion in an organization that has "My Items" disabled (or where the data-ownership policy does not apply to the user) will still create a DefaultUserCollection, contradicting the organization's configuration. Server-side enforcement should not depend on the client omitting the name.
The organization entity is already loaded at line 86, so at minimum the UseMyItems guard is a cheap addition:
var isDemotedFromPrivilegedRole = existingUserType is OrganizationUserType.Admin or OrganizationUserType.Owner
&& organizationUser.Type is not (OrganizationUserType.Admin or OrganizationUserType.Owner);
if (isDemotedFromPrivilegedRole
&& organization.UseMyItems
&& !string.IsNullOrWhiteSpace(defaultUserCollectionName))
{
await _collectionRepository.CreateDefaultCollectionsAsync(...);
}If the data-ownership policy should also gate this (as it does elsewhere), inject IPolicyRequirementQuery and mirror the sibling checks. If the divergence is intentional for the demotion flow, a brief comment explaining why would help future readers.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7918 +/- ##
==========================================
+ Coverage 61.34% 61.36% +0.02%
==========================================
Files 2236 2236
Lines 98517 98563 +46
Branches 8907 8913 +6
==========================================
+ Hits 60437 60486 +49
+ Misses 35943 35941 -2
+ Partials 2137 2136 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
🎟️ Tracking
https://bitwarden.atlassian.net/browse/PM-39554
📔 Objective
This PR fixes an issue when an admin's role is demoted to a user, no "My Items" collection is created for them.
📸 Screenshots