mirror of https://github.com/abpframework/abp.git
7 changed files with 189 additions and 21 deletions
@ -0,0 +1,44 @@ |
|||
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 => GetProviders(); |
|||
private readonly Lazy<List<IFeatureValueProvider>> _lazyProviders; |
|||
|
|||
protected AbpFeatureOptions Options { get; } |
|||
|
|||
public FeatureValueProviderManager( |
|||
IServiceProvider serviceProvider, |
|||
IOptions<AbpFeatureOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
|
|||
_lazyProviders = new Lazy<List<IFeatureValueProvider>>( |
|||
() => Options |
|||
.ValueProviders |
|||
.Select(c => serviceProvider.GetRequiredService(c) as IFeatureValueProvider) |
|||
.ToList()!, |
|||
true |
|||
); |
|||
} |
|||
|
|||
protected virtual List<IFeatureValueProvider> GetProviders() |
|||
{ |
|||
var providers = _lazyProviders.Value; |
|||
|
|||
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(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue