mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[AC-1682] Sqlite migrations + dotnet format
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
-- Create a temporary table to store the groups with AccessAll = 1
|
||||
-- Step 1: Create a temporary table to store the groups with AccessAll = 1
|
||||
CREATE TEMPORARY TABLE TempGroup AS
|
||||
SELECT "Id" AS "GroupId", "OrganizationId"
|
||||
FROM "Group"
|
||||
WHERE "AccessAll" = 1;
|
||||
|
||||
-- Update existing rows in "CollectionGroup"
|
||||
-- Step 2: Update existing rows in "CollectionGroup"
|
||||
UPDATE "CollectionGroups"
|
||||
SET
|
||||
"ReadOnly" = 0,
|
||||
@ -17,7 +17,7 @@ WHERE EXISTS (
|
||||
WHERE "CollectionGroups"."CollectionId" = C."Id" AND C."OrganizationId" = TG."OrganizationId"
|
||||
);
|
||||
|
||||
-- Insert new rows into "CollectionGroup"
|
||||
-- Step 3: Insert new rows into "CollectionGroup"
|
||||
INSERT INTO "CollectionGroups" ("CollectionId", "GroupId", "ReadOnly", "HidePasswords", "Manage")
|
||||
SELECT C."Id", TG."GroupId", 0, 0, 0
|
||||
FROM "Collection" C
|
||||
@ -25,5 +25,5 @@ FROM "Collection" C
|
||||
LEFT JOIN "CollectionGroups" CG ON CG."CollectionId" = C."Id" AND CG."GroupId" = TG."GroupId"
|
||||
WHERE CG."CollectionId" IS NULL;
|
||||
|
||||
-- Drop the temporary table
|
||||
-- Step 4: Drop the temporary table
|
||||
DROP TABLE IF EXISTS TempGroup;
|
||||
|
@ -1,35 +1,24 @@
|
||||
-- Step 1: Insert into a temporary table without batching
|
||||
CREATE TEMPORARY TABLE TempOrgUser AS
|
||||
SELECT Id AS OrganizationUserId, OrganizationId
|
||||
FROM OrganizationUser
|
||||
WHERE AccessAll = 1;
|
||||
|
||||
-- Step 2: Process all records at once
|
||||
-- Update existing rows in CollectionUser
|
||||
UPDATE CollectionUsers
|
||||
-- Update existing rows in CollectionUsers
|
||||
UPDATE "CollectionUsers"
|
||||
SET
|
||||
ReadOnly = 0,
|
||||
HidePasswords = 0,
|
||||
Manage = 0
|
||||
FROM CollectionUsers AS target
|
||||
INNER JOIN (
|
||||
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
||||
FROM Collection C
|
||||
INNER JOIN TempOrgUser T ON C.OrganizationId = T.OrganizationId
|
||||
) AS source
|
||||
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId;
|
||||
"ReadOnly" = 0,
|
||||
"HidePasswords" = 0,
|
||||
"Manage" = 0
|
||||
WHERE "CollectionId" IN (
|
||||
SELECT "C"."Id"
|
||||
FROM "Collection" "C"
|
||||
INNER JOIN "OrganizationUser" "OU" ON "C"."OrganizationId" = "OU"."OrganizationId"
|
||||
WHERE "OU"."AccessAll" = 1
|
||||
);
|
||||
|
||||
-- Insert new rows into CollectionUser
|
||||
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
||||
SELECT source.CollectionId, source.OrganizationUserId, 0, 0, 0
|
||||
FROM (
|
||||
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
||||
FROM Collection C
|
||||
INNER JOIN TempOrgUser T ON C.OrganizationId = T.OrganizationId
|
||||
) AS source
|
||||
LEFT JOIN CollectionUsers AS target
|
||||
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId
|
||||
WHERE target.CollectionId IS NULL;
|
||||
|
||||
-- Step 3: Drop the temporary table
|
||||
DROP TABLE TempOrgUser;
|
||||
-- Insert new rows into CollectionUsers
|
||||
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||
SELECT "C"."Id" AS "CollectionId", "OU"."Id" AS "OrganizationUserId", 0, 0, 0
|
||||
FROM "Collection" "C"
|
||||
INNER JOIN "OrganizationUser" "OU" ON "C"."OrganizationId" = "OU"."OrganizationId"
|
||||
WHERE "OU"."AccessAll" = 1
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM "CollectionUsers" "CU"
|
||||
WHERE "CU"."CollectionId" = "C"."Id" AND "CU"."OrganizationUserId" = "OU"."Id"
|
||||
);
|
||||
|
@ -1,12 +1,28 @@
|
||||
-- Update "CollectionUser" with "Manage" = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||
UPDATE "CollectionUsers"
|
||||
SET "ReadOnly" = 0,
|
||||
-- Update `CollectionUser` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||
UPDATE "CollectionUsers" AS cu
|
||||
SET
|
||||
"ReadOnly" = 0,
|
||||
"HidePasswords" = 0,
|
||||
"Manage" = 1
|
||||
WHERE "OrganizationUserId" IN (
|
||||
SELECT cu."OrganizationUserId"
|
||||
FROM "CollectionUsers" cu
|
||||
INNER JOIN "OrganizationUser" ou ON cu."OrganizationUserId" = ou."Id"
|
||||
WHERE ou."Type" = 3 OR (ou."Permissions" IS NOT NULL AND
|
||||
JSON_VALID(ou."Permissions") AND json_extract(ou."Permissions", '$.editAssignedCollections') = 'true')
|
||||
SELECT ou."Id"
|
||||
FROM "OrganizationUser" AS ou
|
||||
WHERE ou."Type" = 3 OR (
|
||||
ou."Permissions" IS NOT NULL AND
|
||||
JSON_VALID(ou."Permissions") > 0 AND
|
||||
JSON_EXTRACT(ou."Permissions", '$.editAssignedCollections') = 'true'
|
||||
)
|
||||
);
|
||||
|
||||
-- Insert rows to CollectionUser for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
||||
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||
SELECT cg."CollectionId", ou."Id", 0, 0, 1
|
||||
FROM "CollectionGroups" AS cg
|
||||
INNER JOIN "GroupUser" AS gu ON cg."GroupId" = gu."GroupId"
|
||||
INNER JOIN "OrganizationUser" AS ou ON gu."OrganizationUserId" = ou."Id"
|
||||
WHERE (ou."Type" = 3 OR
|
||||
(ou."Permissions" IS NOT NULL AND JSON_VALID(ou."Permissions") > 0 AND JSON_EXTRACT(ou."Permissions", '$.editAssignedCollections')) = 'true')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM "CollectionUsers" AS cu
|
||||
WHERE cu."CollectionId" = cg."CollectionId" AND cu."OrganizationUserId" = ou."Id"
|
||||
);
|
||||
|
Reference in New Issue
Block a user