Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Core/MultiTenancy/MultiTenancyInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,36 @@ public override ValueTask SavingChangesAsync(VaultInterceptorContext context, Ca

foreach (var operation in context.Operations)
{
if (operation.OperationType is OperationType.Add && operation.CurrentDocument is TInterface entity)
// An AddRange operation carries its batch in CurrentDocuments (its CurrentDocument is null and its
// OperationType is AddRange), so without unwrapping it the whole batch reaches the database without a
// tenant id. Single Adds are stamped the same way through their CurrentDocument.
if (operation is AddRangeOperation addRangeOperation)
{
var documentTenantId = tenantIdGetter(entity);
if (documentTenantId is null || documentTenantId.Equals(default(TTenantId)))
foreach (var document in addRangeOperation.CurrentDocuments)
{
tenantIdSetter(entity, tenantId.Value);
StampIfUnset(document);
}
}
else if (operation.OperationType is OperationType.Add && operation.CurrentDocument is TInterface entity)
{
StampIfUnset(entity);
}
}

return ValueTask.CompletedTask;

void StampIfUnset(object? document)
{
if (document is not TInterface tenantEntity)
{
return;
}

var documentTenantId = tenantIdGetter(tenantEntity);
if (documentTenantId is null || documentTenantId.Equals(default(TTenantId)))
{
tenantIdSetter(tenantEntity, tenantId.Value);
}
}
}
}