| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Tenants.Options; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.EntityFrameworkCore.Metadata; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Persistence.EFCore.EntityHandlers; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a class that applies a filter to set the TenantId for entities. |
| | | 13 | | /// </summary> |
| | | 14 | | public class SetTenantIdFilter : IEntityModelCreatingHandler |
| | | 15 | | { |
| | 48 | 16 | | public SetTenantIdFilter(IOptions<TenantsOptions> tenantsOptions) => ArgumentNullException.ThrowIfNull(tenantsOption |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// The filter is always part of the model. Whether it is active is read from the current context so |
| | | 21 | | /// EF Core model caching cannot retain the tenant setting of the first context that built the model. |
| | | 22 | | /// </remarks> |
| | | 23 | | public void Handle(ElsaDbContextBase dbContext, ModelBuilder modelBuilder, IMutableEntityType entityType) |
| | | 24 | | { |
| | 186 | 25 | | if (!typeof(Entity).IsAssignableFrom(entityType.ClrType)) |
| | 96 | 26 | | return; |
| | | 27 | | |
| | 90 | 28 | | var tenantFilter = CreateTenantFilterExpression(dbContext, entityType.ClrType); |
| | 90 | 29 | | var existingFilter = entityType.GetQueryFilter(); |
| | 90 | 30 | | if (existingFilter is not null) |
| | | 31 | | { |
| | 75 | 32 | | var parameter = tenantFilter.Parameters[0]; |
| | 75 | 33 | | var existingBody = new ParameterReplacementVisitor(existingFilter.Parameters[0], parameter).Visit(existingFi |
| | 75 | 34 | | tenantFilter = Expression.Lambda(Expression.AndAlso(existingBody, tenantFilter.Body), parameter); |
| | | 35 | | } |
| | | 36 | | |
| | 90 | 37 | | modelBuilder.Entity(entityType.ClrType).HasQueryFilter(tenantFilter); |
| | 90 | 38 | | } |
| | | 39 | | |
| | | 40 | | private LambdaExpression CreateTenantFilterExpression(ElsaDbContextBase dbContext, Type clrType) |
| | | 41 | | { |
| | 90 | 42 | | var parameter = Expression.Parameter(clrType, "e"); |
| | | 43 | | |
| | | 44 | | // e => EF.Property<string>(e, "TenantId") == this.TenantId || EF.Property<string>(e, "TenantId") == "*" || (EF. |
| | 90 | 45 | | var tenantIdProperty = Expression.Call( |
| | 90 | 46 | | typeof(EF), |
| | 90 | 47 | | nameof(EF.Property), |
| | 90 | 48 | | [typeof(string)], |
| | 90 | 49 | | parameter, |
| | 90 | 50 | | Expression.Constant("TenantId")); |
| | | 51 | | |
| | 90 | 52 | | var tenantIdOnContext = Expression.Property( |
| | 90 | 53 | | Expression.Constant(dbContext), |
| | 90 | 54 | | nameof(ElsaDbContextBase.TenantId)); |
| | | 55 | | |
| | 90 | 56 | | var tenantFilteringEnabled = Expression.Property( |
| | 90 | 57 | | Expression.Constant(dbContext), |
| | 90 | 58 | | nameof(ElsaDbContextBase.IsTenantFilteringEnabled)); |
| | | 59 | | |
| | 90 | 60 | | var equalityCheck = Expression.Equal(tenantIdProperty, tenantIdOnContext); |
| | 90 | 61 | | var agnosticCheck = Expression.Equal(tenantIdProperty, Expression.Constant(Tenant.AgnosticTenantId, typeof(strin |
| | | 62 | | |
| | | 63 | | // For backwards compatibility: include records with null TenantId when context TenantId is empty string |
| | 90 | 64 | | var nullTenantCheck = Expression.Equal(tenantIdProperty, Expression.Constant(null, typeof(string))); |
| | 90 | 65 | | var emptyContextCheck = Expression.Equal(tenantIdOnContext, Expression.Constant(string.Empty, typeof(string))); |
| | 90 | 66 | | var backwardsCompatibilityCheck = Expression.AndAlso(nullTenantCheck, emptyContextCheck); |
| | | 67 | | |
| | 90 | 68 | | var tenantVisibilityCheck = Expression.OrElse( |
| | 90 | 69 | | Expression.OrElse(equalityCheck, agnosticCheck), |
| | 90 | 70 | | backwardsCompatibilityCheck); |
| | 90 | 71 | | var body = Expression.OrElse(Expression.Not(tenantFilteringEnabled), tenantVisibilityCheck); |
| | | 72 | | |
| | 90 | 73 | | return Expression.Lambda(body, parameter); |
| | | 74 | | } |
| | | 75 | | |
| | 75 | 76 | | private sealed class ParameterReplacementVisitor(ParameterExpression source, ParameterExpression replacement) : Expr |
| | | 77 | | { |
| | 675 | 78 | | protected override Expression VisitParameter(ParameterExpression node) => node == source ? replacement : base.Vi |
| | | 79 | | } |
| | | 80 | | } |