1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 09:32:48 -05:00

[AC-1682] Combining all updated OrganizationUserIds to bump all revision dates at the same time

This commit is contained in:
Rui Tome
2024-01-18 16:32:14 +00:00
parent 160fc3edd0
commit 6a519b9180
2 changed files with 82 additions and 81 deletions

View File

@ -7,31 +7,18 @@ BEGIN
-- Step 1: AccessAll migration for Groups -- Step 1: AccessAll migration for Groups
-- Create a temporary table to store the groups with AccessAll = 1 -- Create a temporary table to store the groups with AccessAll = 1
SELECT [Id] AS [GroupId], [OrganizationId] SELECT [Id] AS [GroupId], [OrganizationId]
INTO #TempGroup INTO #TempStep1
FROM [dbo].[Group] FROM [dbo].[Group]
WHERE [AccessAll] = 1 WHERE [OrganizationId] = @OrganizationId
AND [OrganizationId] = @OrganizationId; AND [AccessAll] = 1;
-- Create a temporary table to store distinct OrganizationUserIds
DECLARE @Step1OrgUsersToBump [dbo].[GuidIdArray]
INSERT INTO @Step1OrgUsersToBump
SELECT DISTINCT GU.[OrganizationUserId] AS Id
FROM [dbo].[GroupUser] GU
INNER JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
-- Step 2: AccessAll migration for OrganizationUsers -- Step 2: AccessAll migration for OrganizationUsers
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1 -- Create a temporary table to store the OrganizationUsers with AccessAll = 1
SELECT [Id] AS [OrganizationUserId], [OrganizationId] SELECT [Id] AS [OrganizationUserId], [OrganizationId]
INTO #TempOrgUser INTO #TempStep2
FROM [dbo].[OrganizationUser] FROM [dbo].[OrganizationUser]
WHERE [AccessAll] = 1 WHERE [OrganizationId] = @OrganizationId
AND [OrganizationId] = @OrganizationId; AND [AccessAll] = 1;
-- Create a temporary table to store distinct OrganizationUserIds
DECLARE @Step2OrgUsersToBump [dbo].[GuidIdArray]
INSERT INTO @Step2OrgUsersToBump
SELECT DISTINCT [OrganizationUserId] AS Id
FROM #TempOrgUser;
-- Step 3: For all OrganizationUsers with Manager role or 'EditAssignedCollections' permission update their existing CollectionUser rows and insert new rows with [Manage] = 1 -- Step 3: For all OrganizationUsers with Manager role or 'EditAssignedCollections' permission update their existing CollectionUser rows and insert new rows with [Manage] = 1
-- and finally update all OrganizationUsers with Manager role to User role -- and finally update all OrganizationUsers with Manager role to User role
@ -40,14 +27,33 @@ BEGIN
CASE WHEN ou.[Type] = 3 THEN 1 ELSE 0 END AS [IsManager] CASE WHEN ou.[Type] = 3 THEN 1 ELSE 0 END AS [IsManager]
INTO #TempStep3 INTO #TempStep3
FROM [dbo].[OrganizationUser] ou FROM [dbo].[OrganizationUser] ou
WHERE ou.[OrganizationId] = @OrganizationId AND (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND WHERE ou.[OrganizationId] = @OrganizationId
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true')); AND (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL
AND ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
-- Create a temporary table to store distinct OrganizationUserIds -- Step 4: Bump AccountRevisionDate for all OrganizationUsers updated in the previous steps
DECLARE @Step3OrgUsersToBump [dbo].[GuidIdArray] -- Combine and union the distinct OrganizationUserIds from all steps into a single variable
INSERT INTO @Step3OrgUsersToBump DECLARE @OrgUsersToBump [dbo].[GuidIdArray]
INSERT INTO @OrgUsersToBump
SELECT DISTINCT [OrganizationUserId] AS Id SELECT DISTINCT [OrganizationUserId] AS Id
FROM #TempStep3; FROM (
-- Step 1
SELECT GU.[OrganizationUserId]
FROM [dbo].[GroupUser] GU
INNER JOIN #TempStep1 TG ON GU.[GroupId] = TG.[GroupId]
UNION
-- Step 2
SELECT [OrganizationUserId]
FROM #TempStep2
UNION
-- Step 3
SELECT [OrganizationUserId]
FROM #TempStep3
) AS CombinedOrgUsers;
BEGIN TRY BEGIN TRY
BEGIN TRANSACTION; BEGIN TRANSACTION;
@ -60,14 +66,14 @@ BEGIN
CG.[Manage] = 0 CG.[Manage] = 0
FROM [dbo].[CollectionGroup] CG FROM [dbo].[CollectionGroup] CG
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id] INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId] INNER JOIN #TempStep1 TG ON CG.[GroupId] = TG.[GroupId]
WHERE C.[OrganizationId] = TG.[OrganizationId]; WHERE C.[OrganizationId] = TG.[OrganizationId];
-- Insert new rows into [dbo].[CollectionGroup] -- Insert new rows into [dbo].[CollectionGroup]
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage]) INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
SELECT C.[Id], TG.[GroupId], 0, 0, 0 SELECT C.[Id], TG.[GroupId], 0, 0, 0
FROM [dbo].[Collection] C FROM [dbo].[Collection] C
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId] INNER JOIN #TempStep1 TG ON C.[OrganizationId] = TG.[OrganizationId]
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId] LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
WHERE CG.[CollectionId] IS NULL; WHERE CG.[CollectionId] IS NULL;
@ -75,10 +81,7 @@ BEGIN
UPDATE G UPDATE G
SET [AccessAll] = 0 SET [AccessAll] = 0
FROM [dbo].[Group] G FROM [dbo].[Group] G
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]; INNER JOIN #TempStep1 TG ON G.[Id] = TG.[GroupId];
-- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Step1OrgUsersToBump;
-- Step 2 -- Step 2
-- Update existing rows in [dbo].[CollectionUser] -- Update existing rows in [dbo].[CollectionUser]
@ -91,7 +94,7 @@ BEGIN
INNER JOIN ( INNER JOIN (
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId] SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
FROM [dbo].[Collection] C FROM [dbo].[Collection] C
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] INNER JOIN #TempStep2 T ON C.[OrganizationId] = T.[OrganizationId]
) AS source ) AS source
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]; ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
@ -101,7 +104,7 @@ BEGIN
FROM ( FROM (
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId] SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
FROM [dbo].[Collection] C FROM [dbo].[Collection] C
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] INNER JOIN #TempStep2 T ON C.[OrganizationId] = T.[OrganizationId]
) AS source ) AS source
LEFT JOIN [dbo].[CollectionUser] AS target LEFT JOIN [dbo].[CollectionUser] AS target
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId] ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]
@ -111,10 +114,7 @@ BEGIN
UPDATE OU UPDATE OU
SET [AccessAll] = 0 SET [AccessAll] = 0
FROM [dbo].[OrganizationUser] OU FROM [dbo].[OrganizationUser] OU
INNER JOIN #TempOrgUser T ON OU.[Id] = T.[OrganizationUserId]; INNER JOIN #TempStep2 T ON OU.[Id] = T.[OrganizationUserId];
-- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Step2OrgUsersToBump;
-- Step 3 -- Step 3
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table -- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
@ -143,8 +143,9 @@ BEGIN
INNER JOIN #TempStep3 temp ON ou.[Id] = temp.[OrganizationUserId] INNER JOIN #TempStep3 temp ON ou.[Id] = temp.[OrganizationUserId]
WHERE temp.[IsManager] = 1; -- Filter for Managers WHERE temp.[IsManager] = 1; -- Filter for Managers
-- Step 4
-- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds -- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Step3OrgUsersToBump; EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @OrgUsersToBump;
COMMIT TRANSACTION; COMMIT TRANSACTION;
END TRY END TRY
BEGIN CATCH BEGIN CATCH
@ -153,8 +154,7 @@ BEGIN
END CATCH; END CATCH;
-- Drop the temporary table -- Drop the temporary table
DROP TABLE #TempGroup; DROP TABLE #TempStep1;
DROP TABLE #TempOrgUser; DROP TABLE #TempStep2;
DROP TABLE #TempStep3; DROP TABLE #TempStep3;
END END
GO

View File

@ -7,31 +7,18 @@ BEGIN
-- Step 1: AccessAll migration for Groups -- Step 1: AccessAll migration for Groups
-- Create a temporary table to store the groups with AccessAll = 1 -- Create a temporary table to store the groups with AccessAll = 1
SELECT [Id] AS [GroupId], [OrganizationId] SELECT [Id] AS [GroupId], [OrganizationId]
INTO #TempGroup INTO #TempStep1
FROM [dbo].[Group] FROM [dbo].[Group]
WHERE [AccessAll] = 1 WHERE [OrganizationId] = @OrganizationId
AND [OrganizationId] = @OrganizationId; AND [AccessAll] = 1;
-- Create a temporary table to store distinct OrganizationUserIds
DECLARE @Step1OrgUsersToBump [dbo].[GuidIdArray]
INSERT INTO @Step1OrgUsersToBump
SELECT DISTINCT GU.[OrganizationUserId] AS Id
FROM [dbo].[GroupUser] GU
INNER JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
-- Step 2: AccessAll migration for OrganizationUsers -- Step 2: AccessAll migration for OrganizationUsers
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1 -- Create a temporary table to store the OrganizationUsers with AccessAll = 1
SELECT [Id] AS [OrganizationUserId], [OrganizationId] SELECT [Id] AS [OrganizationUserId], [OrganizationId]
INTO #TempOrgUser INTO #TempStep2
FROM [dbo].[OrganizationUser] FROM [dbo].[OrganizationUser]
WHERE [AccessAll] = 1 WHERE [OrganizationId] = @OrganizationId
AND [OrganizationId] = @OrganizationId; AND [AccessAll] = 1;
-- Create a temporary table to store distinct OrganizationUserIds
DECLARE @Step2OrgUsersToBump [dbo].[GuidIdArray]
INSERT INTO @Step2OrgUsersToBump
SELECT DISTINCT [OrganizationUserId] AS Id
FROM #TempOrgUser;
-- Step 3: For all OrganizationUsers with Manager role or 'EditAssignedCollections' permission update their existing CollectionUser rows and insert new rows with [Manage] = 1 -- Step 3: For all OrganizationUsers with Manager role or 'EditAssignedCollections' permission update their existing CollectionUser rows and insert new rows with [Manage] = 1
-- and finally update all OrganizationUsers with Manager role to User role -- and finally update all OrganizationUsers with Manager role to User role
@ -40,14 +27,33 @@ BEGIN
CASE WHEN ou.[Type] = 3 THEN 1 ELSE 0 END AS [IsManager] CASE WHEN ou.[Type] = 3 THEN 1 ELSE 0 END AS [IsManager]
INTO #TempStep3 INTO #TempStep3
FROM [dbo].[OrganizationUser] ou FROM [dbo].[OrganizationUser] ou
WHERE ou.[OrganizationId] = @OrganizationId AND (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND WHERE ou.[OrganizationId] = @OrganizationId
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true')); AND (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL
AND ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
-- Create a temporary table to store distinct OrganizationUserIds -- Step 4: Bump AccountRevisionDate for all OrganizationUsers updated in the previous steps
DECLARE @Step3OrgUsersToBump [dbo].[GuidIdArray] -- Combine and union the distinct OrganizationUserIds from all steps into a single variable
INSERT INTO @Step3OrgUsersToBump DECLARE @OrgUsersToBump [dbo].[GuidIdArray]
INSERT INTO @OrgUsersToBump
SELECT DISTINCT [OrganizationUserId] AS Id SELECT DISTINCT [OrganizationUserId] AS Id
FROM #TempStep3; FROM (
-- Step 1
SELECT GU.[OrganizationUserId]
FROM [dbo].[GroupUser] GU
INNER JOIN #TempStep1 TG ON GU.[GroupId] = TG.[GroupId]
UNION
-- Step 2
SELECT [OrganizationUserId]
FROM #TempStep2
UNION
-- Step 3
SELECT [OrganizationUserId]
FROM #TempStep3
) AS CombinedOrgUsers;
BEGIN TRY BEGIN TRY
BEGIN TRANSACTION; BEGIN TRANSACTION;
@ -60,14 +66,14 @@ BEGIN
CG.[Manage] = 0 CG.[Manage] = 0
FROM [dbo].[CollectionGroup] CG FROM [dbo].[CollectionGroup] CG
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id] INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId] INNER JOIN #TempStep1 TG ON CG.[GroupId] = TG.[GroupId]
WHERE C.[OrganizationId] = TG.[OrganizationId]; WHERE C.[OrganizationId] = TG.[OrganizationId];
-- Insert new rows into [dbo].[CollectionGroup] -- Insert new rows into [dbo].[CollectionGroup]
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage]) INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
SELECT C.[Id], TG.[GroupId], 0, 0, 0 SELECT C.[Id], TG.[GroupId], 0, 0, 0
FROM [dbo].[Collection] C FROM [dbo].[Collection] C
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId] INNER JOIN #TempStep1 TG ON C.[OrganizationId] = TG.[OrganizationId]
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId] LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
WHERE CG.[CollectionId] IS NULL; WHERE CG.[CollectionId] IS NULL;
@ -75,10 +81,7 @@ BEGIN
UPDATE G UPDATE G
SET [AccessAll] = 0 SET [AccessAll] = 0
FROM [dbo].[Group] G FROM [dbo].[Group] G
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]; INNER JOIN #TempStep1 TG ON G.[Id] = TG.[GroupId];
-- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Step1OrgUsersToBump;
-- Step 2 -- Step 2
-- Update existing rows in [dbo].[CollectionUser] -- Update existing rows in [dbo].[CollectionUser]
@ -91,7 +94,7 @@ BEGIN
INNER JOIN ( INNER JOIN (
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId] SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
FROM [dbo].[Collection] C FROM [dbo].[Collection] C
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] INNER JOIN #TempStep2 T ON C.[OrganizationId] = T.[OrganizationId]
) AS source ) AS source
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]; ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
@ -101,7 +104,7 @@ BEGIN
FROM ( FROM (
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId] SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
FROM [dbo].[Collection] C FROM [dbo].[Collection] C
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] INNER JOIN #TempStep2 T ON C.[OrganizationId] = T.[OrganizationId]
) AS source ) AS source
LEFT JOIN [dbo].[CollectionUser] AS target LEFT JOIN [dbo].[CollectionUser] AS target
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId] ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]
@ -111,10 +114,7 @@ BEGIN
UPDATE OU UPDATE OU
SET [AccessAll] = 0 SET [AccessAll] = 0
FROM [dbo].[OrganizationUser] OU FROM [dbo].[OrganizationUser] OU
INNER JOIN #TempOrgUser T ON OU.[Id] = T.[OrganizationUserId]; INNER JOIN #TempStep2 T ON OU.[Id] = T.[OrganizationUserId];
-- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Step2OrgUsersToBump;
-- Step 3 -- Step 3
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table -- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
@ -143,8 +143,9 @@ BEGIN
INNER JOIN #TempStep3 temp ON ou.[Id] = temp.[OrganizationUserId] INNER JOIN #TempStep3 temp ON ou.[Id] = temp.[OrganizationUserId]
WHERE temp.[IsManager] = 1; -- Filter for Managers WHERE temp.[IsManager] = 1; -- Filter for Managers
-- Step 4
-- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds -- Execute User_BumpAccountRevisionDateByOrganizationUserIds for the distinct OrganizationUserIds
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Step3OrgUsersToBump; EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @OrgUsersToBump;
COMMIT TRANSACTION; COMMIT TRANSACTION;
END TRY END TRY
BEGIN CATCH BEGIN CATCH
@ -153,8 +154,8 @@ BEGIN
END CATCH; END CATCH;
-- Drop the temporary table -- Drop the temporary table
DROP TABLE #TempGroup; DROP TABLE #TempStep1;
DROP TABLE #TempOrgUser; DROP TABLE #TempStep2;
DROP TABLE #TempStep3; DROP TABLE #TempStep3;
END END
GO GO