mirror of https://github.com/abpframework/abp.git
committed by
GitHub
13 changed files with 311 additions and 30 deletions
@ -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; |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Features; |
|||
|
|||
public interface IFeatureValueProviderManager |
|||
{ |
|||
IReadOnlyList<IFeatureValueProvider> ValueProviders { get; } |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue