1
0
mirror of https://github.com/bitwarden/server synced 2026-02-14 15:33:35 +00:00

Use TwoGuidIdArray

This commit is contained in:
Thomas Rittson
2026-01-03 13:41:44 +10:00
parent aa1c0a4a77
commit 2b7caf1e37
4 changed files with 39 additions and 53 deletions

View File

@@ -160,6 +160,21 @@ public static class DapperHelpers
return ids.ToArrayTVP("GuidId");
}
public static DataTable ToTwoGuidIdArrayTVP(this IEnumerable<(Guid id1, Guid id2)> values)
{
var table = new DataTable();
table.SetTypeName("[dbo].[TwoGuidIdArray]");
table.Columns.Add("Id1", typeof(Guid));
table.Columns.Add("Id2", typeof(Guid));
foreach (var value in values)
{
table.Rows.Add(value.id1, value.id2);
}
return table;
}
public static DataTable ToArrayTVP<T>(this IEnumerable<T> values, string columnName)
{
var table = new DataTable();

View File

@@ -7,6 +7,7 @@ using Bit.Core.Entities;
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Bit.Infrastructure.Dapper.AdminConsole.Helpers;
using Dapper;
using Microsoft.Data.SqlClient;
@@ -368,6 +369,10 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
return;
}
var organizationUserCollectionIds = organizationUserIds
.Select(ou => (ou, CoreHelpers.GenerateComb()))
.ToTwoGuidIdArrayTVP();
await using var connection = new SqlConnection(ConnectionString);
await connection.OpenAsync();
@@ -379,7 +384,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
{
OrganizationId = organizationId,
DefaultCollectionName = defaultCollectionName,
OrganizationUserIds = organizationUserIds.ToGuidIdArrayTVP()
OrganizationUserCollectionIds = organizationUserCollectionIds
},
commandType: CommandType.StoredProcedure);
}

View File

@@ -3,31 +3,13 @@
CREATE PROCEDURE [dbo].[Collection_CreateDefaultCollections]
@OrganizationId UNIQUEIDENTIFIER,
@DefaultCollectionName VARCHAR(MAX),
@OrganizationUserIds AS [dbo].[GuidIdArray] READONLY
@OrganizationUserCollectionIds AS [dbo].[TwoGuidIdArray] READONLY -- OrganizationUserId, CollectionId
AS
BEGIN
SET NOCOUNT ON
DECLARE @Now DATETIME2(7) = GETUTCDATE()
-- Create temporary table to allocate collection IDs to each organizationUser
DECLARE @CollectionsToInsert TABLE
(
[OrganizationUserId] UNIQUEIDENTIFIER,
[CollectionId] UNIQUEIDENTIFIER
);
INSERT INTO @CollectionsToInsert
(
[OrganizationUserId],
[CollectionId]
)
SELECT
ou.Id,
NEWID()
FROM
@OrganizationUserIds ou
BEGIN TRANSACTION;
BEGIN TRY
@@ -39,10 +21,10 @@ BEGIN
[CreationDate]
)
SELECT
ou.[OrganizationUserId],
ids.[Id1], -- OrganizationUserId
@Now
FROM
@CollectionsToInsert ou;
@OrganizationUserCollectionIds ids;
-- Insert collections for users who obtained semaphore entries
INSERT INTO [dbo].[Collection]
@@ -57,7 +39,7 @@ BEGIN
[DefaultUserCollectionEmail]
)
SELECT
ou.[CollectionId],
ids.[Id2], -- CollectionId
@OrganizationId,
@DefaultCollectionName,
@Now,
@@ -66,7 +48,7 @@ BEGIN
NULL,
NULL
FROM
@CollectionsToInsert ou;
@OrganizationUserCollectionIds ids;
-- Insert collection user mappings
INSERT INTO [dbo].[CollectionUser]
@@ -78,13 +60,13 @@ BEGIN
[Manage]
)
SELECT
ou.[CollectionId],
ou.[OrganizationUserId],
ids.[Id2], -- CollectionId
ids.[Id1], -- OrganizationUserId
0, -- ReadOnly = false
0, -- HidePasswords = false
1 -- Manage = true
FROM
@CollectionsToInsert ou;
@OrganizationUserCollectionIds ids;
COMMIT TRANSACTION;
END TRY

View File

@@ -1,31 +1,15 @@
-- Creates default user collections for organization users
-- Uses semaphore table to prevent duplicate default collections at database level
CREATE OR ALTER PROCEDURE [dbo].[Collection_CreateDefaultCollections]
@OrganizationId UNIQUEIDENTIFIER,
@DefaultCollectionName VARCHAR(MAX),
@OrganizationUserIds AS [dbo].[GuidIdArray] READONLY
@OrganizationUserCollectionIds AS [dbo].[TwoGuidIdArray] READONLY -- OrganizationUserId, CollectionId
AS
BEGIN
SET NOCOUNT ON
DECLARE @Now DATETIME2(7) = GETUTCDATE()
-- Create temporary table to allocate collection IDs to each organizationUser
DECLARE @CollectionsToInsert TABLE
(
[OrganizationUserId] UNIQUEIDENTIFIER,
[CollectionId] UNIQUEIDENTIFIER
);
INSERT INTO @CollectionsToInsert
(
[OrganizationUserId],
[CollectionId]
)
SELECT
ou.Id,
NEWID()
FROM
@OrganizationUserIds ou
BEGIN TRANSACTION;
BEGIN TRY
@@ -37,10 +21,10 @@ BEGIN
[CreationDate]
)
SELECT
ou.[OrganizationUserId],
ids.[Id1], -- OrganizationUserId
@Now
FROM
@CollectionsToInsert ou;
@OrganizationUserCollectionIds ids;
-- Insert collections for users who obtained semaphore entries
INSERT INTO [dbo].[Collection]
@@ -55,7 +39,7 @@ BEGIN
[DefaultUserCollectionEmail]
)
SELECT
ou.[CollectionId],
ids.[Id2], -- CollectionId
@OrganizationId,
@DefaultCollectionName,
@Now,
@@ -64,7 +48,7 @@ BEGIN
NULL,
NULL
FROM
@CollectionsToInsert ou;
@OrganizationUserCollectionIds ids;
-- Insert collection user mappings
INSERT INTO [dbo].[CollectionUser]
@@ -76,13 +60,13 @@ BEGIN
[Manage]
)
SELECT
ou.[CollectionId],
ou.[OrganizationUserId],
ids.[Id2], -- CollectionId
ids.[Id1], -- OrganizationUserId
0, -- ReadOnly = false
0, -- HidePasswords = false
1 -- Manage = true
FROM
@CollectionsToInsert ou;
@OrganizationUserCollectionIds ids;
COMMIT TRANSACTION;
END TRY