1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-08 00:31:27 +01:00

removed stored procedure, refactor User_DeleteById to accept multiple Ids

This commit is contained in:
Brandon 2024-11-12 12:59:32 -05:00
parent 13ab036f91
commit 16bf910255
No known key found for this signature in database
GPG Key ID: A0E0EF0B207BA40D
3 changed files with 18 additions and 162 deletions

View File

@ -167,7 +167,7 @@ public class UserRepository : Repository<User, Guid>, IUserRepository
{
await connection.ExecuteAsync(
$"[{Schema}].[{Table}_DeleteById]",
new { Id = user.Id },
new { Ids = new List<Guid> { user.Id }.ToGuidIdArrayTVP() },
commandType: CommandType.StoredProcedure,
commandTimeout: 180);
}
@ -178,7 +178,7 @@ public class UserRepository : Repository<User, Guid>, 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);

View File

@ -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

View File

@ -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