-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8117 from abpframework/maliming/global-feature
Create interceptor for global features
- Loading branch information
Showing
26 changed files
with
352 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/AbpGlobalFeatureErrorCodes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public class AbpGlobalFeatureErrorCodes | ||
{ | ||
public const string GlobalFeatureIsNotEnabled = "Volo.GlobalFeature:010001"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/AbpGlobalFeatureNotEnableException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using Volo.Abp.ExceptionHandling; | ||
|
||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public class AbpGlobalFeatureNotEnableException : AbpException, IHasErrorCode | ||
{ | ||
public string Code { get; } | ||
|
||
public AbpGlobalFeatureNotEnableException(string message = null, string code = null, Exception innerException = null) | ||
: base(message, innerException) | ||
{ | ||
Code = code; | ||
} | ||
|
||
public AbpGlobalFeatureNotEnableException WithData(string name, object value) | ||
{ | ||
Data[name] = value; | ||
return this; | ||
} | ||
} | ||
} |
35 changes: 34 additions & 1 deletion
35
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/AbpGlobalFeaturesModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,42 @@ | ||
using Volo.Abp.Modularity; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Volo.Abp.GlobalFeatures.Localization; | ||
using Volo.Abp.Localization; | ||
using Volo.Abp.Localization.ExceptionHandling; | ||
using Volo.Abp.Modularity; | ||
using Volo.Abp.VirtualFileSystem; | ||
|
||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
[DependsOn( | ||
typeof(AbpLocalizationModule), | ||
typeof(AbpVirtualFileSystemModule) | ||
)] | ||
public class AbpGlobalFeaturesModule : AbpModule | ||
{ | ||
public override void PreConfigureServices(ServiceConfigurationContext context) | ||
{ | ||
context.Services.OnRegistred(GlobalFeatureInterceptorRegistrar.RegisterIfNeeded); | ||
} | ||
|
||
public override void ConfigureServices(ServiceConfigurationContext context) | ||
{ | ||
|
||
Configure<AbpVirtualFileSystemOptions>(options => | ||
{ | ||
options.FileSets.AddEmbedded<AbpGlobalFeatureResource>(); | ||
}); | ||
|
||
Configure<AbpLocalizationOptions>(options => | ||
{ | ||
options.Resources | ||
.Add<AbpGlobalFeatureResource>("en") | ||
.AddVirtualJson("/Volo/Abp/GlobalFeatures/Localization"); | ||
}); | ||
|
||
Configure<AbpExceptionLocalizationOptions>(options => | ||
{ | ||
options.MapCodeNamespace("Volo.GlobalFeature", typeof(AbpGlobalFeatureResource)); | ||
}); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeatureHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using Volo.Abp.Reflection; | ||
|
||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public static class GlobalFeatureHelper | ||
{ | ||
public static bool IsGlobalFeatureEnabled(Type controllerType, out RequiresGlobalFeatureAttribute attribute) | ||
{ | ||
attribute = ReflectionHelper.GetSingleAttributeOrDefault<RequiresGlobalFeatureAttribute>(controllerType); | ||
return attribute == null || GlobalFeatureManager.Instance.IsEnabled(attribute.GetFeatureName()); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeatureInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Aspects; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.DynamicProxy; | ||
|
||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public class GlobalFeatureInterceptor : AbpInterceptor, ITransientDependency | ||
{ | ||
public override async Task InterceptAsync(IAbpMethodInvocation invocation) | ||
{ | ||
if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpCrossCuttingConcerns.GlobalFeatureChecking)) | ||
{ | ||
await invocation.ProceedAsync(); | ||
return; | ||
} | ||
|
||
if (!GlobalFeatureHelper.IsGlobalFeatureEnabled(invocation.TargetObject.GetType(), out var attribute)) | ||
{ | ||
throw new AbpGlobalFeatureNotEnableException(code: AbpGlobalFeatureErrorCodes.GlobalFeatureIsNotEnabled) | ||
.WithData("ServiceName", invocation.TargetObject.GetType().FullName) | ||
.WithData("GlobalFeatureName", attribute.Name); | ||
} | ||
|
||
await invocation.ProceedAsync(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeatureInterceptorRegistrar.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.DynamicProxy; | ||
|
||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public static class GlobalFeatureInterceptorRegistrar | ||
{ | ||
public static void RegisterIfNeeded(IOnServiceRegistredContext context) | ||
{ | ||
if (ShouldIntercept(context.ImplementationType)) | ||
{ | ||
context.Interceptors.TryAdd<GlobalFeatureInterceptor>(); | ||
} | ||
} | ||
|
||
private static bool ShouldIntercept(Type type) | ||
{ | ||
return !DynamicProxyIgnoreTypes.Contains(type) && typeof(IGlobalFeatureCheckingEnabled).IsAssignableFrom(type); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...work/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/IGlobalFeatureCheckingEnabled.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public interface IGlobalFeatureCheckingEnabled | ||
{ | ||
|
||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/Localization/AbpGlobalFeatureResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Volo.Abp.Localization; | ||
|
||
namespace Volo.Abp.GlobalFeatures.Localization | ||
{ | ||
[LocalizationResourceName("AbpGlobalFeature")] | ||
public class AbpGlobalFeatureResource | ||
{ | ||
|
||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/Localization/en.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"culture": "en", | ||
"texts": { | ||
"Volo.GlobalFeature:010001": "The '{ServiceName}' service needs to enable '{GlobalFeatureName}' feature." | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/Localization/tr.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"culture": "tr", | ||
"texts": { | ||
"Volo.GlobalFeature:010001": "'{ServiceName}' hizmetinin '{GlobalFeatureName}' özelliğini etkinleştirmesi gerekiyor." | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/Localization/zh-Hans.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"culture": "zh-Hans", | ||
"texts": { | ||
"Volo.GlobalFeature:010001": "'{ServiceName}'服务需要启用'{GlobalFeatureName}'功能." | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/Localization/zh-Hant.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"culture": "zh-Hant", | ||
"texts": { | ||
"Volo.GlobalFeature:010001": "'{ServiceName}'服務需要啟用'{GlobalFeatureName}'功能." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...res.Tests/Volo/Abp/GlobalFeatures/AbpGlobalFeatureNotEnableException_Localization_Test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Shouldly; | ||
using Volo.Abp.AspNetCore.ExceptionHandling; | ||
using Volo.Abp.Localization; | ||
using Xunit; | ||
|
||
namespace Volo.Abp.GlobalFeatures | ||
{ | ||
public class AbpGlobalFeatureNotEnableException_Localization_Test : GlobalFeatureTestBase | ||
{ | ||
private readonly IExceptionToErrorInfoConverter _exceptionToErrorInfoConverter; | ||
|
||
public AbpGlobalFeatureNotEnableException_Localization_Test() | ||
{ | ||
_exceptionToErrorInfoConverter = GetRequiredService<IExceptionToErrorInfoConverter>(); | ||
} | ||
|
||
[Fact] | ||
public void AbpAuthorizationException_Localization() | ||
{ | ||
using (CultureHelper.Use("zh-Hans")) | ||
{ | ||
var exception = new AbpGlobalFeatureNotEnableException(code: AbpGlobalFeatureErrorCodes.GlobalFeatureIsNotEnabled) | ||
.WithData("ServiceName", "MyService") | ||
.WithData("GlobalFeatureName", "TestFeature");; | ||
var errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); | ||
errorInfo.Message.ShouldBe("'MyService'服务需要启用'TestFeature'功能."); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.