diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql b/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql index da66b4cb0..91ed28e39 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql @@ -5,21 +5,69 @@ AS BEGIN SET NOCOUNT ON - ;WITH [IdsToDeleteCTE] AS ( - SELECT - [Id] - FROM - [dbo].[UserCipherDetails](@UserId) - WHERE - [Edit] = 1 - AND [Id] IN (SELECT * FROM @Ids) + CREATE TABLE #Temp + ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [UserId] UNIQUEIDENTIFIER NULL, + [OrganizationId] UNIQUEIDENTIFIER NULL, + [Attachments] BIT NOT NULL ) + + INSERT INTO #Temp + SELECT + [Id], + [UserId], + [OrganizationId], + CASE WHEN [Attachments] IS NULL THEN 0 ELSE 1 END + FROM + [dbo].[UserCipherDetails](@UserId) + WHERE + [Edit] = 1 + AND [Id] IN (SELECT * FROM @Ids) + + -- Delete ciphers DELETE FROM [dbo].[Cipher] WHERE - [Id] IN (SELECT * FROM [IdsToDeleteCTE]) + [Id] IN (SELECT [Id] FROM #Temp) + -- Cleanup orgs + DECLARE @OrgId UNIQUEIDENTIFIER + DECLARE [OrgCursor] CURSOR FORWARD_ONLY FOR + SELECT + [OrganizationId] + FROM + #Temp + WHERE + [OrganizationId] IS NOT NULL + GROUP BY + [OrganizationId] + OPEN [OrgCursor] + FETCH NEXT FROM [OrgCursor] INTO @OrgId + WHILE @@FETCH_STATUS = 0 BEGIN + EXEC [dbo].[Organization_UpdateStorage] @OrgId + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrgId + FETCH NEXT FROM [OrgCursor] INTO @OrgId + END + CLOSE [OrgCursor] + DEALLOCATE [OrgCursor] + + -- Cleanup user + DECLARE @UserCiphersWithStorageCount INT + SELECT + @UserCiphersWithStorageCount = COUNT(1) + FROM + #Temp + WHERE + [UserId] IS NOT NULL + AND [Attachments] = 1 + + IF @UserCiphersWithStorageCount > 0 + BEGIN + EXEC [dbo].[User_UpdateStorage] @UserId + END EXEC [dbo].[User_BumpAccountRevisionDate] @UserId - -- TODO: What if some that were deleted were organization ciphers? Then bump by org ids. + + DROP TABLE #Temp END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql index ca6f39a34..a13aae6ae 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql @@ -6,10 +6,12 @@ BEGIN DECLARE @UserId UNIQUEIDENTIFIER DECLARE @OrganizationId UNIQUEIDENTIFIER + DECLARE @Attachments BIT SELECT TOP 1 @UserId = [UserId], - @OrganizationId = [OrganizationId] + @OrganizationId = [OrganizationId], + @Attachments = CASE WHEN [Attachments] IS NOT NULL THEN 1 ELSE 0 END FROM [dbo].[Cipher] WHERE @@ -23,10 +25,18 @@ BEGIN IF @OrganizationId IS NOT NULL BEGIN + IF @Attachments = 1 + BEGIN + EXEC [dbo].[Organization_UpdateStorage] @OrganizationId + END EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId END ELSE IF @UserId IS NOT NULL BEGIN + IF @Attachments = 1 + BEGIN + EXEC [dbo].[User_UpdateStorage] @UserId + END EXEC [dbo].[User_BumpAccountRevisionDate] @UserId END END \ No newline at end of file