mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
44 lines
1.6 KiB
Plaintext
44 lines
1.6 KiB
Plaintext
-- Step 1: Create a temporary table
|
|
CREATE TEMP TABLE IF NOT EXISTS TempOrgUser AS
|
|
SELECT "Id" AS "OrganizationUserId", "OrganizationId"
|
|
FROM "OrganizationUser"
|
|
WHERE "AccessAll" = true;
|
|
|
|
-- Step 2: Update existing rows in CollectionUsers
|
|
UPDATE "CollectionUsers" cu
|
|
SET
|
|
"ReadOnly" = false,
|
|
"HidePasswords" = false,
|
|
"Manage" = false
|
|
FROM "CollectionUsers" cuUpdate
|
|
INNER JOIN "Collection" C ON cuUpdate."CollectionId" = C."Id"
|
|
INNER JOIN TempOrgUser OU ON cuUpdate."OrganizationUserId" = OU."OrganizationUserId"
|
|
WHERE C."OrganizationId" = OU."OrganizationId";
|
|
|
|
-- Step 3: Insert new rows into CollectionUsers
|
|
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
|
SELECT C."Id" AS "CollectionId", OU."OrganizationUserId", false, false, false
|
|
FROM "Collection" AS C
|
|
INNER JOIN TempOrgUser AS OU ON C."OrganizationId" = OU."OrganizationId"
|
|
WHERE NOT EXISTS (
|
|
SELECT 1
|
|
FROM "CollectionUsers" AS CU
|
|
WHERE CU."CollectionId" = C."Id" AND CU."OrganizationUserId" = OU."OrganizationUserId"
|
|
);
|
|
|
|
-- Step 4: Update OrganizationUser to clear AccessAll flag
|
|
UPDATE "OrganizationUser" AS OU
|
|
SET "AccessAll" = false
|
|
FROM TempOrgUser AS T
|
|
WHERE OU."Id" = T."OrganizationUserId";
|
|
|
|
-- Step 5: Update "User" AccountRevisionDate for each unique OrganizationUserId
|
|
UPDATE "User" AS U
|
|
SET "AccountRevisionDate" = current_timestamp
|
|
FROM "OrganizationUser" AS OU
|
|
JOIN TempOrgUser AS TOU ON OU."Id" = TOU."OrganizationUserId"
|
|
WHERE U."Id" = OU."UserId" AND OU."Status" = 2;
|
|
|
|
-- Step 6: Drop the temporary table
|
|
DROP TABLE IF EXISTS TempOrgUser;
|