-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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 #18823 from abpframework/liangshiwei/settings
Improve `ValueProvider` system.
- Loading branch information
Showing
13 changed files
with
311 additions
and
30 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
43 changes: 43 additions & 0 deletions
43
framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureValueProviderManager.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,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace Volo.Abp.Features; | ||
|
||
public class FeatureValueProviderManager : IFeatureValueProviderManager, ISingletonDependency | ||
{ | ||
public IReadOnlyList<IFeatureValueProvider> ValueProviders => _lazyProviders.Value; | ||
private readonly Lazy<List<IFeatureValueProvider>> _lazyProviders; | ||
|
||
protected AbpFeatureOptions Options { get; } | ||
protected IServiceProvider ServiceProvider { get; } | ||
|
||
public FeatureValueProviderManager( | ||
IServiceProvider serviceProvider, | ||
IOptions<AbpFeatureOptions> options) | ||
{ | ||
Options = options.Value; | ||
ServiceProvider = serviceProvider; | ||
|
||
_lazyProviders = new Lazy<List<IFeatureValueProvider>>(GetProviders, true); | ||
} | ||
|
||
protected virtual List<IFeatureValueProvider> GetProviders() | ||
{ | ||
var providers = Options | ||
.ValueProviders | ||
.Select(type => (ServiceProvider.GetRequiredService(type) as IFeatureValueProvider)!) | ||
.ToList(); | ||
|
||
var multipleProviders = providers.GroupBy(p => p.Name).FirstOrDefault(x => x.Count() > 1); | ||
if(multipleProviders != null) | ||
{ | ||
throw new AbpException($"Duplicate feature value provider name detected: {multipleProviders.Key}. Providers:{Environment.NewLine}{multipleProviders.Select(p => p.GetType().FullName!).JoinAsString(Environment.NewLine)}"); | ||
} | ||
|
||
return providers; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
framework/src/Volo.Abp.Features/Volo/Abp/Features/IFeatureValueProviderManager.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,8 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Volo.Abp.Features; | ||
|
||
public interface IFeatureValueProviderManager | ||
{ | ||
IReadOnlyList<IFeatureValueProvider> ValueProviders { get; } | ||
} |
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
59 changes: 59 additions & 0 deletions
59
...lo.Abp.Authorization.Tests/Volo/Abp/Authorization/PermissionValueProviderManager_Tests.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,59 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shouldly; | ||
using Volo.Abp.Authorization; | ||
using Volo.Abp.Authorization.Permissions; | ||
using Volo.Abp.Authorization.TestServices; | ||
using Xunit; | ||
|
||
namespace Volo.Abp; | ||
|
||
public class PermissionValueProviderManager_Tests: AuthorizationTestBase | ||
{ | ||
private readonly IPermissionValueProviderManager _permissionValueProviderManager; | ||
|
||
public PermissionValueProviderManager_Tests() | ||
{ | ||
_permissionValueProviderManager = GetRequiredService<IPermissionValueProviderManager>(); | ||
} | ||
|
||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) | ||
{ | ||
options.Services.Configure<AbpPermissionOptions>(permissionOptions => | ||
{ | ||
permissionOptions.ValueProviders.Add<TestDuplicatePermissionValueProvider>(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_Exception_If_Duplicate_Provider_Name_Detected() | ||
{ | ||
var exception = Assert.Throws<AbpException>(() => | ||
{ | ||
var providers = _permissionValueProviderManager.ValueProviders; | ||
}); | ||
|
||
exception.Message.ShouldBe($"Duplicate permission value provider name detected: TestPermissionValueProvider1. Providers:{Environment.NewLine}{typeof(TestDuplicatePermissionValueProvider).FullName}{Environment.NewLine}{typeof(TestPermissionValueProvider1).FullName}"); | ||
} | ||
} | ||
|
||
public class TestDuplicatePermissionValueProvider : PermissionValueProvider | ||
{ | ||
public TestDuplicatePermissionValueProvider(IPermissionStore permissionStore) : base(permissionStore) | ||
{ | ||
} | ||
|
||
public override string Name => "TestPermissionValueProvider1"; | ||
|
||
public override Task<PermissionGrantResult> CheckAsync(PermissionValueCheckContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override Task<MultiplePermissionGrantResult> CheckAsync(PermissionValuesCheckContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureValueProviderManager_Tests.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,54 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Volo.Abp.Features; | ||
|
||
public class FeatureValueProviderManager_Tests : FeatureTestBase | ||
{ | ||
private readonly IFeatureValueProviderManager _featureValueProviderManager; | ||
|
||
public FeatureValueProviderManager_Tests() | ||
{ | ||
_featureValueProviderManager = GetRequiredService<IFeatureValueProviderManager>(); | ||
} | ||
|
||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) | ||
{ | ||
options.Services.Configure<AbpFeatureOptions>(permissionOptions => | ||
{ | ||
permissionOptions.ValueProviders.Add<TestDuplicateFeatureValueProvider>(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_Exception_If_Duplicate_Provider_Name_Detected() | ||
{ | ||
var exception = Assert.Throws<AbpException>(() => | ||
{ | ||
var providers = _featureValueProviderManager.ValueProviders; | ||
}); | ||
|
||
exception.Message.ShouldBe($"Duplicate feature value provider name detected: {TestDuplicateFeatureValueProvider.ProviderName}. Providers:{Environment.NewLine}{typeof(TestDuplicateFeatureValueProvider).FullName}{Environment.NewLine}{typeof(DefaultValueFeatureValueProvider).FullName}"); | ||
} | ||
} | ||
|
||
public class TestDuplicateFeatureValueProvider : FeatureValueProvider | ||
{ | ||
public const string ProviderName = "D"; | ||
|
||
public override string Name => ProviderName; | ||
|
||
public TestDuplicateFeatureValueProvider(IFeatureStore settingStore) | ||
: base(settingStore) | ||
{ | ||
|
||
} | ||
|
||
public override Task<string> GetOrNullAsync(FeatureDefinition setting) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ework/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingValueProviderManager_Tests.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,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shouldly; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Testing; | ||
using Xunit; | ||
|
||
namespace Volo.Abp.Settings; | ||
|
||
public class SettingValueProviderManager_Tests: AbpIntegratedTest<AbpSettingsTestModule> | ||
{ | ||
private readonly ISettingValueProviderManager _settingValueProviderManager; | ||
|
||
public SettingValueProviderManager_Tests() | ||
{ | ||
_settingValueProviderManager = GetRequiredService<ISettingValueProviderManager>(); | ||
} | ||
|
||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) | ||
{ | ||
options.UseAutofac(); | ||
options.Services.Configure<AbpSettingOptions>(settingOptions => | ||
{ | ||
settingOptions.ValueProviders.Add<TestDuplicateSettingValueProvider>(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_Exception_If_Duplicate_Provider_Name_Detected() | ||
{ | ||
var exception = Assert.Throws<AbpException>(() => | ||
{ | ||
var providers = _settingValueProviderManager.Providers; | ||
}); | ||
|
||
exception.Message.ShouldBe($"Duplicate setting value provider name detected: {TestDuplicateSettingValueProvider.ProviderName}. Providers:{Environment.NewLine}{typeof(TestDuplicateSettingValueProvider).FullName}{Environment.NewLine}{typeof(TestSettingValueProvider).FullName}"); | ||
} | ||
} | ||
|
||
public class TestDuplicateSettingValueProvider : ISettingValueProvider, ITransientDependency | ||
{ | ||
public const string ProviderName = "Test"; | ||
|
||
|
||
public string Name => ProviderName; | ||
|
||
public TestDuplicateSettingValueProvider() | ||
{ | ||
} | ||
|
||
public Task<string> GetOrNullAsync(SettingDefinition setting) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<List<SettingValue>> GetAllAsync(SettingDefinition[] settings) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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
Oops, something went wrong.