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
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ public async Task<long> DeleteWhereAsync(Expression<Func<TDocument, bool>> predi
/// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteWhereAsync(Expression<Func<TDocument, bool>> predicate, string key, bool tenantAgnostic = false, CancellationToken cancellationToken = default)
{
var queryable = GetQueryableCollection(tenantAgnostic);
// Strict tenant scoping on delete: never match shared "*" entities under a concrete tenant.
var queryable = GetQueryableCollection(tenantAgnostic, includeTenantAgnostic: false);
var documentsToDelete = await queryable.Where(predicate).ToListAsync(cancellationToken);
var count = documentsToDelete.LongCount();
var filter = documentsToDelete.BuildIdFilterForList(key);
Expand Down Expand Up @@ -417,7 +418,8 @@ public async Task<long> DeleteWhereAsync(Func<IQueryable<TDocument>, IQueryable<
/// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteWhereAsync(Func<IQueryable<TDocument>, IQueryable<TDocument>> query, string key = nameof(Entity.Id), bool tenantAgnostic = false, CancellationToken cancellationToken = default)
{
var queryable = GetQueryableCollection(tenantAgnostic);
// Strict tenant scoping on delete: never match shared "*" entities under a concrete tenant.
var queryable = GetQueryableCollection(tenantAgnostic, includeTenantAgnostic: false);
var documentsToDelete = await query(queryable).ToListAsync(cancellationToken);
var count = documentsToDelete.LongCount();
var filter = documentsToDelete.BuildIdFilterForList(key);
Expand All @@ -426,7 +428,7 @@ public async Task<long> DeleteWhereAsync(Func<IQueryable<TDocument>, IQueryable<
return count;
}

private IQueryable<TDocument> GetQueryableCollection(bool tenantAgnostic = false)
private IQueryable<TDocument> GetQueryableCollection(bool tenantAgnostic = false, bool includeTenantAgnostic = true)
{
var queryable = collection.AsQueryable();

Expand All @@ -437,7 +439,13 @@ private IQueryable<TDocument> GetQueryableCollection(bool tenantAgnostic = false
{
var tenant = tenantAccessor.Tenant;
var tenantId = tenant?.Id.EmptyToNull();
queryable = queryable.Where(x => (x as Entity)!.TenantId == tenantId);
// Reads include tenant-agnostic ("*") rows so global entities (e.g. CLR workflow
// definitions) remain visible under a specific tenant, matching the EFCore provider.
// Deletes pass includeTenantAgnostic: false so a tenant-scoped delete cannot remove a
// shared "*" entity (the Dapper store keeps tenant-scoped deletes strict).
queryable = includeTenantAgnostic
? queryable.Where(x => (x as Entity)!.TenantId == tenantId || (x as Entity)!.TenantId == Tenant.AgnosticTenantId)
: queryable.Where(x => (x as Entity)!.TenantId == tenantId);
}

return queryable;
Expand All @@ -448,7 +456,8 @@ private void ApplyTenantId(TDocument document)
var tenant = tenantAccessor.Tenant;
var tenantId = tenant?.Id;

if (document is Entity tenantDocument)
// Don't overwrite tenant-agnostic ("*") entities; only stamp tenant-specific ones.
if (document is Entity tenantDocument && tenantDocument.TenantId != Tenant.AgnosticTenantId)
tenantDocument.TenantId = tenantId.EmptyToNull();
}

Expand All @@ -459,7 +468,8 @@ private void ApplyTenantId(IEnumerable<TDocument> documents)

foreach (var document in documents)
{
if (document is Entity tenantDocument)
// Don't overwrite tenant-agnostic ("*") entities; only stamp tenant-specific ones.
if (document is Entity tenantDocument && tenantDocument.TenantId != Tenant.AgnosticTenantId)
tenantDocument.TenantId = tenantId.EmptyToNull();
}
}
Expand Down
Loading