mirror of
https://github.com/bitwarden/server
synced 2025-12-24 20:23:21 +00:00
[PM-26050] Migrate all DefaultUserCollection when claimed user is deleted (#6366)
* feat: migrate DefaultUserCollection to SharedCollection during user deletion - Implemented migration of DefaultUserCollection to SharedCollection in EF UserRepository before deleting organization users. - Updated stored procedures User_DeleteById and User_DeleteByIds to include migration logic. - Added new migration script for updating stored procedures. * Add unit test for user deletion and DefaultUserCollection migration - Implemented a new test to verify the migration of DefaultUserCollection to SharedCollection during user deletion in UserRepository. - The test ensures that the user is deleted and the associated collection is updated correctly. * Refactor user deletion process in UserRepository - Moved migrating DefaultUserCollection to SharedCollection to happen before the deletion of user-related entities. - Updated the deletion logic to use ExecuteDeleteAsync for improved performance and clarity. - Ensured that all related entities are removed in a single transaction to maintain data integrity. * Add unit test for DeleteManyAsync in UserRepository - Implemented a new test to verify the deletion of multiple users and the migration of their DefaultUserCollections to SharedCollections. - Ensured that both users are deleted and their associated collections are updated correctly in a single transaction. * Refactor UserRepositoryTests to use test user creation methods and streamline collection creation * Ensure changes are saved after deleting users in bulk * Refactor UserRepository to simplify migration queries and remove unnecessary loops for better performance * Refactor UserRepository to encapsulate DefaultUserCollection migration logic in a separate method * Refactor UserRepository to optimize deletion queries by using joins instead of subqueries for improved performance * Refactor UserRepositoryTest DeleteManyAsync_Works to ensure GroupUser and CollectionUser deletion --------- Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com>
This commit is contained in:
@@ -52,6 +52,16 @@ BEGIN
|
||||
WHERE
|
||||
[UserId] = @Id
|
||||
|
||||
-- Migrate DefaultUserCollection to SharedCollection before deleting CollectionUser records
|
||||
DECLARE @OrgUserIds [dbo].[GuidIdArray]
|
||||
INSERT INTO @OrgUserIds (Id)
|
||||
SELECT [Id] FROM [dbo].[OrganizationUser] WHERE [UserId] = @Id
|
||||
|
||||
IF EXISTS (SELECT 1 FROM @OrgUserIds)
|
||||
BEGIN
|
||||
EXEC [dbo].[OrganizationUser_MigrateDefaultCollection] @OrgUserIds
|
||||
END
|
||||
|
||||
-- Delete collection users
|
||||
DELETE
|
||||
CU
|
||||
|
||||
@@ -66,6 +66,16 @@ BEGIN
|
||||
WHERE
|
||||
[UserId] IN (SELECT * FROM @ParsedIds)
|
||||
|
||||
-- Migrate DefaultUserCollection to SharedCollection before deleting CollectionUser records
|
||||
DECLARE @OrgUserIds [dbo].[GuidIdArray]
|
||||
INSERT INTO @OrgUserIds (Id)
|
||||
SELECT [Id] FROM [dbo].[OrganizationUser] WHERE [UserId] IN (SELECT * FROM @ParsedIds)
|
||||
|
||||
IF EXISTS (SELECT 1 FROM @OrgUserIds)
|
||||
BEGIN
|
||||
EXEC [dbo].[OrganizationUser_MigrateDefaultCollection] @OrgUserIds
|
||||
END
|
||||
|
||||
-- Delete collection users
|
||||
DELETE
|
||||
CU
|
||||
|
||||
Reference in New Issue
Block a user