mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
[AC-1682] Placed temp tables outside transactions
This commit is contained in:
@ -5,295 +5,295 @@ BEGIN
|
|||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
|
|
||||||
-- Step 1: AccessAll migration for Groups
|
-- Step 1: AccessAll migration for Groups
|
||||||
BEGIN TRY
|
-- Create a temporary table to store the groups with AccessAll = 1
|
||||||
BEGIN TRANSACTION;
|
SELECT [Id] AS [GroupId], [OrganizationId]
|
||||||
-- Create a temporary table to store the groups with AccessAll = 1
|
INTO #TempGroup
|
||||||
SELECT [Id] AS [GroupId], [OrganizationId]
|
FROM [dbo].[Group]
|
||||||
INTO #TempGroup
|
WHERE [AccessAll] = 1
|
||||||
FROM [dbo].[Group]
|
AND [OrganizationId] = @OrganizationId;
|
||||||
WHERE [AccessAll] = 1
|
|
||||||
AND [OrganizationId] = @OrganizationId;
|
|
||||||
|
|
||||||
-- Update existing rows in [dbo].[CollectionGroup]
|
-- Create a temporary table to store distinct OrganizationUserIds
|
||||||
UPDATE CG
|
SELECT DISTINCT GU.[OrganizationUserId]
|
||||||
SET
|
INTO #TempOrganizationUsers
|
||||||
CG.[ReadOnly] = 0,
|
FROM [dbo].[GroupUser] GU
|
||||||
CG.[HidePasswords] = 0,
|
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
|
||||||
CG.[Manage] = 0
|
|
||||||
FROM [dbo].[CollectionGroup] CG
|
|
||||||
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
|
|
||||||
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId]
|
|
||||||
WHERE C.[OrganizationId] = TG.[OrganizationId];
|
|
||||||
|
|
||||||
-- Insert new rows into [dbo].[CollectionGroup]
|
BEGIN TRY
|
||||||
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
|
BEGIN TRANSACTION;
|
||||||
SELECT C.[Id], TG.[GroupId], 0, 0, 0
|
-- Update existing rows in [dbo].[CollectionGroup]
|
||||||
FROM [dbo].[Collection] C
|
UPDATE CG
|
||||||
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId]
|
SET
|
||||||
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
|
CG.[ReadOnly] = 0,
|
||||||
WHERE CG.[CollectionId] IS NULL;
|
CG.[HidePasswords] = 0,
|
||||||
|
CG.[Manage] = 0
|
||||||
|
FROM [dbo].[CollectionGroup] CG
|
||||||
|
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
|
||||||
|
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId]
|
||||||
|
WHERE C.[OrganizationId] = TG.[OrganizationId];
|
||||||
|
|
||||||
-- Update Group to clear AccessAll flag
|
-- Insert new rows into [dbo].[CollectionGroup]
|
||||||
UPDATE G
|
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
SET [AccessAll] = 0
|
SELECT C.[Id], TG.[GroupId], 0, 0, 0
|
||||||
FROM [dbo].[Group] G
|
FROM [dbo].[Collection] C
|
||||||
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
|
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId]
|
||||||
|
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
|
||||||
|
WHERE CG.[CollectionId] IS NULL;
|
||||||
|
|
||||||
-- Create a temporary table to store distinct OrganizationUserIds
|
-- Update Group to clear AccessAll flag
|
||||||
SELECT DISTINCT GU.[OrganizationUserId]
|
UPDATE G
|
||||||
INTO #TempOrganizationUsers
|
SET [AccessAll] = 0
|
||||||
FROM [dbo].[GroupUser] GU
|
FROM [dbo].[Group] G
|
||||||
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
|
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step1OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step1OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT [OrganizationUserId]
|
SELECT [OrganizationUserId]
|
||||||
FROM #TempOrganizationUsers
|
FROM #TempOrganizationUsers
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step1OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step1OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempGroup;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
||||||
DROP TABLE #TempOrganizationUsers;
|
END
|
||||||
COMMIT TRANSACTION;
|
|
||||||
END TRY
|
CLOSE UniqueOrgUserIdCursor
|
||||||
BEGIN CATCH
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
ROLLBACK TRANSACTION;
|
COMMIT TRANSACTION;
|
||||||
THROW;
|
END TRY
|
||||||
END CATCH;
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempGroup;
|
||||||
|
DROP TABLE #TempOrganizationUsers;
|
||||||
|
|
||||||
-- Step 2: AccessAll migration for users
|
-- Step 2: AccessAll migration for users
|
||||||
BEGIN TRY
|
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1
|
||||||
BEGIN TRANSACTION;
|
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
|
||||||
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1
|
INTO #TempOrgUser
|
||||||
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
|
FROM [dbo].[OrganizationUser]
|
||||||
INTO #TempOrgUser
|
WHERE [AccessAll] = 1
|
||||||
FROM [dbo].[OrganizationUser]
|
AND [OrganizationId] = @OrganizationId;
|
||||||
WHERE [AccessAll] = 1
|
|
||||||
AND [OrganizationId] = @OrganizationId;
|
|
||||||
|
|
||||||
-- Update existing rows in [dbo].[CollectionUser]
|
BEGIN TRY
|
||||||
UPDATE target
|
BEGIN TRANSACTION;
|
||||||
SET
|
-- Update existing rows in [dbo].[CollectionUser]
|
||||||
target.[ReadOnly] = 0,
|
UPDATE target
|
||||||
target.[HidePasswords] = 0,
|
SET
|
||||||
target.[Manage] = 0
|
target.[ReadOnly] = 0,
|
||||||
FROM [dbo].[CollectionUser] AS target
|
target.[HidePasswords] = 0,
|
||||||
INNER JOIN (
|
target.[Manage] = 0
|
||||||
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
FROM [dbo].[CollectionUser] AS target
|
||||||
FROM [dbo].[Collection] C
|
INNER JOIN (
|
||||||
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
|
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
||||||
) AS source
|
FROM [dbo].[Collection] C
|
||||||
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
|
||||||
|
) AS source
|
||||||
|
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
||||||
|
|
||||||
-- Insert new rows into [dbo].[CollectionUser]
|
-- Insert new rows into [dbo].[CollectionUser]
|
||||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
|
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
|
||||||
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 #TempOrgUser 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]
|
||||||
WHERE target.[CollectionId] IS NULL;
|
WHERE target.[CollectionId] IS NULL;
|
||||||
|
|
||||||
-- Update OrganizationUser to clear AccessAll flag
|
-- Update OrganizationUser to clear AccessAll flag
|
||||||
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 #TempOrgUser T ON OU.[Id] = T.[OrganizationUserId]
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step2OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step2OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT DISTINCT [OrganizationUserId]
|
SELECT DISTINCT [OrganizationUserId]
|
||||||
FROM #TempOrgUser
|
FROM #TempOrgUser
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step2OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step2OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempOrgUser;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempOrgUser;
|
||||||
|
|
||||||
-- Step 3: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
-- Step 3: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||||
BEGIN TRY
|
-- Store the results in a temporary table
|
||||||
BEGIN TRANSACTION;
|
SELECT ou.[Id] AS [OrganizationUserId]
|
||||||
-- Store the results in a temporary table
|
INTO #TempStep3
|
||||||
SELECT ou.[Id] AS [OrganizationUserId]
|
FROM [dbo].[OrganizationUser] ou
|
||||||
INTO #TempStep3
|
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
|
||||||
FROM [dbo].[OrganizationUser] ou
|
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
|
||||||
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
|
|
||||||
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
|
|
||||||
|
|
||||||
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
|
BEGIN TRY
|
||||||
UPDATE cu
|
BEGIN TRANSACTION;
|
||||||
SET cu.[ReadOnly] = 0,
|
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
|
||||||
cu.[HidePasswords] = 0,
|
UPDATE cu
|
||||||
cu.[Manage] = 1
|
SET cu.[ReadOnly] = 0,
|
||||||
FROM [dbo].[CollectionUser] cu
|
cu.[HidePasswords] = 0,
|
||||||
INNER JOIN #TempStep3 temp ON cu.[OrganizationUserId] = temp.[OrganizationUserId];
|
cu.[Manage] = 1
|
||||||
|
FROM [dbo].[CollectionUser] cu
|
||||||
|
INNER JOIN #TempStep3 temp ON cu.[OrganizationUserId] = temp.[OrganizationUserId];
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step3OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step3OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT [OrganizationUserId]
|
SELECT [OrganizationUserId]
|
||||||
FROM #TempStep3
|
FROM #TempStep3
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step3OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step3OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempStep3;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempStep3;
|
||||||
|
|
||||||
-- Step 4: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
-- Step 4: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
||||||
BEGIN TRY
|
-- Store the results in a temporary table
|
||||||
BEGIN TRANSACTION;
|
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
|
||||||
-- Store the results in a temporary table
|
INTO #TempStep4
|
||||||
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
|
FROM [dbo].[CollectionGroup] cg
|
||||||
INTO #TempStep4
|
INNER JOIN [dbo].[GroupUser] gu ON cg.GroupId = gu.GroupId
|
||||||
FROM [dbo].[CollectionGroup] cg
|
INNER JOIN [dbo].[OrganizationUser] ou ON gu.OrganizationUserId = ou.[Id]
|
||||||
INNER JOIN [dbo].[GroupUser] gu ON cg.GroupId = gu.GroupId
|
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'))
|
||||||
INNER JOIN [dbo].[OrganizationUser] ou ON gu.OrganizationUserId = ou.[Id]
|
AND NOT EXISTS (
|
||||||
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'))
|
SELECT 1 FROM [dbo].[CollectionUser] cu
|
||||||
AND NOT EXISTS (
|
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
|
||||||
SELECT 1 FROM [dbo].[CollectionUser] cu
|
);
|
||||||
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Insert rows into [dbo].[CollectionUser] using the temporary table
|
BEGIN TRY
|
||||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
BEGIN TRANSACTION;
|
||||||
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
|
-- Insert rows into [dbo].[CollectionUser] using the temporary table
|
||||||
FROM #TempStep4;
|
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
|
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
|
||||||
|
FROM #TempStep4;
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step4OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step4OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT DISTINCT [OrganizationUserId]
|
SELECT DISTINCT [OrganizationUserId]
|
||||||
FROM #TempStep4
|
FROM #TempStep4
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step4OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step4OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempStep4;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempStep4;
|
||||||
|
|
||||||
-- Step 5: Set all Managers to Users
|
-- Step 5: Set all Managers to Users
|
||||||
BEGIN TRY
|
-- Store the results in a temporary table
|
||||||
BEGIN TRANSACTION;
|
SELECT [Id] AS [OrganizationUserId]
|
||||||
-- Store the results in a temporary table
|
INTO #TempStep5
|
||||||
SELECT [Id] AS [OrganizationUserId]
|
FROM [dbo].[OrganizationUser]
|
||||||
INTO #TempStep5
|
WHERE [Type] = 3; -- Manager
|
||||||
FROM [dbo].[OrganizationUser]
|
|
||||||
WHERE [Type] = 3; -- Manager
|
|
||||||
|
|
||||||
-- Update [dbo].[OrganizationUser] based on the temporary table
|
BEGIN TRY
|
||||||
UPDATE ou
|
BEGIN TRANSACTION;
|
||||||
SET ou.[Type] = 2 -- User
|
-- Update [dbo].[OrganizationUser] based on the temporary table
|
||||||
FROM [dbo].[OrganizationUser] ou
|
UPDATE ou
|
||||||
INNER JOIN #TempStep5 temp ON ou.[Id] = temp.[OrganizationUserId];
|
SET ou.[Type] = 2 -- User
|
||||||
|
FROM [dbo].[OrganizationUser] ou
|
||||||
|
INNER JOIN #TempStep5 temp ON ou.[Id] = temp.[OrganizationUserId];
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step5OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step5OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT [OrganizationUserId]
|
SELECT [OrganizationUserId]
|
||||||
FROM #TempStep5
|
FROM #TempStep5
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step5OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step5OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempStep5;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempStep5;
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
@ -5,295 +5,295 @@ BEGIN
|
|||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
|
|
||||||
-- Step 1: AccessAll migration for Groups
|
-- Step 1: AccessAll migration for Groups
|
||||||
BEGIN TRY
|
-- Create a temporary table to store the groups with AccessAll = 1
|
||||||
BEGIN TRANSACTION;
|
SELECT [Id] AS [GroupId], [OrganizationId]
|
||||||
-- Create a temporary table to store the groups with AccessAll = 1
|
INTO #TempGroup
|
||||||
SELECT [Id] AS [GroupId], [OrganizationId]
|
FROM [dbo].[Group]
|
||||||
INTO #TempGroup
|
WHERE [AccessAll] = 1
|
||||||
FROM [dbo].[Group]
|
AND [OrganizationId] = @OrganizationId;
|
||||||
WHERE [AccessAll] = 1
|
|
||||||
AND [OrganizationId] = @OrganizationId;
|
|
||||||
|
|
||||||
-- Update existing rows in [dbo].[CollectionGroup]
|
-- Create a temporary table to store distinct OrganizationUserIds
|
||||||
UPDATE CG
|
SELECT DISTINCT GU.[OrganizationUserId]
|
||||||
SET
|
INTO #TempOrganizationUsers
|
||||||
CG.[ReadOnly] = 0,
|
FROM [dbo].[GroupUser] GU
|
||||||
CG.[HidePasswords] = 0,
|
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
|
||||||
CG.[Manage] = 0
|
|
||||||
FROM [dbo].[CollectionGroup] CG
|
|
||||||
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
|
|
||||||
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId]
|
|
||||||
WHERE C.[OrganizationId] = TG.[OrganizationId];
|
|
||||||
|
|
||||||
-- Insert new rows into [dbo].[CollectionGroup]
|
BEGIN TRY
|
||||||
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
|
BEGIN TRANSACTION;
|
||||||
SELECT C.[Id], TG.[GroupId], 0, 0, 0
|
-- Update existing rows in [dbo].[CollectionGroup]
|
||||||
FROM [dbo].[Collection] C
|
UPDATE CG
|
||||||
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId]
|
SET
|
||||||
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
|
CG.[ReadOnly] = 0,
|
||||||
WHERE CG.[CollectionId] IS NULL;
|
CG.[HidePasswords] = 0,
|
||||||
|
CG.[Manage] = 0
|
||||||
|
FROM [dbo].[CollectionGroup] CG
|
||||||
|
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
|
||||||
|
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId]
|
||||||
|
WHERE C.[OrganizationId] = TG.[OrganizationId];
|
||||||
|
|
||||||
-- Update Group to clear AccessAll flag
|
-- Insert new rows into [dbo].[CollectionGroup]
|
||||||
UPDATE G
|
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
SET [AccessAll] = 0
|
SELECT C.[Id], TG.[GroupId], 0, 0, 0
|
||||||
FROM [dbo].[Group] G
|
FROM [dbo].[Collection] C
|
||||||
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
|
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId]
|
||||||
|
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
|
||||||
|
WHERE CG.[CollectionId] IS NULL;
|
||||||
|
|
||||||
-- Create a temporary table to store distinct OrganizationUserIds
|
-- Update Group to clear AccessAll flag
|
||||||
SELECT DISTINCT GU.[OrganizationUserId]
|
UPDATE G
|
||||||
INTO #TempOrganizationUsers
|
SET [AccessAll] = 0
|
||||||
FROM [dbo].[GroupUser] GU
|
FROM [dbo].[Group] G
|
||||||
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
|
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step1OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step1OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT [OrganizationUserId]
|
SELECT [OrganizationUserId]
|
||||||
FROM #TempOrganizationUsers
|
FROM #TempOrganizationUsers
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step1OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step1OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempGroup;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step1OrganizationUserId
|
||||||
DROP TABLE #TempOrganizationUsers;
|
END
|
||||||
COMMIT TRANSACTION;
|
|
||||||
END TRY
|
CLOSE UniqueOrgUserIdCursor
|
||||||
BEGIN CATCH
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
ROLLBACK TRANSACTION;
|
COMMIT TRANSACTION;
|
||||||
THROW;
|
END TRY
|
||||||
END CATCH;
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempGroup;
|
||||||
|
DROP TABLE #TempOrganizationUsers;
|
||||||
|
|
||||||
-- Step 2: AccessAll migration for users
|
-- Step 2: AccessAll migration for users
|
||||||
BEGIN TRY
|
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1
|
||||||
BEGIN TRANSACTION;
|
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
|
||||||
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1
|
INTO #TempOrgUser
|
||||||
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
|
FROM [dbo].[OrganizationUser]
|
||||||
INTO #TempOrgUser
|
WHERE [AccessAll] = 1
|
||||||
FROM [dbo].[OrganizationUser]
|
AND [OrganizationId] = @OrganizationId;
|
||||||
WHERE [AccessAll] = 1
|
|
||||||
AND [OrganizationId] = @OrganizationId;
|
|
||||||
|
|
||||||
-- Update existing rows in [dbo].[CollectionUser]
|
BEGIN TRY
|
||||||
UPDATE target
|
BEGIN TRANSACTION;
|
||||||
SET
|
-- Update existing rows in [dbo].[CollectionUser]
|
||||||
target.[ReadOnly] = 0,
|
UPDATE target
|
||||||
target.[HidePasswords] = 0,
|
SET
|
||||||
target.[Manage] = 0
|
target.[ReadOnly] = 0,
|
||||||
FROM [dbo].[CollectionUser] AS target
|
target.[HidePasswords] = 0,
|
||||||
INNER JOIN (
|
target.[Manage] = 0
|
||||||
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
FROM [dbo].[CollectionUser] AS target
|
||||||
FROM [dbo].[Collection] C
|
INNER JOIN (
|
||||||
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
|
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
||||||
) AS source
|
FROM [dbo].[Collection] C
|
||||||
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
|
||||||
|
) AS source
|
||||||
|
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
||||||
|
|
||||||
-- Insert new rows into [dbo].[CollectionUser]
|
-- Insert new rows into [dbo].[CollectionUser]
|
||||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
|
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
|
||||||
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 #TempOrgUser 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]
|
||||||
WHERE target.[CollectionId] IS NULL;
|
WHERE target.[CollectionId] IS NULL;
|
||||||
|
|
||||||
-- Update OrganizationUser to clear AccessAll flag
|
-- Update OrganizationUser to clear AccessAll flag
|
||||||
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 #TempOrgUser T ON OU.[Id] = T.[OrganizationUserId]
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step2OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step2OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT DISTINCT [OrganizationUserId]
|
SELECT DISTINCT [OrganizationUserId]
|
||||||
FROM #TempOrgUser
|
FROM #TempOrgUser
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step2OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step2OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempOrgUser;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step2OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempOrgUser;
|
||||||
|
|
||||||
-- Step 3: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
-- Step 3: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||||
BEGIN TRY
|
-- Store the results in a temporary table
|
||||||
BEGIN TRANSACTION;
|
SELECT ou.[Id] AS [OrganizationUserId]
|
||||||
-- Store the results in a temporary table
|
INTO #TempStep3
|
||||||
SELECT ou.[Id] AS [OrganizationUserId]
|
FROM [dbo].[OrganizationUser] ou
|
||||||
INTO #TempStep3
|
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
|
||||||
FROM [dbo].[OrganizationUser] ou
|
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
|
||||||
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
|
|
||||||
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
|
|
||||||
|
|
||||||
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
|
BEGIN TRY
|
||||||
UPDATE cu
|
BEGIN TRANSACTION;
|
||||||
SET cu.[ReadOnly] = 0,
|
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
|
||||||
cu.[HidePasswords] = 0,
|
UPDATE cu
|
||||||
cu.[Manage] = 1
|
SET cu.[ReadOnly] = 0,
|
||||||
FROM [dbo].[CollectionUser] cu
|
cu.[HidePasswords] = 0,
|
||||||
INNER JOIN #TempStep3 temp ON cu.[OrganizationUserId] = temp.[OrganizationUserId];
|
cu.[Manage] = 1
|
||||||
|
FROM [dbo].[CollectionUser] cu
|
||||||
|
INNER JOIN #TempStep3 temp ON cu.[OrganizationUserId] = temp.[OrganizationUserId];
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step3OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step3OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT [OrganizationUserId]
|
SELECT [OrganizationUserId]
|
||||||
FROM #TempStep3
|
FROM #TempStep3
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step3OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step3OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempStep3;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step3OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempStep3;
|
||||||
|
|
||||||
-- Step 4: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
-- Step 4: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
||||||
BEGIN TRY
|
-- Store the results in a temporary table
|
||||||
BEGIN TRANSACTION;
|
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
|
||||||
-- Store the results in a temporary table
|
INTO #TempStep4
|
||||||
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
|
FROM [dbo].[CollectionGroup] cg
|
||||||
INTO #TempStep4
|
INNER JOIN [dbo].[GroupUser] gu ON cg.GroupId = gu.GroupId
|
||||||
FROM [dbo].[CollectionGroup] cg
|
INNER JOIN [dbo].[OrganizationUser] ou ON gu.OrganizationUserId = ou.[Id]
|
||||||
INNER JOIN [dbo].[GroupUser] gu ON cg.GroupId = gu.GroupId
|
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'))
|
||||||
INNER JOIN [dbo].[OrganizationUser] ou ON gu.OrganizationUserId = ou.[Id]
|
AND NOT EXISTS (
|
||||||
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'))
|
SELECT 1 FROM [dbo].[CollectionUser] cu
|
||||||
AND NOT EXISTS (
|
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
|
||||||
SELECT 1 FROM [dbo].[CollectionUser] cu
|
);
|
||||||
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Insert rows into [dbo].[CollectionUser] using the temporary table
|
BEGIN TRY
|
||||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
BEGIN TRANSACTION;
|
||||||
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
|
-- Insert rows into [dbo].[CollectionUser] using the temporary table
|
||||||
FROM #TempStep4;
|
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
|
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
|
||||||
|
FROM #TempStep4;
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step4OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step4OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT DISTINCT [OrganizationUserId]
|
SELECT DISTINCT [OrganizationUserId]
|
||||||
FROM #TempStep4
|
FROM #TempStep4
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step4OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step4OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempStep4;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step4OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempStep4;
|
||||||
|
|
||||||
-- Step 5: Set all Managers to Users
|
-- Step 5: Set all Managers to Users
|
||||||
BEGIN TRY
|
-- Store the results in a temporary table
|
||||||
BEGIN TRANSACTION;
|
SELECT [Id] AS [OrganizationUserId]
|
||||||
-- Store the results in a temporary table
|
INTO #TempStep5
|
||||||
SELECT [Id] AS [OrganizationUserId]
|
FROM [dbo].[OrganizationUser]
|
||||||
INTO #TempStep5
|
WHERE [Type] = 3; -- Manager
|
||||||
FROM [dbo].[OrganizationUser]
|
|
||||||
WHERE [Type] = 3; -- Manager
|
|
||||||
|
|
||||||
-- Update [dbo].[OrganizationUser] based on the temporary table
|
BEGIN TRY
|
||||||
UPDATE ou
|
BEGIN TRANSACTION;
|
||||||
SET ou.[Type] = 2 -- User
|
-- Update [dbo].[OrganizationUser] based on the temporary table
|
||||||
FROM [dbo].[OrganizationUser] ou
|
UPDATE ou
|
||||||
INNER JOIN #TempStep5 temp ON ou.[Id] = temp.[OrganizationUserId];
|
SET ou.[Type] = 2 -- User
|
||||||
|
FROM [dbo].[OrganizationUser] ou
|
||||||
|
INNER JOIN #TempStep5 temp ON ou.[Id] = temp.[OrganizationUserId];
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
||||||
DECLARE @Step5OrganizationUserId UNIQUEIDENTIFIER
|
DECLARE @Step5OrganizationUserId UNIQUEIDENTIFIER
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
||||||
SELECT [OrganizationUserId]
|
SELECT [OrganizationUserId]
|
||||||
FROM #TempStep5
|
FROM #TempStep5
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
OPEN UniqueOrgUserIdCursor
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step5OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
WHILE (@@FETCH_STATUS = 0)
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
BEGIN
|
||||||
|
-- Execute the stored procedure for the current OrganizationUserId
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @Step5OrganizationUserId
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Fetch the next row
|
||||||
DROP TABLE #TempStep5;
|
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @Step5OrganizationUserId
|
||||||
COMMIT TRANSACTION;
|
END
|
||||||
END TRY
|
|
||||||
BEGIN CATCH
|
CLOSE UniqueOrgUserIdCursor
|
||||||
ROLLBACK TRANSACTION;
|
DEALLOCATE UniqueOrgUserIdCursor;
|
||||||
THROW;
|
COMMIT TRANSACTION;
|
||||||
END CATCH;
|
END TRY
|
||||||
|
BEGIN CATCH
|
||||||
|
ROLLBACK TRANSACTION;
|
||||||
|
THROW;
|
||||||
|
END CATCH;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE #TempStep5;
|
||||||
END
|
END
|
||||||
GO
|
GO
|
||||||
|
Reference in New Issue
Block a user