1
0
mirror of https://github.com/bitwarden/server synced 2026-02-19 10:53:34 +00:00

Move filtering out of db layer

This commit is contained in:
Thomas Rittson
2026-01-01 13:51:10 +10:00
parent a6fcee7094
commit 21d7276625
6 changed files with 196 additions and 51 deletions

View File

@@ -383,7 +383,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
commandType: CommandType.StoredProcedure);
}
public async Task UpsertDefaultCollectionsBulkAsync(Guid organizationId, IEnumerable<Guid> organizationUserIds, string defaultCollectionName)
public async Task CreateDefaultCollectionsBulkAsync(Guid organizationId, IEnumerable<Guid> organizationUserIds, string defaultCollectionName)
{
organizationUserIds = organizationUserIds.ToList();
if (!organizationUserIds.Any())
@@ -391,8 +391,7 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
return;
}
var orgUserIdWithDefaultCollection = await GetDefaultCollectionSemaphoresAsync(organizationUserIds);
var missingDefaultCollectionUserIds = organizationUserIds.Except(orgUserIdWithDefaultCollection);
var (collectionUsers, collections) = BuildDefaultCollectionForUsers(organizationId, organizationUserIds, defaultCollectionName);
await using var connection = new SqlConnection(ConnectionString);
connection.Open();
@@ -400,15 +399,9 @@ public class CollectionRepository : Repository<Collection, Guid>, ICollectionRep
try
{
var (collectionUsers, collections) = BuildDefaultCollectionForUsers(organizationId, missingDefaultCollectionUserIds, defaultCollectionName);
if (!collectionUsers.Any() || !collections.Any())
{
return;
}
// CRITICAL: Insert semaphore entries BEFORE collections
// TODO: this will result in a creation date of the semaphore AFTER that of the collection, which is weird
// Database will throw on duplicate primary key (OrganizationUserId)
var now = DateTime.UtcNow;
var semaphores = collectionUsers.Select(c => new DefaultCollectionSemaphore
{