Merge remote-tracking branch 'origin/rc/3.4.0' into develop/3.5.0
This commit is contained in:
commit
ed6a20fd25
|
|
@ -36,4 +36,7 @@
|
|||
<!-- IL trimming warnings -->
|
||||
<NoWarn>$(NoWarn);IL2026;IL2046;IL2057;IL2067;IL2070;IL2072;IL2075;IL2087;IL2091</NoWarn>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ElsaStudioVersion>3.5.0-preview.1019</ElsaStudioVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
@ -31,10 +31,10 @@
|
|||
<PackageVersion Include="DistributedLock.Postgres" Version="1.3.0"/>
|
||||
<PackageVersion Include="DistributedLock.Redis" Version="1.0.3"/>
|
||||
<PackageVersion Include="Elastic.Clients.Elasticsearch" Version="9.0.1"/>
|
||||
<PackageVersion Include="Elsa.Studio" Version="3.5.0-preview.1005"/>
|
||||
<PackageVersion Include="Elsa.Studio.Agents" Version="3.5.0-preview.1005"/>
|
||||
<PackageVersion Include="Elsa.Studio.Core.BlazorWasm" Version="3.5.0-preview.1005"/>
|
||||
<PackageVersion Include="Elsa.Studio.Login.BlazorWasm" Version="3.5.0-preview.1005"/>
|
||||
<PackageVersion Include="Elsa.Studio" Version="$(ElsaStudioVersion)"/>
|
||||
<PackageVersion Include="Elsa.Studio.Agents" Version="$(ElsaStudioVersion)"/>
|
||||
<PackageVersion Include="Elsa.Studio.Core.BlazorWasm" Version="$(ElsaStudioVersion)"/>
|
||||
<PackageVersion Include="Elsa.Studio.Login.BlazorWasm" Version="$(ElsaStudioVersion)"/>
|
||||
<PackageVersion Include="FastEndpoints" Version="6.0.0"/>
|
||||
<PackageVersion Include="FastEndpoints.Security" Version="6.0.0"/>
|
||||
<PackageVersion Include="FastEndpoints.Swagger" Version="6.0.0"/>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
|
|||
};
|
||||
|
||||
protected IServiceProvider ServiceProvider { get; }
|
||||
private readonly ElsaDbContextOptions? _elsaDbContextOptions;
|
||||
private readonly ElsaDbContextOptions? elsaDbContextOptions;
|
||||
public string? TenantId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -41,10 +41,10 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
|
|||
protected ElsaDbContextBase(DbContextOptions options, IServiceProvider serviceProvider) : base(options)
|
||||
{
|
||||
ServiceProvider = serviceProvider;
|
||||
_elsaDbContextOptions = options.FindExtension<ElsaDbContextOptionsExtension>()?.Options;
|
||||
|
||||
elsaDbContextOptions = options.FindExtension<ElsaDbContextOptionsExtension>()?.Options;
|
||||
|
||||
// ReSharper disable once VirtualMemberCallInConstructor
|
||||
Schema = !string.IsNullOrWhiteSpace(_elsaDbContextOptions?.SchemaName) ? _elsaDbContextOptions.SchemaName : ElsaSchema;
|
||||
Schema = !string.IsNullOrWhiteSpace(elsaDbContextOptions?.SchemaName) ? elsaDbContextOptions.SchemaName : ElsaSchema;
|
||||
|
||||
var tenantAccessor = serviceProvider.GetService<ITenantAccessor>();
|
||||
var tenantId = tenantAccessor?.Tenant?.Id;
|
||||
|
|
@ -63,11 +63,11 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
|
|||
/// <inheritdoc />
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Schema))
|
||||
if (!string.IsNullOrWhiteSpace(Schema))
|
||||
modelBuilder.HasDefaultSchema(Schema);
|
||||
|
||||
var additionalConfigurations = _elsaDbContextOptions?.GetModelConfigurations(this);
|
||||
|
||||
var additionalConfigurations = elsaDbContextOptions?.GetModelConfigurations(this);
|
||||
|
||||
additionalConfigurations?.Invoke(modelBuilder);
|
||||
|
||||
using var scope = ServiceProvider.CreateScope();
|
||||
|
|
@ -75,7 +75,7 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
|
|||
|
||||
foreach (var entityType in modelBuilder.Model.GetEntityTypes().ToList())
|
||||
{
|
||||
foreach (var handler in entityTypeHandlers)
|
||||
foreach (var handler in entityTypeHandlers)
|
||||
handler.Handle(this, modelBuilder, entityType);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
using System.Linq.Expressions;
|
||||
using Elsa.Common.Entities;
|
||||
using Elsa.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace Elsa.EntityFrameworkCore.EntityHandlers;
|
||||
|
||||
|
|
@ -15,15 +13,32 @@ public class SetTenantIdFilter : IEntityModelCreatingHandler
|
|||
/// <inheritdoc />
|
||||
public void Handle(ElsaDbContextBase dbContext, ModelBuilder modelBuilder, IMutableEntityType entityType)
|
||||
{
|
||||
if (!entityType.ClrType.IsAssignableTo(typeof(Entity)))
|
||||
if (!typeof(Entity).IsAssignableFrom(entityType.ClrType))
|
||||
return;
|
||||
|
||||
var tenantId = dbContext.TenantId.NullIfEmpty();
|
||||
var parameter = Expression.Parameter(entityType.ClrType);
|
||||
Expression<Func<Entity, bool>> filterExpr = entity => entity.TenantId == tenantId;
|
||||
var body = ReplacingExpressionVisitor.Replace(filterExpr.Parameters[0], parameter, filterExpr.Body);
|
||||
var lambdaExpression = Expression.Lambda(body, parameter);
|
||||
modelBuilder
|
||||
.Entity(entityType.ClrType)
|
||||
.HasQueryFilter(CreateTenantFilterExpression(dbContext, entityType.ClrType));
|
||||
}
|
||||
|
||||
entityType.SetQueryFilter(lambdaExpression);
|
||||
private LambdaExpression CreateTenantFilterExpression(ElsaDbContextBase dbContext, Type clrType)
|
||||
{
|
||||
var parameter = Expression.Parameter(clrType, "e");
|
||||
|
||||
// e => EF.Property<string>(e, "TenantId") == this.TenantId
|
||||
var tenantIdProperty = Expression.Call(
|
||||
typeof(EF),
|
||||
nameof(EF.Property),
|
||||
[typeof(string)],
|
||||
parameter,
|
||||
Expression.Constant("TenantId"));
|
||||
|
||||
var tenantIdOnContext = Expression.Property(
|
||||
Expression.Constant(dbContext),
|
||||
nameof(ElsaDbContextBase.TenantId));
|
||||
|
||||
var body = Expression.Equal(tenantIdProperty, tenantIdOnContext);
|
||||
|
||||
return Expression.Lambda(body, parameter);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue