Skip to content

Commit c261e79

Browse files
Copilotsfmskywalker
andcommitted
Fix tenant filtering for workflow definitions
- Fixed SetTenantIdFilter to use ITenantAccessor instead of capturing DbContext instance - Fixed missing tenantAgnostic parameter in WorkflowDefinitionStore.FindManyAsync Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>
1 parent d0eaf10 commit c261e79

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/modules/Elsa.Persistence.EFCore.Common/EntityHandlers/SetTenantIdFilter.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq.Expressions;
22
using Elsa.Common.Entities;
3+
using Elsa.Common.Multitenancy;
34
using Microsoft.EntityFrameworkCore;
45
using Microsoft.EntityFrameworkCore.Metadata;
56

@@ -8,7 +9,7 @@ namespace Elsa.Persistence.EFCore.EntityHandlers;
89
/// <summary>
910
/// Represents a class that applies a filter to set the TenantId for entities.
1011
/// </summary>
11-
public class SetTenantIdFilter : IEntityModelCreatingHandler
12+
public class SetTenantIdFilter(ITenantAccessor? tenantAccessor) : IEntityModelCreatingHandler
1213
{
1314
/// <inheritdoc />
1415
public void Handle(ElsaDbContextBase dbContext, ModelBuilder modelBuilder, IMutableEntityType entityType)
@@ -18,26 +19,32 @@ public void Handle(ElsaDbContextBase dbContext, ModelBuilder modelBuilder, IMuta
1819

1920
modelBuilder
2021
.Entity(entityType.ClrType)
21-
.HasQueryFilter(CreateTenantFilterExpression(dbContext, entityType.ClrType));
22+
.HasQueryFilter(CreateTenantFilterExpression(entityType.ClrType));
2223
}
2324

24-
private LambdaExpression CreateTenantFilterExpression(ElsaDbContextBase dbContext, Type clrType)
25+
private LambdaExpression CreateTenantFilterExpression(Type clrType)
2526
{
2627
var parameter = Expression.Parameter(clrType, "e");
2728

28-
// e => EF.Property<string>(e, "TenantId") == this.TenantId
29+
// e => EF.Property<string>(e, "TenantId") == tenantAccessor.Tenant.Id
2930
var tenantIdProperty = Expression.Call(
3031
typeof(EF),
3132
nameof(EF.Property),
3233
[typeof(string)],
3334
parameter,
3435
Expression.Constant("TenantId"));
3536

36-
var tenantIdOnContext = Expression.Property(
37-
Expression.Constant(dbContext),
38-
nameof(ElsaDbContextBase.TenantId));
39-
40-
var body = Expression.Equal(tenantIdProperty, tenantIdOnContext);
37+
// Build an expression that accesses tenantAccessor.Tenant.Id
38+
// This will be evaluated at query time, not at model creation time
39+
var tenantAccessorConstant = Expression.Constant(tenantAccessor, typeof(ITenantAccessor));
40+
var tenantProperty = Expression.Property(tenantAccessorConstant, nameof(ITenantAccessor.Tenant));
41+
var tenantIdOnAccessor = Expression.Condition(
42+
Expression.Equal(tenantProperty, Expression.Constant(null, typeof(Tenant))),
43+
Expression.Constant(null, typeof(string)),
44+
Expression.Property(tenantProperty, nameof(Tenant.Id))
45+
);
46+
47+
var body = Expression.Equal(tenantIdProperty, tenantIdOnAccessor);
4148

4249
return Expression.Lambda(body, parameter);
4350
}

src/modules/Elsa.Persistence.EFCore/Modules/Management/WorkflowDefinitionStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task<Page<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilt
4545
/// <inheritdoc />
4646
public async Task<Page<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrder<TOrderBy> order, PageArgs pageArgs, CancellationToken cancellationToken = default)
4747
{
48-
var count = await store.QueryAsync(queryable => Filter(queryable, filter), cancellationToken).LongCount();
48+
var count = await store.QueryAsync(queryable => Filter(queryable, filter), filter.TenantAgnostic, cancellationToken).LongCount();
4949
var results = await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(order).Paginate(pageArgs), OnLoadAsync, filter.TenantAgnostic, cancellationToken).ToList();
5050
return new(results, count);
5151
}

0 commit comments

Comments
 (0)