diff --git a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/DataAccessKeywordContributorContext.cs b/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/DataAccessKeywordContributorContext.cs index 2cb849833..5237a1b62 100644 --- a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/DataAccessKeywordContributorContext.cs +++ b/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; } } diff --git a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/EntityTypeFilterBuilder.cs b/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/EntityTypeFilterBuilder.cs index 2e17bf9a1..f5f3cd4f9 100644 --- a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/EntityTypeFilterBuilder.cs +++ b/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 { diff --git a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/IDataAccessKeywordContributor.cs b/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/IDataAccessKeywordContributor.cs index be985d265..aec82e52c 100644 --- a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/IDataAccessKeywordContributor.cs +++ b/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); } diff --git a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/Keywords/DataAccessCurrentUserContributor.cs b/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/Keywords/DataAccessCurrentUserContributor.cs index 50afda82e..296310ff1 100644 --- a/aspnet-core/framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN/Abp/DataProtection/Keywords/DataAccessCurrentUserContributor.cs +++ b/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(); - 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); } }