mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 21:18:13 -05:00
[PM-3505][PM-4587] Update Delete Organization and User SPROCs and EF methods (#3604)
* update Organization_DeleteById SPROC * Add migration for user delete * Updated delete methods for EF support * added WITH RECOMPILE * updating sprocs in sql project * Add recompile
This commit is contained in:
parent
c60f260c0f
commit
9b50cf89b7
@ -157,6 +157,8 @@ public class OrganizationRepository : Repository<Core.AdminConsole.Entities.Orga
|
|||||||
await deleteCiphersTransaction.CommitAsync();
|
await deleteCiphersTransaction.CommitAsync();
|
||||||
|
|
||||||
var organizationDeleteTransaction = await dbContext.Database.BeginTransactionAsync();
|
var organizationDeleteTransaction = await dbContext.Database.BeginTransactionAsync();
|
||||||
|
await dbContext.AuthRequests.Where(ar => ar.OrganizationId == organization.Id)
|
||||||
|
.ExecuteDeleteAsync();
|
||||||
await dbContext.SsoUsers.Where(su => su.OrganizationId == organization.Id)
|
await dbContext.SsoUsers.Where(su => su.OrganizationId == organization.Id)
|
||||||
.ExecuteDeleteAsync();
|
.ExecuteDeleteAsync();
|
||||||
await dbContext.SsoConfigs.Where(sc => sc.OrganizationId == organization.Id)
|
await dbContext.SsoConfigs.Where(sc => sc.OrganizationId == organization.Id)
|
||||||
|
@ -210,6 +210,7 @@ public class UserRepository : Repository<Core.Entities.User, User, Guid>, IUserR
|
|||||||
|
|
||||||
var transaction = await dbContext.Database.BeginTransactionAsync();
|
var transaction = await dbContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
|
dbContext.WebAuthnCredentials.RemoveRange(dbContext.WebAuthnCredentials.Where(w => w.UserId == user.Id));
|
||||||
dbContext.Ciphers.RemoveRange(dbContext.Ciphers.Where(c => c.UserId == user.Id));
|
dbContext.Ciphers.RemoveRange(dbContext.Ciphers.Where(c => c.UserId == user.Id));
|
||||||
dbContext.Folders.RemoveRange(dbContext.Folders.Where(f => f.UserId == user.Id));
|
dbContext.Folders.RemoveRange(dbContext.Folders.Where(f => f.UserId == user.Id));
|
||||||
dbContext.AuthRequests.RemoveRange(dbContext.AuthRequests.Where(s => s.UserId == user.Id));
|
dbContext.AuthRequests.RemoveRange(dbContext.AuthRequests.Where(s => s.UserId == user.Id));
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
CREATE PROCEDURE [dbo].[Organization_DeleteById]
|
CREATE PROCEDURE [dbo].[Organization_DeleteById]
|
||||||
@Id UNIQUEIDENTIFIER
|
@Id UNIQUEIDENTIFIER
|
||||||
|
WITH RECOMPILE
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
@ -25,6 +26,12 @@ BEGIN
|
|||||||
|
|
||||||
BEGIN TRANSACTION Organization_DeleteById
|
BEGIN TRANSACTION Organization_DeleteById
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[AuthRequest]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
DELETE
|
DELETE
|
||||||
FROM
|
FROM
|
||||||
[dbo].[SsoUser]
|
[dbo].[SsoUser]
|
||||||
|
@ -24,6 +24,13 @@ BEGIN
|
|||||||
|
|
||||||
BEGIN TRANSACTION User_DeleteById
|
BEGIN TRANSACTION User_DeleteById
|
||||||
|
|
||||||
|
-- Delete WebAuthnCredentials
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[WebAuthnCredential]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
-- Delete folders
|
-- Delete folders
|
||||||
DELETE
|
DELETE
|
||||||
FROM
|
FROM
|
||||||
|
@ -0,0 +1,136 @@
|
|||||||
|
IF OBJECT_ID('[dbo].[Organization_DeleteById]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[Organization_DeleteById]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[Organization_DeleteById]
|
||||||
|
@Id UNIQUEIDENTIFIER
|
||||||
|
WITH
|
||||||
|
RECOMPILE
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @Id
|
||||||
|
|
||||||
|
DECLARE @BatchSize INT = 100
|
||||||
|
WHILE @BatchSize > 0
|
||||||
|
BEGIN
|
||||||
|
BEGIN TRANSACTION Organization_DeleteById_Ciphers
|
||||||
|
|
||||||
|
DELETE TOP(@BatchSize)
|
||||||
|
FROM
|
||||||
|
[dbo].[Cipher]
|
||||||
|
WHERE
|
||||||
|
[UserId] IS NULL
|
||||||
|
AND [OrganizationId] = @Id
|
||||||
|
|
||||||
|
SET @BatchSize = @@ROWCOUNT
|
||||||
|
|
||||||
|
COMMIT TRANSACTION Organization_DeleteById_Ciphers
|
||||||
|
END
|
||||||
|
|
||||||
|
BEGIN TRANSACTION Organization_DeleteById
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[AuthRequest]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[SsoUser]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[SsoConfig]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE CU
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON [CU].[OrganizationUserId] = [OU].[Id]
|
||||||
|
WHERE
|
||||||
|
[OU].[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE AP
|
||||||
|
FROM
|
||||||
|
[dbo].[AccessPolicy] AP
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON [AP].[OrganizationUserId] = [OU].[Id]
|
||||||
|
WHERE
|
||||||
|
[OU].[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE GU
|
||||||
|
FROM
|
||||||
|
[dbo].[GroupUser] GU
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON [GU].[OrganizationUserId] = [OU].[Id]
|
||||||
|
WHERE
|
||||||
|
[OU].[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationUser]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[ProviderOrganization]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
EXEC [dbo].[OrganizationApiKey_OrganizationDeleted] @Id
|
||||||
|
EXEC [dbo].[OrganizationConnection_OrganizationDeleted] @Id
|
||||||
|
EXEC [dbo].[OrganizationSponsorship_OrganizationDeleted] @Id
|
||||||
|
EXEC [dbo].[OrganizationDomain_OrganizationDeleted] @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[Project]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[Secret]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE AK
|
||||||
|
FROM
|
||||||
|
[dbo].[ApiKey] AK
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[ServiceAccount] SA ON [AK].[ServiceAccountId] = [SA].[Id]
|
||||||
|
WHERE
|
||||||
|
[SA].[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE AP
|
||||||
|
FROM
|
||||||
|
[dbo].[AccessPolicy] AP
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[ServiceAccount] SA ON [AP].[GrantedServiceAccountId] = [SA].[Id]
|
||||||
|
WHERE
|
||||||
|
[SA].[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[ServiceAccount]
|
||||||
|
WHERE
|
||||||
|
[OrganizationId] = @Id
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[Organization]
|
||||||
|
WHERE
|
||||||
|
[Id] = @Id
|
||||||
|
|
||||||
|
COMMIT TRANSACTION Organization_DeleteById
|
||||||
|
END
|
@ -0,0 +1,137 @@
|
|||||||
|
IF OBJECT_ID('[dbo].[User_DeleteById]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[User_DeleteById]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[User_DeleteById]
|
||||||
|
@Id UNIQUEIDENTIFIER
|
||||||
|
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] = @Id
|
||||||
|
|
||||||
|
SET @BatchSize = @@ROWCOUNT
|
||||||
|
|
||||||
|
COMMIT TRANSACTION User_DeleteById_Ciphers
|
||||||
|
END
|
||||||
|
|
||||||
|
BEGIN TRANSACTION User_DeleteById
|
||||||
|
|
||||||
|
-- Delete WebAuthnCredentials
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[WebAuthnCredential]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete folders
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[Folder]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete AuthRequest, must be before Device
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[AuthRequest]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete devices
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[Device]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete collection users
|
||||||
|
DELETE
|
||||||
|
CU
|
||||||
|
FROM
|
||||||
|
[dbo].[CollectionUser] CU
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON OU.[Id] = CU.[OrganizationUserId]
|
||||||
|
WHERE
|
||||||
|
OU.[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete group users
|
||||||
|
DELETE
|
||||||
|
GU
|
||||||
|
FROM
|
||||||
|
[dbo].[GroupUser] GU
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON OU.[Id] = GU.[OrganizationUserId]
|
||||||
|
WHERE
|
||||||
|
OU.[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete AccessPolicy
|
||||||
|
DELETE
|
||||||
|
AP
|
||||||
|
FROM
|
||||||
|
[dbo].[AccessPolicy] AP
|
||||||
|
INNER JOIN
|
||||||
|
[dbo].[OrganizationUser] OU ON OU.[Id] = AP.[OrganizationUserId]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete organization users
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[OrganizationUser]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete provider users
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[ProviderUser]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete SSO Users
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[SsoUser]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Delete Emergency Accesses
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[EmergencyAccess]
|
||||||
|
WHERE
|
||||||
|
[GrantorId] = @Id
|
||||||
|
OR
|
||||||
|
[GranteeId] = @Id
|
||||||
|
|
||||||
|
-- Delete Sends
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[Send]
|
||||||
|
WHERE
|
||||||
|
[UserId] = @Id
|
||||||
|
|
||||||
|
-- Finally, delete the user
|
||||||
|
DELETE
|
||||||
|
FROM
|
||||||
|
[dbo].[User]
|
||||||
|
WHERE
|
||||||
|
[Id] = @Id
|
||||||
|
|
||||||
|
COMMIT TRANSACTION User_DeleteById
|
||||||
|
END
|
Loading…
x
Reference in New Issue
Block a user