1
0
mirror of https://github.com/bitwarden/server synced 2026-02-20 19:33:32 +00:00

Move semaphore into helper method for consistent creation date

This commit is contained in:
Thomas Rittson
2026-01-01 15:21:29 +10:00
parent ebbdc946a1
commit 897719e695
3 changed files with 21 additions and 23 deletions

View File

@@ -7,16 +7,21 @@ namespace Bit.Core.AdminConsole.OrganizationFeatures.Collections;
public static class CollectionUtils
{
/// <summary>
/// Arranges Collection and CollectionUser objects to create default user collections.
/// Arranges semaphore, Collection and CollectionUser objects to create default user collections.
/// </summary>
/// <param name="organizationId">The organization ID.</param>
/// <param name="organizationUserIds">The IDs for organization users who need default collections.</param>
/// <param name="defaultCollectionName">The encrypted string to use as the default collection name.</param>
/// <returns></returns>
public static (IEnumerable<Collection> collection, IEnumerable<CollectionUser> collectionUsers)
public static (IEnumerable<DefaultCollectionSemaphore> semaphores,
IEnumerable<Collection> collection,
IEnumerable<CollectionUser> collectionUsers)
BuildDefaultUserCollections(Guid organizationId, IEnumerable<Guid> organizationUserIds,
string defaultCollectionName)
{
var now = DateTime.UtcNow;
var semaphores = new List<DefaultCollectionSemaphore>();
var collectionUsers = new List<CollectionUser>();
var collections = new List<Collection>();
@@ -24,13 +29,19 @@ public static class CollectionUtils
{
var collectionId = CoreHelpers.GenerateComb();
semaphores.Add(new DefaultCollectionSemaphore
{
OrganizationUserId = orgUserId,
CreationDate = now
});
collections.Add(new Collection
{
Id = collectionId,
OrganizationId = organizationId,
Name = defaultCollectionName,
CreationDate = DateTime.UtcNow,
RevisionDate = DateTime.UtcNow,
CreationDate = now,
RevisionDate = now,
Type = CollectionType.DefaultUserCollection,
DefaultUserCollectionEmail = null
@@ -46,6 +57,6 @@ public static class CollectionUtils
});
}
return (collections, collectionUsers);
return (semaphores, collections, collectionUsers);
}
}

View File

@@ -398,7 +398,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
return;
}
var (collections, collectionUsers) =
var (semaphores, collections, collectionUsers) =
CollectionUtils.BuildDefaultUserCollections(organizationId, organizationUserIds, defaultCollectionName);
await using var connection = new SqlConnection(ConnectionString);
@@ -410,13 +410,6 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
// CRITICAL: Insert semaphore entries BEFORE collections
// Database will throw on duplicate primary key (OrganizationUserId)
var now = DateTime.UtcNow;
var semaphores = collectionUsers.Select(c => new DefaultCollectionSemaphore
{
OrganizationUserId = c.OrganizationUserId,
CreationDate = now
}).ToList();
await BulkInsertDefaultCollectionSemaphoresAsync(connection, transaction, semaphores);
await BulkResourceCreationService.CreateCollectionsAsync(connection, transaction, collections);
await BulkResourceCreationService.CreateCollectionsUsersAsync(connection, transaction, collectionUsers);
@@ -447,8 +440,9 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
return results.ToHashSet();
}
private async Task BulkInsertDefaultCollectionSemaphoresAsync(SqlConnection connection, SqlTransaction transaction, List<DefaultCollectionSemaphore> semaphores)
private async Task BulkInsertDefaultCollectionSemaphoresAsync(SqlConnection connection, SqlTransaction transaction, IEnumerable<DefaultCollectionSemaphore> semaphores)
{
semaphores = semaphores.ToList();
if (!semaphores.Any())
{
return;

View File

@@ -805,7 +805,7 @@ public class CollectionRepository : Repository<Core.Entities.Collection, Collect
return;
}
var (collections, collectionUsers) =
var (semaphores, collections, collectionUsers) =
CollectionUtils.BuildDefaultUserCollections(organizationId, organizationUserIds, defaultCollectionName);
using var scope = ServiceScopeFactory.CreateScope();
@@ -815,14 +815,7 @@ public class CollectionRepository : Repository<Core.Entities.Collection, Collect
{
// CRITICAL: Insert semaphore entries BEFORE collections
// Database will throw on duplicate primary key (OrganizationUserId)
var now = DateTime.UtcNow;
var semaphores = collectionUsers.Select(c => new DefaultCollectionSemaphore
{
OrganizationUserId = c.OrganizationUserId,
CreationDate = now
}).ToList();
await dbContext.BulkCopyAsync(semaphores);
await dbContext.BulkCopyAsync(Mapper.Map<IEnumerable<DefaultCollectionSemaphore>>(semaphores));
await dbContext.BulkCopyAsync(Mapper.Map<IEnumerable<Collection>>(collections));
await dbContext.BulkCopyAsync(Mapper.Map<IEnumerable<CollectionUser>>(collectionUsers));