Browse Source

feat(data-protection): 关键字解析返回表达式树

pull/1013/head
colin 1 year ago
parent
commit
e3204f0ff4
  1. 6
      aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/DataAccessKeywordContributorContext.cs
  2. 6
      aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/EntityTypeFilterBuilder.cs
  3. 6
      aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/IDataAccessKeywordContributor.cs
  4. 21
      aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/Keywords/DataAccessCurrentUserContributor.cs

6
aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/DataAccessKeywordContributorContext.cs

@ -4,8 +4,12 @@ namespace LINGYUN.Abp.DataProtection;
public class DataAccessKeywordContributorContext
{
public IServiceProvider ServiceProvider { get; }
public DataAccessKeywordContributorContext(IServiceProvider serviceProvider)
public Type ConversionType { get; }
public DataAccessKeywordContributorContext(
IServiceProvider serviceProvider,
Type conversionType)
{
ServiceProvider = serviceProvider;
ConversionType = conversionType;
}
}

6
aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/EntityTypeFilterBuilder.cs

@ -240,10 +240,8 @@ public class EntityTypeFilterBuilder : IEntityTypeFilterBuilder, ITransientDepen
{
if (_options.KeywordContributors.TryGetValue(rule.Value?.ToString() ?? "", out var contributor))
{
var context = new DataAccessKeywordContributorContext(_serviceProvider);
var keyValue = contributor.Contribute(context);
var value = CastTo(keyValue, conversionType);
return Expression.Constant(value, conversionType);
var context = new DataAccessKeywordContributorContext(_serviceProvider, conversionType);
return contributor.Contribute(context);
}
else
{

6
aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/IDataAccessKeywordContributor.cs

@ -1,6 +1,8 @@
namespace LINGYUN.Abp.DataProtection;
using System.Linq.Expressions;
namespace LINGYUN.Abp.DataProtection;
public interface IDataAccessKeywordContributor
{
string Keyword { get; }
object Contribute(DataAccessKeywordContributorContext context);
Expression Contribute(DataAccessKeywordContributorContext context);
}

21
aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/Keywords/DataAccessCurrentUserContributor.cs

@ -1,4 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq.Expressions;
using Volo.Abp.Users;
namespace LINGYUN.Abp.DataProtection.Keywords;
@ -7,9 +11,22 @@ public class DataAccessCurrentUserContributor : IDataAccessKeywordContributor
public const string Name = "@CurrentUser";
public string Keyword => Name;
public object Contribute(DataAccessKeywordContributorContext context)
public Expression Contribute(DataAccessKeywordContributorContext context)
{
var currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();
return currentUser.Id;
var userId = CastTo(currentUser.Id, context.ConversionType);
// entity.Where(x => x.CreatorId == CurrentUser.Id);
return Expression.Constant(userId, context.ConversionType);
}
private static object CastTo(object value, Type conversionType)
{
if (conversionType == typeof(Guid) || conversionType == typeof(Guid?))
{
return TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString()!)!;
}
return Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture);
}
}

Loading…
Cancel
Save