From e3204f0ff4102e8168ed4770602b206ccb67dbd0 Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 9 Oct 2024 15:00:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(data-protection):=20=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E5=AD=97=E8=A7=A3=E6=9E=90=E8=BF=94=E5=9B=9E=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataAccessKeywordContributorContext.cs | 6 +++++- .../DataProtection/EntityTypeFilterBuilder.cs | 6 ++---- .../IDataAccessKeywordContributor.cs | 6 ++++-- .../DataAccessCurrentUserContributor.cs | 21 +++++++++++++++++-- 4 files changed, 30 insertions(+), 9 deletions(-) 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); } }