mirror of
https://github.com/bitwarden/server
synced 2025-12-06 00:03:34 +00:00
[PM-22104] Migrate default collection when org user is removed (#6135)
* migrate default collection to a shared collection when users are removed * remove redundant logic * fix test * fix tests * fix test * clean up * add migrations * run dotnet format * clean up, refactor duplicate logic to sproc, wip integration test * fix sql * add migration for new sproc * integration test wip * integration test wip * integration test wip * integration test wip * fix integration test LINQ expression * fix using wrong Id * wip integration test for DeleteManyAsync * fix LINQ * only set DefaultUserEmail when it is null in sproc * check for null * spelling, separate create and update request models * fix test * fix child class * refactor sproc * clean up * more cleanup * fix tests * fix user email * remove unneccesary test * add DefaultUserCollectionEmail to EF query * fix test * fix EF logic to match sprocs * clean up logic * cleanup
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_MigrateDefaultCollection]
|
||||
@Ids [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
DECLARE @UtcNow DATETIME2(7) = GETUTCDATE();
|
||||
|
||||
UPDATE c
|
||||
SET
|
||||
[DefaultUserCollectionEmail] = CASE WHEN c.[DefaultUserCollectionEmail] IS NULL THEN u.[Email] ELSE c.[DefaultUserCollectionEmail] END,
|
||||
[RevisionDate] = @UtcNow,
|
||||
[Type] = 0
|
||||
FROM
|
||||
[dbo].[Collection] c
|
||||
INNER JOIN [dbo].[CollectionUser] cu ON c.[Id] = cu.[CollectionId]
|
||||
INNER JOIN [dbo].[OrganizationUser] ou ON cu.[OrganizationUserId] = ou.[Id]
|
||||
INNER JOIN [dbo].[User] u ON ou.[UserId] = u.[Id]
|
||||
INNER JOIN @Ids i ON ou.[Id] = i.[Id]
|
||||
WHERE
|
||||
c.[Type] = 1
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,55 @@
|
||||
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_DeleteById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Id
|
||||
|
||||
DECLARE @OrganizationId UNIQUEIDENTIFIER
|
||||
DECLARE @UserId UNIQUEIDENTIFIER
|
||||
|
||||
SELECT
|
||||
@OrganizationId = [OrganizationId],
|
||||
@UserId = [UserId]
|
||||
FROM
|
||||
[dbo].[OrganizationUser]
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
|
||||
-- Migrate DefaultUserCollection to SharedCollection
|
||||
DECLARE @Ids [dbo].[GuidIdArray]
|
||||
INSERT INTO @Ids (Id) VALUES (@Id)
|
||||
EXEC [dbo].[OrganizationUser_MigrateDefaultCollection] @Ids
|
||||
|
||||
IF @OrganizationId IS NOT NULL AND @UserId IS NOT NULL
|
||||
BEGIN
|
||||
EXEC [dbo].[SsoUser_Delete] @UserId, @OrganizationId
|
||||
END
|
||||
|
||||
DELETE
|
||||
FROM
|
||||
[dbo].[CollectionUser]
|
||||
WHERE
|
||||
[OrganizationUserId] = @Id
|
||||
|
||||
DELETE
|
||||
FROM
|
||||
[dbo].[GroupUser]
|
||||
WHERE
|
||||
[OrganizationUserId] = @Id
|
||||
|
||||
DELETE
|
||||
FROM
|
||||
[dbo].[AccessPolicy]
|
||||
WHERE
|
||||
[OrganizationUserId] = @Id
|
||||
|
||||
EXEC [dbo].[OrganizationSponsorship_OrganizationUserDeleted] @Id
|
||||
|
||||
DELETE
|
||||
FROM
|
||||
[dbo].[OrganizationUser]
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
||||
105
util/Migrator/DbScripts/2025-08-04-02_OrgUsers_DeleteByIds.sql
Normal file
105
util/Migrator/DbScripts/2025-08-04-02_OrgUsers_DeleteByIds.sql
Normal file
@@ -0,0 +1,105 @@
|
||||
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_DeleteByIds]
|
||||
@Ids [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Ids
|
||||
|
||||
-- Migrate DefaultCollection to SharedCollection
|
||||
EXEC [dbo].[OrganizationUser_MigrateDefaultCollection] @Ids
|
||||
|
||||
DECLARE @UserAndOrganizationIds [dbo].[TwoGuidIdArray]
|
||||
|
||||
INSERT INTO @UserAndOrganizationIds
|
||||
(Id1, Id2)
|
||||
SELECT
|
||||
UserId,
|
||||
OrganizationId
|
||||
FROM
|
||||
[dbo].[OrganizationUser] OU
|
||||
INNER JOIN
|
||||
@Ids OUIds ON OUIds.Id = OU.Id
|
||||
WHERE
|
||||
UserId IS NOT NULL AND
|
||||
OrganizationId IS NOT NULL
|
||||
|
||||
BEGIN
|
||||
EXEC [dbo].[SsoUser_DeleteMany] @UserAndOrganizationIds
|
||||
END
|
||||
|
||||
DECLARE @BatchSize INT = 100
|
||||
|
||||
-- Delete CollectionUsers
|
||||
WHILE @BatchSize > 0
|
||||
BEGIN
|
||||
BEGIN TRANSACTION CollectionUser_DeleteMany_CUs
|
||||
|
||||
DELETE TOP(@BatchSize) CU
|
||||
FROM
|
||||
[dbo].[CollectionUser] CU
|
||||
INNER JOIN
|
||||
@Ids I ON I.Id = CU.OrganizationUserId
|
||||
|
||||
SET @BatchSize = @@ROWCOUNT
|
||||
|
||||
COMMIT TRANSACTION CollectionUser_DeleteMany_CUs
|
||||
END
|
||||
|
||||
SET @BatchSize = 100;
|
||||
|
||||
-- Delete GroupUsers
|
||||
WHILE @BatchSize > 0
|
||||
BEGIN
|
||||
BEGIN TRANSACTION GroupUser_DeleteMany_GroupUsers
|
||||
|
||||
DELETE TOP(@BatchSize) GU
|
||||
FROM
|
||||
[dbo].[GroupUser] GU
|
||||
INNER JOIN
|
||||
@Ids I ON I.Id = GU.OrganizationUserId
|
||||
|
||||
SET @BatchSize = @@ROWCOUNT
|
||||
|
||||
COMMIT TRANSACTION GroupUser_DeleteMany_GroupUsers
|
||||
END
|
||||
|
||||
SET @BatchSize = 100;
|
||||
|
||||
-- Delete User Access Policies
|
||||
WHILE @BatchSize > 0
|
||||
BEGIN
|
||||
BEGIN TRANSACTION AccessPolicy_DeleteMany_Users
|
||||
|
||||
DELETE TOP(@BatchSize) AP
|
||||
FROM
|
||||
[dbo].[AccessPolicy] AP
|
||||
INNER JOIN
|
||||
@Ids I ON I.Id = AP.OrganizationUserId
|
||||
|
||||
SET @BatchSize = @@ROWCOUNT
|
||||
|
||||
COMMIT TRANSACTION AccessPolicy_DeleteMany_Users
|
||||
END
|
||||
|
||||
EXEC [dbo].[OrganizationSponsorship_OrganizationUsersDeleted] @Ids
|
||||
|
||||
SET @BatchSize = 100;
|
||||
|
||||
-- Delete OrganizationUsers
|
||||
WHILE @BatchSize > 0
|
||||
BEGIN
|
||||
BEGIN TRANSACTION OrganizationUser_DeleteMany_OUs
|
||||
|
||||
DELETE TOP(@BatchSize) OU
|
||||
FROM
|
||||
[dbo].[OrganizationUser] OU
|
||||
INNER JOIN
|
||||
@Ids I ON I.Id = OU.Id
|
||||
|
||||
SET @BatchSize = @@ROWCOUNT
|
||||
|
||||
COMMIT TRANSACTION OrganizationUser_DeleteMany_OUs
|
||||
END
|
||||
END
|
||||
GO
|
||||
Reference in New Issue
Block a user