-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SM-718: Delete appropriate Access Policies on Org deletion (#2868)
* SM-718: Delete appropriate Access Policies on Org deletion * SM-718: Add migration script for SPROC change * SM-718: Add GO statement to migration script * SM-718: Fix GroupUser bug that was also found (cherry picked from commit f961787)
- Loading branch information
1 parent
6633ec6
commit 7842e12
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
util/Migrator/DbScripts/2023-04-21_00_DeleteAccessPoliciesOnOrganizationDelete.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
CREATE OR ALTER PROCEDURE [dbo].[Organization_DeleteById] | ||
@Id UNIQUEIDENTIFIER | ||
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].[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 | ||
GO |