1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 08:32:50 -05:00

[AC-1682] Added script to update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission

This commit is contained in:
Rui Tome
2023-11-10 16:18:47 +00:00
parent e14ba4ddb1
commit 1527af7017
3 changed files with 19 additions and 7 deletions

View File

@ -1,10 +1,10 @@
-- Step 1: Retrieve relevant data from [dbo].[Group] where [AccessAll] is 1
-- Step 1: Retrieve Groups with [AccessAll] permission
SELECT [Id] AS [GroupId], [OrganizationId]
INTO #TempGroup
FROM [dbo].[Group]
WHERE [AccessAll] = 1;
-- Step 2: Declare variables for group and organization IDs
-- Step 2: Declare variables for GroupId and OrganizationId
DECLARE @GroupId UNIQUEIDENTIFIER;
DECLARE @OrganizationId UNIQUEIDENTIFIER;
@ -33,7 +33,7 @@ UPDATE SET
INSERT ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
VALUES (source.[CollectionId], source.[GroupId], 0, 0, 1);
-- Step 6: Fetch the next group and organization IDs
-- Step 6: Fetch the next GroupId and OrganizationId
FETCH NEXT FROM GroupCursor INTO @GroupId, @OrganizationId;
END;

View File

@ -1,10 +1,10 @@
-- Step 1: Retrieve relevant data from [dbo].[OrganizationUser] where [AccessAll] is 1
-- Step 1: Retrieve OrganizationUsers with [AccessAll] permission
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
INTO #TempOrgUser
FROM [dbo].[OrganizationUser]
WHERE [AccessAll] = 1;
-- Step 2: Declare variables for organization user and organization ID
-- Step 2: Declare variables for OrganizationUserId and OrganizationId
DECLARE @OrgUserId UNIQUEIDENTIFIER;
DECLARE @OrganizationId UNIQUEIDENTIFIER;
@ -22,7 +22,7 @@ WHILE @@FETCH_STATUS = 0
BEGIN
-- Step 5: Use MERGE to insert or update into [dbo].[CollectionUser] for each [dbo].[Collection] entry
MERGE INTO [dbo].[CollectionUser] AS target
USING (SELECT C.[Id] AS [CollectionId], @OrgUserId AS [OrganizationUserId] FROM [dbo].[Collection] C WHERE C.[OrganizationId] = @OrganizationId) AS source -- Adjusted to use OrganizationId
USING (SELECT C.[Id] AS [CollectionId], @OrgUserId AS [OrganizationUserId] FROM [dbo].[Collection] C WHERE C.[OrganizationId] = @OrganizationId) AS source
ON (target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId])
WHEN MATCHED THEN
UPDATE SET
@ -33,7 +33,7 @@ UPDATE SET
INSERT ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
VALUES (source.[CollectionId], source.[OrganizationUserId], 0, 0, 1);
-- Step 6: Fetch the next organization user and organization ID
-- Step 6: Fetch the next OrganizationUserId and OrganizationId
FETCH NEXT FROM OrgUserCursor INTO @OrgUserId, @OrganizationId;
END;

View File

@ -0,0 +1,12 @@
-- Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
UPDATE cu
SET cu.[ReadOnly] = 0,
cu.[HidePasswords] = 0,
cu.[Manage] = 1
FROM [dbo].[CollectionUser] cu
JOIN [dbo].[Collection] c
ON cu.[CollectionId] = c.[Id]
JOIN [dbo].[OrganizationUser] ou
ON cu.[OrganizationUserId] = ou.[Id]
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'))