diff --git a/src/Infrastructure.Dapper/Repositories/UserRepository.cs b/src/Infrastructure.Dapper/Repositories/UserRepository.cs index fad1cdbc85..f3d440e0db 100644 --- a/src/Infrastructure.Dapper/Repositories/UserRepository.cs +++ b/src/Infrastructure.Dapper/Repositories/UserRepository.cs @@ -167,7 +167,7 @@ public class UserRepository : Repository, IUserRepository { await connection.ExecuteAsync( $"[{Schema}].[{Table}_DeleteById]", - new { Id = user.Id }, + new { Ids = new List { user.Id }.ToGuidIdArrayTVP() }, commandType: CommandType.StoredProcedure, commandTimeout: 180); } @@ -178,7 +178,7 @@ public class UserRepository : Repository, IUserRepository using (var connection = new SqlConnection(ConnectionString)) { await connection.ExecuteAsync( - $"[{Schema}].[{Table}_DeleteByIds]", + $"[{Schema}].[{Table}_DeleteById]", new { Ids = list.ToGuidIdArrayTVP() }, commandType: CommandType.StoredProcedure, commandTimeout: 180); diff --git a/src/Sql/dbo/Stored Procedures/User_DeleteById.sql b/src/Sql/dbo/Stored Procedures/User_DeleteById.sql index 0608982e37..fa35624098 100644 --- a/src/Sql/dbo/Stored Procedures/User_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/User_DeleteById.sql @@ -1,5 +1,5 @@ -CREATE PROCEDURE [dbo].[User_DeleteById] - @Id UNIQUEIDENTIFIER +CREATE PROCEDURE [dbo].[User_DeleteByIds] + @Ids [dbo].[GuidIdArray] WITH RECOMPILE AS BEGIN @@ -15,7 +15,7 @@ BEGIN FROM [dbo].[Cipher] WHERE - [UserId] = @Id + [UserId] IN (@Ids) SET @BatchSize = @@ROWCOUNT @@ -29,28 +29,28 @@ BEGIN FROM [dbo].[WebAuthnCredential] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete folders DELETE FROM [dbo].[Folder] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete AuthRequest, must be before Device DELETE FROM [dbo].[AuthRequest] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete devices DELETE FROM [dbo].[Device] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete collection users DELETE @@ -60,7 +60,7 @@ BEGIN INNER JOIN [dbo].[OrganizationUser] OU ON OU.[Id] = CU.[OrganizationUserId] WHERE - OU.[UserId] = @Id + OU.[UserId] IN (@Ids) -- Delete group users DELETE @@ -70,7 +70,7 @@ BEGIN INNER JOIN [dbo].[OrganizationUser] OU ON OU.[Id] = GU.[OrganizationUserId] WHERE - OU.[UserId] = @Id + OU.[UserId] IN (@Ids) -- Delete AccessPolicy DELETE @@ -80,28 +80,28 @@ BEGIN INNER JOIN [dbo].[OrganizationUser] OU ON OU.[Id] = AP.[OrganizationUserId] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete organization users DELETE FROM [dbo].[OrganizationUser] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete provider users DELETE FROM [dbo].[ProviderUser] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete SSO Users DELETE FROM [dbo].[SsoUser] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete Emergency Accesses DELETE @@ -117,21 +117,21 @@ BEGIN FROM [dbo].[Send] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete Notification Status DELETE FROM [dbo].[NotificationStatus] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Delete Notification DELETE FROM [dbo].[Notification] WHERE - [UserId] = @Id + [UserId] IN (@Ids) -- Finally, delete the user DELETE diff --git a/src/Sql/dbo/Stored Procedures/User_DeleteByIds.sql b/src/Sql/dbo/Stored Procedures/User_DeleteByIds.sql deleted file mode 100644 index 1f4122c902..0000000000 --- a/src/Sql/dbo/Stored Procedures/User_DeleteByIds.sql +++ /dev/null @@ -1,144 +0,0 @@ -CREATE PROCEDURE [dbo].[User_DeleteByIds] - @Ids [dbo].[GuidIdArray] -WITH RECOMPILE -AS -BEGIN - SET NOCOUNT ON - DECLARE @BatchSize INT = 100 - - -- Delete ciphers - WHILE @BatchSize > 0 - BEGIN - BEGIN TRANSACTION User_DeleteById_Ciphers - - DELETE TOP(@BatchSize) - FROM - [dbo].[Cipher] - WHERE - [UserId] IN (@Ids) - - SET @BatchSize = @@ROWCOUNT - - COMMIT TRANSACTION User_DeleteById_Ciphers - END - - BEGIN TRANSACTION User_DeleteById - - -- Delete WebAuthnCredentials - DELETE - FROM - [dbo].[WebAuthnCredential] - WHERE - [UserId] IN (@Ids) - - -- Delete folders - DELETE - FROM - [dbo].[Folder] - WHERE - [UserId] IN (@Ids) - - -- Delete AuthRequest, must be before Device - DELETE - FROM - [dbo].[AuthRequest] - WHERE - [UserId] IN (@Ids) - - -- Delete devices - DELETE - FROM - [dbo].[Device] - WHERE - [UserId] IN (@Ids) - - -- Delete collection users - DELETE - CU - FROM - [dbo].[CollectionUser] CU - INNER JOIN - [dbo].[OrganizationUser] OU ON OU.[Id] = CU.[OrganizationUserId] - WHERE - OU.[UserId] IN (@Ids) - - -- Delete group users - DELETE - GU - FROM - [dbo].[GroupUser] GU - INNER JOIN - [dbo].[OrganizationUser] OU ON OU.[Id] = GU.[OrganizationUserId] - WHERE - OU.[UserId] IN (@Ids) - - -- Delete AccessPolicy - DELETE - AP - FROM - [dbo].[AccessPolicy] AP - INNER JOIN - [dbo].[OrganizationUser] OU ON OU.[Id] = AP.[OrganizationUserId] - WHERE - [UserId] IN (@Ids) - - -- Delete organization users - DELETE - FROM - [dbo].[OrganizationUser] - WHERE - [UserId] IN (@Ids) - - -- Delete provider users - DELETE - FROM - [dbo].[ProviderUser] - WHERE - [UserId] IN (@Ids) - - -- Delete SSO Users - DELETE - FROM - [dbo].[SsoUser] - WHERE - [UserId] IN (@Ids) - - -- Delete Emergency Accesses - DELETE - FROM - [dbo].[EmergencyAccess] - WHERE - [GrantorId] = @Id - OR - [GranteeId] = @Id - - -- Delete Sends - DELETE - FROM - [dbo].[Send] - WHERE - [UserId] IN (@Ids) - - -- Delete Notification Status - DELETE - FROM - [dbo].[NotificationStatus] - WHERE - [UserId] IN (@Ids) - - -- Delete Notification - DELETE - FROM - [dbo].[Notification] - WHERE - [UserId] IN (@Ids) - - -- Finally, delete the user - DELETE - FROM - [dbo].[User] - WHERE - [Id] = @Id - - COMMIT TRANSACTION User_DeleteById -END