mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[AC-1682] Updated mysql migrations
This commit is contained in:
@ -36,30 +36,11 @@ INNER JOIN TempGroup TG ON G.`Id` = TG.`GroupId`
|
|||||||
SET G.`AccessAll` = 0;
|
SET G.`AccessAll` = 0;
|
||||||
|
|
||||||
-- Step 6: Update User AccountRevisionDate for each unique OrganizationUserId
|
-- Step 6: Update User AccountRevisionDate for each unique OrganizationUserId
|
||||||
DECLARE Step1OrganizationUserId UUID;
|
UPDATE `User` U
|
||||||
|
INNER JOIN `OrganizationUser` OU ON OU.`UserId` = U.`Id`
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
INNER JOIN TempOrganizationUsers TOU ON TOU.`OrganizationUserId` = OU.`Id`
|
||||||
SELECT `OrganizationUserId`
|
SET U.`AccountRevisionDate` = UTC_TIMESTAMP()
|
||||||
FROM TempOrganizationUsers;
|
WHERE OU.`Status` = 2;
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor;
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO Step1OrganizationUserId;
|
|
||||||
|
|
||||||
WHILE (FETCH_STATUS = 0) DO
|
|
||||||
-- Update User AccountRevisionDate for the current OrganizationUserId
|
|
||||||
UPDATE `User` U
|
|
||||||
SET U.`AccountRevisionDate` = UTC_TIMESTAMP()
|
|
||||||
FROM `User` U
|
|
||||||
INNER JOIN `OrganizationUser` OU ON OU.`UserId` = U.`Id`
|
|
||||||
WHERE OU.`Id` = Step1OrganizationUserId
|
|
||||||
AND OU.`Status` = 2;
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO Step1OrganizationUserId;
|
|
||||||
END WHILE;
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor;
|
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
|
||||||
|
|
||||||
-- Step 7: Drop the temporary tables
|
-- Step 7: Drop the temporary tables
|
||||||
DROP TEMPORARY TABLE IF EXISTS TempGroup;
|
DROP TEMPORARY TABLE IF EXISTS TempGroup;
|
||||||
|
@ -1,21 +1,40 @@
|
|||||||
-- Update existing rows in CollectionUsers
|
-- Step 1: Create a temporary table
|
||||||
UPDATE CollectionUsers AS target
|
CREATE TEMPORARY TABLE TempOrgUser AS
|
||||||
INNER JOIN Collection AS C ON target.CollectionId = C.Id
|
SELECT `Id` AS `OrganizationUserId`, `OrganizationId`
|
||||||
INNER JOIN OrganizationUser AS OU ON C.OrganizationId = OU.OrganizationId
|
FROM `OrganizationUser`
|
||||||
SET
|
WHERE `AccessAll` = 1;
|
||||||
target.ReadOnly = 0,
|
|
||||||
target.HidePasswords = 0,
|
|
||||||
target.Manage = 0
|
|
||||||
WHERE OU.AccessAll = 1;
|
|
||||||
|
|
||||||
-- Insert new rows into CollectionUsers
|
-- Step 2: Update existing rows in CollectionUsers
|
||||||
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
UPDATE `CollectionUsers` AS `target`
|
||||||
SELECT C.Id AS CollectionId, OU.Id AS OrganizationUserId, 0, 0, 0
|
INNER JOIN `Collection` AS `C` ON `target`.`CollectionId` = `C`.`Id`
|
||||||
FROM Collection AS C
|
INNER JOIN `TempOrgUser` AS `OU` ON `C`.`OrganizationId` = `OU`.`OrganizationId`
|
||||||
INNER JOIN OrganizationUser AS OU ON C.OrganizationId = OU.OrganizationId
|
SET
|
||||||
WHERE OU.AccessAll = 1
|
`target`.`ReadOnly` = 0,
|
||||||
AND NOT EXISTS (
|
`target`.`HidePasswords` = 0,
|
||||||
|
`target`.`Manage` = 0;
|
||||||
|
|
||||||
|
-- Step 3: Insert new rows into CollectionUsers
|
||||||
|
INSERT INTO `CollectionUsers` (`CollectionId`, `OrganizationUserId`, `ReadOnly`, `HidePasswords`, `Manage`)
|
||||||
|
SELECT `C`.`Id` AS `CollectionId`, `OU`.`OrganizationUserId`, 0, 0, 0
|
||||||
|
FROM `Collection` AS `C`
|
||||||
|
INNER JOIN `TempOrgUser` AS `OU` ON `C`.`OrganizationId` = `OU`.`OrganizationId`
|
||||||
|
WHERE NOT EXISTS (
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM CollectionUsers AS CU
|
FROM `CollectionUsers` AS `CU`
|
||||||
WHERE CU.CollectionId = C.Id AND CU.OrganizationUserId = OU.Id
|
WHERE `CU`.`CollectionId` = `C`.`Id` AND `CU`.`OrganizationUserId` = `OU`.`OrganizationUserId`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- Step 4: Update OrganizationUser to clear AccessAll flag
|
||||||
|
UPDATE `OrganizationUser` AS `OU`
|
||||||
|
INNER JOIN `TempOrgUser` AS `T` ON `OU`.`Id` = `T`.`OrganizationUserId`
|
||||||
|
SET `OU`.`AccessAll` = 0;
|
||||||
|
|
||||||
|
-- Step 5: Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE `User` AS `U`
|
||||||
|
INNER JOIN `OrganizationUser` AS `OU` ON `OU`.`UserId` = `U`.`Id`
|
||||||
|
INNER JOIN `TempOrgUser` AS `TOU` ON `TOU`.`OrganizationUserId` = `OU`.`Id`
|
||||||
|
SET `U`.`AccountRevisionDate` = UTC_TIMESTAMP()
|
||||||
|
WHERE `OU`.`Status` = 2;
|
||||||
|
|
||||||
|
-- Step 6: Drop the temporary table
|
||||||
|
DROP TEMPORARY TABLE IF EXISTS TempOrgUser;
|
||||||
|
@ -1,20 +1,74 @@
|
|||||||
-- Update `CollectionUser` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
-- Step 1: Update `CollectionUser` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||||
UPDATE CollectionUsers cu
|
-- Create a temporary table
|
||||||
INNER JOIN OrganizationUser ou ON cu.OrganizationUserId = ou.Id
|
CREATE TEMPORARY TABLE TempOrgUser AS
|
||||||
SET cu.ReadOnly = 0,
|
SELECT ou.Id AS OrganizationUserId
|
||||||
cu.HidePasswords = 0,
|
FROM OrganizationUser ou
|
||||||
cu.Manage = 1
|
WHERE ou.Type = 3 OR (ou.Permissions IS NOT NULL AND
|
||||||
WHERE ou.Type = 3 OR (ou.Permissions IS NOT NULL AND
|
JSON_VALID(ou.Permissions) > 0 AND JSON_UNQUOTE(JSON_EXTRACT(ou.Permissions, '$.editAssignedCollections')) = 'true');
|
||||||
JSON_VALID(ou.Permissions) > 0 AND JSON_UNQUOTE(JSON_EXTRACT(ou.Permissions, '$.editAssignedCollections')) = 'true');
|
|
||||||
|
|
||||||
-- Insert rows to CollectionUser for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
-- Update CollectionUsers with Manage = 1 using the temporary table
|
||||||
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
UPDATE CollectionUsers cu
|
||||||
SELECT cg.CollectionId, ou.Id, 0, 0, 1
|
INNER JOIN TempOrgUser temp ON cu.OrganizationUserId = temp.OrganizationUserId
|
||||||
FROM CollectionGroups cg
|
SET cu.ReadOnly = 0,
|
||||||
INNER JOIN GroupUser gu ON cg.GroupId = gu.GroupId
|
cu.HidePasswords = 0,
|
||||||
INNER JOIN OrganizationUser ou ON gu.OrganizationUserId = ou.Id
|
cu.Manage = 1;
|
||||||
WHERE (ou.Type = 3 OR
|
|
||||||
(ou.Permissions IS NOT NULL AND JSON_VALID(ou.Permissions) > 0 AND JSON_UNQUOTE(JSON_EXTRACT(ou.Permissions, '$.editAssignedCollections')) = 'true'))
|
-- Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
AND NOT EXISTS (
|
UPDATE `User` AS `U`
|
||||||
SELECT 1 FROM CollectionUsers cu
|
INNER JOIN `OrganizationUser` AS `OU` ON `OU`.`UserId` = `U`.`Id`
|
||||||
WHERE cu.CollectionId = cg.CollectionId AND cu.OrganizationUserId = ou.Id);
|
INNER JOIN `TempOrgUser` AS `TOU` ON `TOU`.`OrganizationUserId` = `OU`.`Id`
|
||||||
|
SET `U`.`AccountRevisionDate` = UTC_TIMESTAMP()
|
||||||
|
WHERE `OU`.`Status` = 2;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TEMPORARY TABLE IF EXISTS TempOrgUser;
|
||||||
|
|
||||||
|
-- Step 2: Insert rows to CollectionUser for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
||||||
|
-- Store the results in a temporary table
|
||||||
|
CREATE TEMPORARY TABLE IF NOT EXISTS TempCol AS
|
||||||
|
SELECT cg.CollectionId, ou.Id AS OrganizationUserId
|
||||||
|
FROM CollectionGroups cg
|
||||||
|
INNER JOIN GroupUser gu ON cg.GroupId = gu.GroupId
|
||||||
|
INNER JOIN OrganizationUser ou ON gu.OrganizationUserId = ou.Id
|
||||||
|
WHERE (ou.Type = 3 OR (ou.Permissions IS NOT NULL AND JSON_VALID(ou.Permissions) > 0 AND JSON_UNQUOTE(JSON_EXTRACT(ou.Permissions, '$.editAssignedCollections')) = 'true'))
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM CollectionUsers cu
|
||||||
|
WHERE cu.CollectionId = cg.CollectionId AND cu.OrganizationUserId = ou.Id
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Insert rows into CollectionUsers using the temporary table
|
||||||
|
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
||||||
|
SELECT CollectionId, OrganizationUserId, 0, 0, 1
|
||||||
|
FROM TempCol;
|
||||||
|
|
||||||
|
-- Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE `User` AS `U`
|
||||||
|
INNER JOIN `OrganizationUser` AS `OU` ON `OU`.`UserId` = `U`.`Id`
|
||||||
|
INNER JOIN `TempCol` AS `TC` ON `TC`.`OrganizationUserId` = `OU`.`Id`
|
||||||
|
SET `U`.`AccountRevisionDate` = UTC_TIMESTAMP()
|
||||||
|
WHERE `OU`.`Status` = 2;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TEMPORARY TABLE IF EXISTS TempCol;
|
||||||
|
|
||||||
|
-- Step 3: Set all Managers to Users
|
||||||
|
-- Create a temporary table
|
||||||
|
CREATE TEMPORARY TABLE TempOrgUser AS
|
||||||
|
SELECT ou.Id AS OrganizationUserId
|
||||||
|
FROM OrganizationUser ou
|
||||||
|
WHERE ou.Type = 3;
|
||||||
|
|
||||||
|
-- Update OrganizationUser with Type = 2 using the temporary table
|
||||||
|
UPDATE OrganizationUser ou
|
||||||
|
INNER JOIN TempOrgUser temp ON ou.Id = temp.OrganizationUserId
|
||||||
|
SET ou.Type = 2;
|
||||||
|
|
||||||
|
-- Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE `User` AS `U`
|
||||||
|
INNER JOIN `OrganizationUser` AS `OU` ON `OU`.`UserId` = `U`.`Id`
|
||||||
|
INNER JOIN `TempOrgUser` AS `TOU` ON `TOU`.`OrganizationUserId` = `OU`.`Id`
|
||||||
|
SET `U`.`AccountRevisionDate` = UTC_TIMESTAMP()
|
||||||
|
WHERE `OU`.`Status` = 2;
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TEMPORARY TABLE IF EXISTS TempOrgUser;
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
-- Set `FlexibleCollections` = 1 for all organizations that have not yet been migrated.
|
||||||
|
UPDATE `Organization`
|
||||||
|
SET `FlexibleCollections` = 1
|
||||||
|
WHERE `FlexibleCollections` = 0;
|
||||||
|
Reference in New Issue
Block a user