mirror of https://github.com/abpframework/abp.git
committed by
GitHub
214 changed files with 2908 additions and 628 deletions
@ -0,0 +1,54 @@ |
|||
# Microservice Demo, Projects Status and Road Map |
|||
|
|||
After [the first announcement](https://abp.io/blog/abp/Abp-vNext-Announcement) on the ABP vNext, we have a lot of improvements on the codebase (1100+ commits on the [GitHub repository](https://github.com/abpframework/abp)). We've created features, samples, documentation and much more. In this post, I want to inform you about some news and the status of the project. |
|||
|
|||
## Microservice Demo Solution |
|||
|
|||
One of the major goals of the ABP framework is to provide a [convenient infrastructure to create microservice solutions](https://abp.io/documents/abp/latest/Microservice-Architecture). |
|||
|
|||
We've been working to develop a microservice solution demo. Initial version was completed and [documented](https://abp.io/documents/abp/latest/Samples/Microservice-Demo). This sample solution aims to demonstrate a simple yet complete microservice solution; |
|||
|
|||
- Has multiple, independent, self-deployable **microservices**. |
|||
- Multiple **web applications**, each uses a different API gateway. |
|||
- Has multiple **gateways** / BFFs (Backend for Frontends) developed using the [Ocelot](https://github.com/ThreeMammals/Ocelot) library. |
|||
- Has an **authentication service** developed using the [IdentityServer](https://identityserver.io/) framework. It's also a SSO (Single Sign On) application with necessary UIs. |
|||
- Has **multiple databases**. Some microservices has their own database while some services/applications shares a database (to demonstrate different use cases). |
|||
- Has different types of databases: **SQL Server** (with **Entity Framework Core** ORM) and **MongoDB**. |
|||
- Has a **console application** to show the simplest way of using a service by authenticating. |
|||
- Uses [Redis](https://redis.io/) for **distributed caching**. |
|||
- Uses [RabbitMQ](https://www.rabbitmq.com/) for service-to-service **messaging**. |
|||
- Uses [Docker](https://www.docker.com/) & [Kubernates](https://kubernetes.io/) to **deploy** & run all services and applications. |
|||
- Uses [Elasticsearch](https://www.elastic.co/products/elasticsearch) & [Kibana](https://www.elastic.co/products/kibana) to store and visualize the logs (written using [Serilog](https://serilog.net/)). |
|||
|
|||
See [its documentation](https://abp.io/documents/abp/latest/Samples/Microservice-Demo) for a detailed explanation of the solution. |
|||
|
|||
## Improvements/Features |
|||
|
|||
We've worked on so many features including **distributed event bus** (with RabbitMQ integration), **IdentityServer4 integration** and enhancements for almost all features. We are continuously refactoring and adding tests to make the framework more stable and production ready. It is [rapidly growing](https://github.com/abpframework/abp/graphs/contributors). |
|||
|
|||
## Road Map |
|||
|
|||
There are still too much work to be done before the first stable release (v1.0). You can see [prioritized backlog items](https://github.com/abpframework/abp/issues?q=is%3Aopen+is%3Aissue+milestone%3ABacklog) on the GitHub repo. |
|||
|
|||
According to our estimation, we have planned to release v1.0 in Q2 of 2019 (probably in May or June). So, not too much time to wait. We are also very excited for the first stable release. |
|||
|
|||
We will also work on [the documentation](https://abp.io/documents/abp/latest) since it is far from complete now. |
|||
|
|||
First release may not include a SPA template. However, we want to prepare a simple one if it can be possible. Haven't decided yet about the SPA framework. Alternatives: **Angular, React and Blazor**. Please write your thought as a comment to this post. |
|||
|
|||
## Chinese Web Site |
|||
|
|||
There is a big ABP community in China. They have created a Chinese version of the abp.io web site: https://cn.abp.io/ They are keeping it up to date. Thanks to the Chinese developers and especially to [Liming Ma](https://github.com/maliming). |
|||
|
|||
## NDC {London} 2019 |
|||
|
|||
It was a pleasure to be in [NDC {London}](https://ndc-london.com/) 2019 as a partner. We've talked to many developers about the current ASP.NET Boilerplate and the ABP vNext and we got good feedbacks. |
|||
|
|||
We also had a chance to talk with [Scott Hanselman](https://twitter.com/shanselman) and [Jon Galloway](https://twitter.com/jongalloway). They visited our booth and we talked about the ideas for ABP vNext. They liked features, approaches and the goal of new ABP framework. See some photos and comments on twitter: |
|||
|
|||
 |
|||
|
|||
## Follow It |
|||
|
|||
* You can star and follow the **GitHub** repository: https://github.com/abpframework/abp |
|||
* You can follow the official **Twitter** account for news: https://twitter.com/abpframework |
|||
|
After Width: | Height: | Size: 477 KiB |
|
After Width: | Height: | Size: 154 KiB |
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Features; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
public class RemoteFeatureChecker : FeatureCheckerBase |
|||
{ |
|||
protected ICachedApplicationConfigurationClient ConfigurationClient { get; } |
|||
|
|||
public RemoteFeatureChecker(ICachedApplicationConfigurationClient configurationClient) |
|||
{ |
|||
ConfigurationClient = configurationClient; |
|||
} |
|||
|
|||
public override async Task<string> GetOrNullAsync(string name) |
|||
{ |
|||
var configuration = await ConfigurationClient.GetAsync(); |
|||
return configuration.Features.Values.GetOrDefault(name); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations |
|||
{ |
|||
public class ApplicationFeatureConfigurationDto |
|||
{ |
|||
public Dictionary<string, string> Values { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.AspNetCore.Mvc.UI.Bundling; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.FlagIconCss |
|||
{ |
|||
public class FlagIconCssStyleContributor : BundleContributor |
|||
{ |
|||
public override void ConfigureBundle(BundleConfigurationContext context) |
|||
{ |
|||
context.Files.AddIfNotContains("/libs/flag-icon-css/css/flag-icon.min.css"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using Volo.Abp.AspNetCore.Mvc.UI.Bundling; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Bundling |
|||
{ |
|||
public class BasicThemeGlobalScriptContributor : BundleContributor |
|||
{ |
|||
public override void ConfigureBundle(BundleConfigurationContext context) |
|||
{ |
|||
context.Files.Add("/themes/basic/layout.js"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
@using Volo.Abp.UI.Navigation |
|||
@model ApplicationMenuItem |
|||
@{ |
|||
var elementId = string.IsNullOrEmpty(Model.ElementId) ? string.Empty : $"id=\"{Model.ElementId}\""; |
|||
var cssClass = string.IsNullOrEmpty(Model.CssClass) ? string.Empty : Model.CssClass; |
|||
var disabled = Model.IsDisabled ? "disabled" : string.Empty; |
|||
} |
|||
@if (Model.IsLeaf) |
|||
{ |
|||
@if (Model.Url != null) |
|||
{ |
|||
<a class="dropdown-item @cssClass @disabled" href="@(Model.Url ?? "#")" @Html.Raw(elementId)> |
|||
@Model.DisplayName |
|||
</a> |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
<div class="dropdown-submenu"> |
|||
<a role="button" class="btn dropdown-toggle" data-toggle="dropdown" |
|||
aria-haspopup="true" aria-expanded="false"> |
|||
<span class="lp-icon"> |
|||
<i class="@(Model.Icon ?? "")"></i> |
|||
</span> |
|||
<span class="lp-text"> |
|||
@Model.DisplayName |
|||
</span> |
|||
</a> |
|||
<div class="dropdown-menu"> |
|||
@foreach (var childMenuItem in Model.Items) |
|||
{ |
|||
@await Html.PartialAsync("~/Themes/Basic/Components/Menu/_MenuItem.cshtml", childMenuItem) |
|||
} |
|||
</div> |
|||
</div> |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
$(function () { |
|||
$('.dropdown-menu a.dropdown-toggle').on('click', function (e) { |
|||
if (!$(this).next().hasClass('show')) { |
|||
$(this).parents('.dropdown-menu').first().find('.show').removeClass("show"); |
|||
} |
|||
|
|||
var $subMenu = $(this).next(".dropdown-menu"); |
|||
$subMenu.toggleClass('show'); |
|||
|
|||
$(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function (e) { |
|||
$('.dropdown-submenu .show').removeClass("show"); |
|||
}); |
|||
|
|||
return false; |
|||
}); |
|||
}); |
|||
@ -0,0 +1,42 @@ |
|||
using Microsoft.AspNetCore.Mvc.Filters; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc.Abstractions; |
|||
using Volo.Abp.Aspects; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Features; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Features |
|||
{ |
|||
public class AbpFeatureActionFilter : IAsyncActionFilter, ITransientDependency |
|||
{ |
|||
private readonly IMethodInvocationFeatureCheckerService _methodInvocationAuthorizationService; |
|||
|
|||
public AbpFeatureActionFilter(IMethodInvocationFeatureCheckerService methodInvocationAuthorizationService) |
|||
{ |
|||
_methodInvocationAuthorizationService = methodInvocationAuthorizationService; |
|||
} |
|||
|
|||
public async Task OnActionExecutionAsync( |
|||
ActionExecutingContext context, |
|||
ActionExecutionDelegate next) |
|||
{ |
|||
if (!context.ActionDescriptor.IsControllerAction()) |
|||
{ |
|||
await next(); |
|||
return; |
|||
} |
|||
|
|||
var methodInfo = context.ActionDescriptor.GetMethodInfo(); |
|||
|
|||
using (AbpCrossCuttingConcerns.Applying(context.Controller, AbpCrossCuttingConcerns.FeatureChecking)) |
|||
{ |
|||
await _methodInvocationAuthorizationService.CheckAsync( |
|||
new MethodInvocationFeatureCheckerContext(methodInfo) |
|||
); |
|||
|
|||
await next(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.Authorization |
|||
{ |
|||
public interface IAuthorizationEnabled |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -1,8 +1,6 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Authorization.Permissions |
|||
namespace Volo.Abp.Authorization.Permissions |
|||
{ |
|||
public interface IPermissionDefinitionProvider : ISingletonDependency |
|||
public interface IPermissionDefinitionProvider |
|||
{ |
|||
void Define(IPermissionDefinitionContext context); |
|||
} |
|||
|
|||
@ -1,12 +1,12 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Authorization.Permissions |
|||
{ |
|||
public interface IPermissionValueProvider : ISingletonDependency |
|||
public interface IPermissionValueProvider |
|||
{ |
|||
string Name { get; } |
|||
|
|||
Task<PermissionValueProviderGrantInfo> CheckAsync(PermissionValueCheckContext context); |
|||
//TODO: Rename to GetResult? (CheckAsync throws exception by naming convention)
|
|||
Task<PermissionGrantResult> CheckAsync(PermissionValueCheckContext context); |
|||
} |
|||
} |
|||
@ -1,20 +0,0 @@ |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Authorization.Permissions |
|||
{ |
|||
public static class PermissionCheckerExtensions |
|||
{ |
|||
public static async Task<bool> IsGrantedAsync(this IPermissionChecker permissionChecker, string name) |
|||
{ |
|||
return (await permissionChecker.CheckAsync(name)).IsGranted; |
|||
} |
|||
|
|||
public static async Task<bool> IsGrantedAsync(this IPermissionChecker permissionChecker, ClaimsPrincipal principal, string name) |
|||
{ |
|||
return (await permissionChecker.CheckAsync(principal, name)).IsGranted; |
|||
} |
|||
|
|||
//TODO: Add sync extensions
|
|||
} |
|||
} |
|||
@ -1,6 +1,8 @@ |
|||
namespace Volo.Abp.Authorization.Permissions |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Authorization.Permissions |
|||
{ |
|||
public abstract class PermissionDefinitionProvider : IPermissionDefinitionProvider |
|||
public abstract class PermissionDefinitionProvider : IPermissionDefinitionProvider, ITransientDependency |
|||
{ |
|||
public abstract void Define(IPermissionDefinitionContext context); |
|||
} |
|||
|
|||
@ -0,0 +1,9 @@ |
|||
namespace Volo.Abp.Authorization.Permissions |
|||
{ |
|||
public enum PermissionGrantResult |
|||
{ |
|||
Undefined, |
|||
Granted, |
|||
Prohibited |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Threading |
|||
{ |
|||
public static class TaskCache |
|||
{ |
|||
public static Task<bool> TrueResult { get; } |
|||
public static Task<bool> FalseResult { get; } |
|||
|
|||
static TaskCache() |
|||
{ |
|||
TrueResult = Task.FromResult(true); |
|||
FalseResult = Task.FromResult(false); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.Features</AssemblyName> |
|||
<PackageId>Volo.Abp.Features</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.Localization.Abstractions\Volo.Abp.Localization.Abstractions.csproj" /> |
|||
<ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" /> |
|||
<ProjectReference Include="..\Volo.Abp.Validation\Volo.Abp.Validation.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,51 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpLocalizationAbstractionsModule), |
|||
typeof(AbpMultiTenancyAbstractionsModule), |
|||
typeof(AbpValidationModule) |
|||
)] |
|||
public class AbpFeaturesModule : AbpModule |
|||
{ |
|||
public override void PreConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.OnRegistred(FeatureInterceptorRegistrar.RegisterIfNeeded); |
|||
AutoAddDefinitionProviders(context.Services); |
|||
} |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.Configure<FeatureOptions>(options => |
|||
{ |
|||
options.ValueProviders.Add<DefaultValueFeatureValueProvider>(); |
|||
options.ValueProviders.Add<TenantFeatureValueProvider>(); |
|||
}); |
|||
} |
|||
|
|||
private static void AutoAddDefinitionProviders(IServiceCollection services) |
|||
{ |
|||
var definitionProviders = new List<Type>(); |
|||
|
|||
services.OnRegistred(context => |
|||
{ |
|||
if (typeof(IFeatureDefinitionProvider).IsAssignableFrom(context.ImplementationType)) |
|||
{ |
|||
definitionProviders.Add(context.ImplementationType); |
|||
} |
|||
}); |
|||
|
|||
services.Configure<FeatureOptions>(options => |
|||
{ |
|||
options.DefinitionProviders.AddIfNotContains(definitionProviders); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class DefaultValueFeatureValueProvider : FeatureValueProvider |
|||
{ |
|||
public const string ProviderName = "Default"; |
|||
|
|||
public override string Name => ProviderName; |
|||
|
|||
public DefaultValueFeatureValueProvider(IFeatureStore settingStore) |
|||
: base(settingStore) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public override Task<string> GetOrNullAsync(FeatureDefinition setting) |
|||
{ |
|||
return Task.FromResult(setting.DefaultValue); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Method)] |
|||
public class DisableFeatureCheckAttribute : Attribute |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureChecker : FeatureCheckerBase |
|||
{ |
|||
protected FeatureOptions Options { get; } |
|||
protected IServiceProvider ServiceProvider { get; } |
|||
protected IFeatureDefinitionManager FeatureDefinitionManager { get; } |
|||
protected List<IFeatureValueProvider> Providers => _providers.Value; |
|||
|
|||
private readonly Lazy<List<IFeatureValueProvider>> _providers; |
|||
|
|||
public FeatureChecker( |
|||
IOptions<FeatureOptions> options, |
|||
IServiceProvider serviceProvider, |
|||
IFeatureDefinitionManager featureDefinitionManager) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
FeatureDefinitionManager = featureDefinitionManager; |
|||
|
|||
Options = options.Value; |
|||
|
|||
_providers = new Lazy<List<IFeatureValueProvider>>( |
|||
() => Options |
|||
.ValueProviders |
|||
.Select(type => ServiceProvider.GetRequiredService(type) as IFeatureValueProvider) |
|||
.ToList(), |
|||
true |
|||
); |
|||
} |
|||
|
|||
public override async Task<string> GetOrNullAsync(string name) |
|||
{ |
|||
var featureDefinition = FeatureDefinitionManager.Get(name); |
|||
var providers = Enumerable |
|||
.Reverse(Providers); |
|||
|
|||
if (featureDefinition.AllowedProviders.Any()) |
|||
{ |
|||
providers = providers.Where(p => featureDefinition.AllowedProviders.Contains(p.Name)); |
|||
} |
|||
|
|||
return await GetOrNullValueFromProvidersAsync(providers, featureDefinition); |
|||
} |
|||
|
|||
protected virtual async Task<string> GetOrNullValueFromProvidersAsync( |
|||
IEnumerable<IFeatureValueProvider> providers, |
|||
FeatureDefinition feature) |
|||
{ |
|||
foreach (var provider in providers) |
|||
{ |
|||
var value = await provider.GetOrNullAsync(feature); |
|||
if (value != null) |
|||
{ |
|||
return value; |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public abstract class FeatureCheckerBase : IFeatureChecker, ITransientDependency |
|||
{ |
|||
public abstract Task<string> GetOrNullAsync(string name); |
|||
|
|||
public virtual async Task<bool> IsEnabledAsync(string name) |
|||
{ |
|||
var value = await GetOrNullAsync(name); |
|||
if (value == null) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
try |
|||
{ |
|||
return bool.Parse(value); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
throw new AbpException( |
|||
$"The value '{value}' for the feature '{name}' should be a boolean, but was not!", |
|||
ex |
|||
); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,143 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Authorization; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public static class FeatureCheckerExtensions |
|||
{ |
|||
public static async Task<T> GetAsync<T>( |
|||
[NotNull] this IFeatureChecker featureChecker, |
|||
[NotNull] string name, |
|||
T defaultValue = default) |
|||
where T : struct |
|||
{ |
|||
Check.NotNull(featureChecker, nameof(featureChecker)); |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
var value = await featureChecker.GetOrNullAsync(name); |
|||
return value?.To<T>() ?? defaultValue; |
|||
} |
|||
|
|||
public static string GetOrNull( |
|||
[NotNull] this IFeatureChecker featureChecker, |
|||
[NotNull] string name) |
|||
{ |
|||
Check.NotNull(featureChecker, nameof(featureChecker)); |
|||
return AsyncHelper.RunSync(() => featureChecker.GetOrNullAsync(name)); |
|||
} |
|||
|
|||
public static T Get<T>( |
|||
[NotNull] this IFeatureChecker featureChecker, |
|||
[NotNull] string name, |
|||
T defaultValue = default) |
|||
where T : struct |
|||
{ |
|||
return AsyncHelper.RunSync(() => featureChecker.GetAsync(name, defaultValue)); |
|||
} |
|||
|
|||
public static bool IsEnabled( |
|||
[NotNull] this IFeatureChecker featureChecker, |
|||
[NotNull] string name) |
|||
{ |
|||
return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(name)); |
|||
} |
|||
|
|||
public static async Task<bool> IsEnabledAsync(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames) |
|||
{ |
|||
if (featureNames.IsNullOrEmpty()) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
if (requiresAll) |
|||
{ |
|||
foreach (var featureName in featureNames) |
|||
{ |
|||
if (!(await featureChecker.IsEnabledAsync(featureName))) |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
foreach (var featureName in featureNames) |
|||
{ |
|||
if (await featureChecker.IsEnabledAsync(featureName)) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public static bool IsEnabled(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames) |
|||
{ |
|||
return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(requiresAll, featureNames)); |
|||
} |
|||
|
|||
public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, string featureName) |
|||
{ |
|||
if (!(await featureChecker.IsEnabledAsync(featureName))) |
|||
{ |
|||
throw new AbpAuthorizationException("Feature is not enabled: " + featureName); |
|||
} |
|||
} |
|||
|
|||
public static void CheckEnabled(this IFeatureChecker featureChecker, string featureName) |
|||
{ |
|||
if (!featureChecker.IsEnabled(featureName)) |
|||
{ |
|||
throw new AbpAuthorizationException("Feature is not enabled: " + featureName); |
|||
} |
|||
} |
|||
|
|||
public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames) |
|||
{ |
|||
if (featureNames.IsNullOrEmpty()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (requiresAll) |
|||
{ |
|||
foreach (var featureName in featureNames) |
|||
{ |
|||
if (!(await featureChecker.IsEnabledAsync(featureName))) |
|||
{ |
|||
throw new AbpAuthorizationException( |
|||
"Required features are not enabled. All of these features must be enabled: " + |
|||
string.Join(", ", featureNames) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
foreach (var featureName in featureNames) |
|||
{ |
|||
if (await featureChecker.IsEnabledAsync(featureName)) |
|||
{ |
|||
return; |
|||
} |
|||
} |
|||
|
|||
throw new AbpAuthorizationException( |
|||
"Required features are not enabled. At least one of these features must be enabled: " + |
|||
string.Join(", ", featureNames) |
|||
); |
|||
} |
|||
} |
|||
|
|||
public static void CheckEnabled(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames) |
|||
{ |
|||
AsyncHelper.RunSync(() => featureChecker.CheckEnabledAsync(requiresAll, featureNames)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,178 @@ |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using System.Linq; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.Validation.StringValues; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureDefinition |
|||
{ |
|||
/// <summary>
|
|||
/// Unique name of the feature.
|
|||
/// </summary>
|
|||
[NotNull] |
|||
public string Name { get; } |
|||
|
|||
[NotNull] |
|||
public ILocalizableString DisplayName |
|||
{ |
|||
get => _displayName; |
|||
set => _displayName = Check.NotNull(value, nameof(value)); |
|||
} |
|||
private ILocalizableString _displayName; |
|||
|
|||
[CanBeNull] |
|||
public ILocalizableString Description { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Parent of this feature, if one exists.
|
|||
/// If set, this feature can be enabled only if the parent is enabled.
|
|||
/// </summary>
|
|||
[CanBeNull] |
|||
public FeatureDefinition Parent { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// List of child features.
|
|||
/// </summary>
|
|||
public IReadOnlyList<FeatureDefinition> Children => _children.ToImmutableList(); |
|||
private readonly List<FeatureDefinition> _children; |
|||
|
|||
/// <summary>
|
|||
/// Default value of the feature.
|
|||
/// </summary>
|
|||
[CanBeNull] |
|||
public string DefaultValue { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Can clients see this feature and it's value.
|
|||
/// Default: true.
|
|||
/// </summary>
|
|||
public bool IsVisibleToClients { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// A list of allowed providers to get/set value of this feature.
|
|||
/// An empty list indicates that all providers are allowed.
|
|||
/// </summary>
|
|||
[NotNull] |
|||
public List<string> AllowedProviders { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets/sets a key-value on the <see cref="Properties"/>.
|
|||
/// </summary>
|
|||
/// <param name="name">Name of the property</param>
|
|||
/// <returns>
|
|||
/// Returns the value in the <see cref="Properties"/> dictionary by given <see cref="name"/>.
|
|||
/// Returns null if given <see cref="name"/> is not present in the <see cref="Properties"/> dictionary.
|
|||
/// </returns>
|
|||
[CanBeNull] |
|||
public object this[string name] |
|||
{ |
|||
get => Properties.GetOrDefault(name); |
|||
set => Properties[name] = value; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can be used to get/set custom properties for this feature.
|
|||
/// </summary>
|
|||
[NotNull] |
|||
public Dictionary<string, object> Properties { get; } |
|||
|
|||
/// <summary>
|
|||
/// Input type.
|
|||
/// This can be used to prepare an input for changing this feature's value.
|
|||
/// Default: <see cref="ToggleStringValueType"/>.
|
|||
/// </summary>
|
|||
[CanBeNull] |
|||
public IStringValueType ValueType { get; set; } |
|||
|
|||
public FeatureDefinition( |
|||
string name, |
|||
string defaultValue = null, |
|||
ILocalizableString displayName = null, |
|||
ILocalizableString description = null, |
|||
IStringValueType valueType = null, |
|||
bool isVisibleToClients = true) |
|||
{ |
|||
Name = name; |
|||
DefaultValue = defaultValue; |
|||
DisplayName = displayName ?? new FixedLocalizableString(name); |
|||
Description = description; |
|||
ValueType = valueType; |
|||
IsVisibleToClients = isVisibleToClients; |
|||
|
|||
Properties = new Dictionary<string, object>(); |
|||
AllowedProviders = new List<string>(); |
|||
_children = new List<FeatureDefinition>(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets a property in the <see cref="Properties"/> dictionary.
|
|||
/// This is a shortcut for nested calls on this object.
|
|||
/// </summary>
|
|||
public virtual FeatureDefinition WithProperty(string key, object value) |
|||
{ |
|||
Properties[key] = value; |
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets a property in the <see cref="Properties"/> dictionary.
|
|||
/// This is a shortcut for nested calls on this object.
|
|||
/// </summary>
|
|||
public virtual FeatureDefinition WithProviders(params string[] providers) |
|||
{ |
|||
if (!providers.IsNullOrEmpty()) |
|||
{ |
|||
AllowedProviders.AddRange(providers); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds a child feature.
|
|||
/// </summary>
|
|||
/// <returns>Returns a newly created child feature</returns>
|
|||
public FeatureDefinition CreateChild( |
|||
string name, |
|||
string defaultValue = null, |
|||
ILocalizableString displayName = null, |
|||
ILocalizableString description = null, |
|||
IStringValueType valueType = null, |
|||
bool isVisibleToClients = true) |
|||
{ |
|||
var feature = new FeatureDefinition( |
|||
name, |
|||
defaultValue, |
|||
displayName, |
|||
description, |
|||
valueType, |
|||
isVisibleToClients) |
|||
{ |
|||
Parent = this |
|||
}; |
|||
|
|||
_children.Add(feature); |
|||
return feature; |
|||
} |
|||
|
|||
public void RemoveChild(string name) |
|||
{ |
|||
var featureToRemove = _children.FirstOrDefault(f => f.Name == name); |
|||
if (featureToRemove == null) |
|||
{ |
|||
throw new AbpException($"Could not find a feature named '{name}' in the Children of this feature '{Name}'."); |
|||
} |
|||
|
|||
featureToRemove.Parent = null; |
|||
_children.Remove(featureToRemove); |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"[{nameof(FeatureDefinition)}: {Name}]"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.Localization; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureDefinitionContext : IFeatureDefinitionContext |
|||
{ |
|||
internal Dictionary<string, FeatureGroupDefinition> Groups { get; } |
|||
|
|||
public FeatureDefinitionContext() |
|||
{ |
|||
Groups = new Dictionary<string, FeatureGroupDefinition>(); |
|||
} |
|||
|
|||
public FeatureGroupDefinition AddGroup(string name, ILocalizableString displayName = null) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
if (Groups.ContainsKey(name)) |
|||
{ |
|||
throw new AbpException($"There is already an existing permission group with name: {name}"); |
|||
} |
|||
|
|||
return Groups[name] = new FeatureGroupDefinition(name, displayName); |
|||
} |
|||
|
|||
public FeatureGroupDefinition GetGroupOrNull(string name) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
if (!Groups.ContainsKey(name)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return Groups[name]; |
|||
} |
|||
|
|||
public void RemoveGroup(string name) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
if (!Groups.ContainsKey(name)) |
|||
{ |
|||
throw new AbpException($"Undefined feature group: '{name}'."); |
|||
} |
|||
|
|||
Groups.Remove(name); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using System.Linq; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureDefinitionManager : IFeatureDefinitionManager, ISingletonDependency |
|||
{ |
|||
protected IDictionary<string, FeatureGroupDefinition> FeatureGroupDefinitions => _lazyFeatureGroupDefinitions.Value; |
|||
private readonly Lazy<Dictionary<string, FeatureGroupDefinition>> _lazyFeatureGroupDefinitions; |
|||
|
|||
protected IDictionary<string, FeatureDefinition> FeatureDefinitions => _lazyFeatureDefinitions.Value; |
|||
private readonly Lazy<Dictionary<string, FeatureDefinition>> _lazyFeatureDefinitions; |
|||
|
|||
protected FeatureOptions Options { get; } |
|||
|
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public FeatureDefinitionManager( |
|||
IOptions<FeatureOptions> options, |
|||
IServiceProvider serviceProvider) |
|||
{ |
|||
_serviceProvider = serviceProvider; |
|||
Options = options.Value; |
|||
|
|||
_lazyFeatureDefinitions = new Lazy<Dictionary<string, FeatureDefinition>>( |
|||
CreateFeatureDefinitions, |
|||
isThreadSafe: true |
|||
); |
|||
|
|||
_lazyFeatureGroupDefinitions = new Lazy<Dictionary<string, FeatureGroupDefinition>>( |
|||
CreateFeatureGroupDefinitions, |
|||
isThreadSafe:true |
|||
); |
|||
} |
|||
|
|||
public virtual FeatureDefinition Get(string name) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
var feature = GetOrNull(name); |
|||
|
|||
if (feature == null) |
|||
{ |
|||
throw new AbpException("Undefined feature: " + name); |
|||
} |
|||
|
|||
return feature; |
|||
} |
|||
|
|||
public virtual IReadOnlyList<FeatureDefinition> GetAll() |
|||
{ |
|||
return FeatureDefinitions.Values.ToImmutableList(); |
|||
} |
|||
|
|||
public virtual FeatureDefinition GetOrNull(string name) |
|||
{ |
|||
return FeatureDefinitions.GetOrDefault(name); |
|||
} |
|||
|
|||
protected virtual Dictionary<string, FeatureDefinition> CreateFeatureDefinitions() |
|||
{ |
|||
var features = new Dictionary<string, FeatureDefinition>(); |
|||
|
|||
foreach (var groupDefinition in FeatureGroupDefinitions.Values) |
|||
{ |
|||
foreach (var feature in groupDefinition.Features) |
|||
{ |
|||
AddFeatureToDictionaryRecursively(features, feature); |
|||
} |
|||
} |
|||
|
|||
return features; |
|||
} |
|||
|
|||
protected virtual void AddFeatureToDictionaryRecursively( |
|||
Dictionary<string, FeatureDefinition> features, |
|||
FeatureDefinition feature) |
|||
{ |
|||
if (features.ContainsKey(feature.Name)) |
|||
{ |
|||
throw new AbpException("Duplicate feature name: " + feature.Name); |
|||
} |
|||
|
|||
features[feature.Name] = feature; |
|||
|
|||
foreach (var child in feature.Children) |
|||
{ |
|||
AddFeatureToDictionaryRecursively(features, child); |
|||
} |
|||
} |
|||
|
|||
protected virtual Dictionary<string, FeatureGroupDefinition> CreateFeatureGroupDefinitions() |
|||
{ |
|||
var context = new FeatureDefinitionContext(); |
|||
|
|||
using (var scope = _serviceProvider.CreateScope()) |
|||
{ |
|||
var providers = Options |
|||
.DefinitionProviders |
|||
.Select(p => scope.ServiceProvider.GetRequiredService(p) as IFeatureDefinitionProvider) |
|||
.ToList(); |
|||
|
|||
foreach (var provider in providers) |
|||
{ |
|||
provider.Define(context); |
|||
} |
|||
} |
|||
|
|||
return context.Groups; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public abstract class FeatureDefinitionProvider : IFeatureDefinitionProvider, ISingletonDependency |
|||
{ |
|||
public abstract void Define(IFeatureDefinitionContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,111 @@ |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.Validation.StringValues; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureGroupDefinition |
|||
{ |
|||
/// <summary>
|
|||
/// Unique name of the group.
|
|||
/// </summary>
|
|||
public string Name { get; } |
|||
|
|||
public Dictionary<string, object> Properties { get; } |
|||
|
|||
public ILocalizableString DisplayName |
|||
{ |
|||
get => _displayName; |
|||
set => _displayName = Check.NotNull(value, nameof(value)); |
|||
} |
|||
private ILocalizableString _displayName; |
|||
|
|||
public IReadOnlyList<FeatureDefinition> Features => _features.ToImmutableList(); |
|||
private readonly List<FeatureDefinition> _features; |
|||
|
|||
/// <summary>
|
|||
/// Gets/sets a key-value on the <see cref="Properties"/>.
|
|||
/// </summary>
|
|||
/// <param name="name">Name of the property</param>
|
|||
/// <returns>
|
|||
/// Returns the value in the <see cref="Properties"/> dictionary by given <see cref="name"/>.
|
|||
/// Returns null if given <see cref="name"/> is not present in the <see cref="Properties"/> dictionary.
|
|||
/// </returns>
|
|||
public object this[string name] |
|||
{ |
|||
get => Properties.GetOrDefault(name); |
|||
set => Properties[name] = value; |
|||
} |
|||
|
|||
protected internal FeatureGroupDefinition( |
|||
string name, |
|||
ILocalizableString displayName = null) |
|||
{ |
|||
Name = name; |
|||
DisplayName = displayName ?? new FixedLocalizableString(Name); |
|||
|
|||
Properties = new Dictionary<string, object>(); |
|||
_features = new List<FeatureDefinition>(); |
|||
} |
|||
|
|||
public virtual FeatureDefinition AddFeature( |
|||
string name, |
|||
string defaultValue = null, |
|||
ILocalizableString displayName = null, |
|||
ILocalizableString description = null, |
|||
IStringValueType valueType = null, |
|||
bool isVisibleToClients = true) |
|||
{ |
|||
var feature = new FeatureDefinition( |
|||
name, |
|||
defaultValue, |
|||
displayName, |
|||
description, |
|||
valueType, |
|||
isVisibleToClients |
|||
); |
|||
|
|||
_features.Add(feature); |
|||
|
|||
return feature; |
|||
} |
|||
|
|||
public virtual List<FeatureDefinition> GetFeaturesWithChildren() |
|||
{ |
|||
var features = new List<FeatureDefinition>(); |
|||
|
|||
foreach (var feature in _features) |
|||
{ |
|||
AddFeatureToListRecursively(features, feature); |
|||
} |
|||
|
|||
return features; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets a property in the <see cref="Properties"/> dictionary.
|
|||
/// This is a shortcut for nested calls on this object.
|
|||
/// </summary>
|
|||
public virtual FeatureGroupDefinition WithProperty(string key, object value) |
|||
{ |
|||
Properties[key] = value; |
|||
return this; |
|||
} |
|||
|
|||
private void AddFeatureToListRecursively(List<FeatureDefinition> features, FeatureDefinition feature) |
|||
{ |
|||
features.Add(feature); |
|||
|
|||
foreach (var child in feature.Children) |
|||
{ |
|||
AddFeatureToListRecursively(features, child); |
|||
} |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"[{nameof(FeatureGroupDefinition)} {Name}]"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Aspects; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureInterceptor : AbpInterceptor, ITransientDependency |
|||
{ |
|||
private readonly IMethodInvocationFeatureCheckerService _methodInvocationFeatureCheckerService; |
|||
|
|||
public FeatureInterceptor( |
|||
IMethodInvocationFeatureCheckerService methodInvocationFeatureCheckerService) |
|||
{ |
|||
_methodInvocationFeatureCheckerService = methodInvocationFeatureCheckerService; |
|||
} |
|||
|
|||
public override void Intercept(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied( |
|||
invocation.TargetObject, |
|||
AbpCrossCuttingConcerns.FeatureChecking)) |
|||
{ |
|||
invocation.Proceed(); |
|||
return; |
|||
} |
|||
|
|||
AsyncHelper.RunSync(() => CheckFeaturesAsync(invocation)); |
|||
invocation.Proceed(); |
|||
} |
|||
|
|||
public override async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied( |
|||
invocation.TargetObject, |
|||
AbpCrossCuttingConcerns.FeatureChecking)) |
|||
{ |
|||
await invocation.ProceedAsync(); |
|||
return; |
|||
} |
|||
|
|||
AsyncHelper.RunSync(() => CheckFeaturesAsync(invocation)); |
|||
await invocation.ProceedAsync(); |
|||
} |
|||
|
|||
protected virtual Task CheckFeaturesAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
return _methodInvocationFeatureCheckerService.CheckAsync( |
|||
new MethodInvocationFeatureCheckerContext( |
|||
invocation.Method |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public static class FeatureInterceptorRegistrar |
|||
{ |
|||
public static void RegisterIfNeeded(IOnServiceRegistredContext context) |
|||
{ |
|||
if (ShouldIntercept(context.ImplementationType)) |
|||
{ |
|||
context.Interceptors.TryAdd<FeatureInterceptor>(); |
|||
} |
|||
} |
|||
|
|||
private static bool ShouldIntercept(Type type) |
|||
{ |
|||
return type.IsDefined(typeof(RequiresFeatureAttribute), true) || |
|||
AnyMethodHasRequiresFeatureAttribute(type); |
|||
} |
|||
|
|||
private static bool AnyMethodHasRequiresFeatureAttribute(Type implementationType) |
|||
{ |
|||
return implementationType |
|||
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) |
|||
.Any(HasRequiresFeatureAttribute); |
|||
} |
|||
|
|||
private static bool HasRequiresFeatureAttribute(MemberInfo methodInfo) |
|||
{ |
|||
return methodInfo.IsDefined(typeof(RequiresFeatureAttribute), true); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using Volo.Abp.Collections; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class FeatureOptions |
|||
{ |
|||
public ITypeList<IFeatureDefinitionProvider> DefinitionProviders { get; } |
|||
|
|||
public ITypeList<IFeatureValueProvider> ValueProviders { get; } |
|||
|
|||
public FeatureOptions() |
|||
{ |
|||
DefinitionProviders = new TypeList<IFeatureDefinitionProvider>(); |
|||
ValueProviders = new TypeList<IFeatureValueProvider>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
[Serializable] |
|||
public class FeatureValue : NameValue |
|||
{ |
|||
public FeatureValue() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public FeatureValue(string name, string value) |
|||
{ |
|||
Name = name; |
|||
Value = value; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public abstract class FeatureValueProvider : IFeatureValueProvider, ISingletonDependency |
|||
{ |
|||
public abstract string Name { get; } |
|||
|
|||
protected IFeatureStore FeatureStore { get; } |
|||
|
|||
protected FeatureValueProvider(IFeatureStore featureStore) |
|||
{ |
|||
FeatureStore = featureStore; |
|||
} |
|||
|
|||
public abstract Task<string> GetOrNullAsync(FeatureDefinition feature); |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IFeatureChecker |
|||
{ |
|||
Task<string> GetOrNullAsync([NotNull] string name); |
|||
|
|||
Task<bool> IsEnabledAsync(string name); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Localization; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IFeatureDefinitionContext |
|||
{ |
|||
FeatureGroupDefinition AddGroup([NotNull] string name, ILocalizableString displayName = null); |
|||
|
|||
FeatureGroupDefinition GetGroupOrNull(string name); |
|||
|
|||
void RemoveGroup(string name); |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using System.Collections.Generic; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IFeatureDefinitionManager |
|||
{ |
|||
[NotNull] |
|||
FeatureDefinition Get([NotNull] string name); |
|||
|
|||
IReadOnlyList<FeatureDefinition> GetAll(); |
|||
|
|||
FeatureDefinition GetOrNull(string name); |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IFeatureDefinitionProvider |
|||
{ |
|||
void Define(IFeatureDefinitionContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IFeatureStore |
|||
{ |
|||
Task<string> GetOrNullAsync( |
|||
[NotNull] string name, |
|||
[CanBeNull] string providerName, |
|||
[CanBeNull] string providerKey |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IFeatureValueProvider |
|||
{ |
|||
string Name { get; } |
|||
|
|||
Task<string> GetOrNullAsync([NotNull] FeatureDefinition feature); |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public interface IMethodInvocationFeatureCheckerService |
|||
{ |
|||
Task CheckAsync( |
|||
MethodInvocationFeatureCheckerContext context |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Reflection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class MethodInvocationFeatureCheckerContext |
|||
{ |
|||
public MethodInfo Method { get; } |
|||
|
|||
public MethodInvocationFeatureCheckerContext(MethodInfo method) |
|||
{ |
|||
Method = method; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class MethodInvocationFeatureCheckerService : IMethodInvocationFeatureCheckerService, ITransientDependency |
|||
{ |
|||
private readonly IFeatureChecker _featureChecker; |
|||
|
|||
public MethodInvocationFeatureCheckerService( |
|||
IFeatureChecker featureChecker) |
|||
{ |
|||
_featureChecker = featureChecker; |
|||
} |
|||
|
|||
public async Task CheckAsync(MethodInvocationFeatureCheckerContext context) |
|||
{ |
|||
if (IsFeatureCheckDisabled(context)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var requiresFeatureAttribute in GetRequiredFeatureAttributes(context.Method)) |
|||
{ |
|||
await _featureChecker.CheckEnabledAsync(requiresFeatureAttribute.RequiresAll, requiresFeatureAttribute.Features); |
|||
} |
|||
} |
|||
|
|||
protected virtual bool IsFeatureCheckDisabled(MethodInvocationFeatureCheckerContext context) |
|||
{ |
|||
return context.Method |
|||
.GetCustomAttributes(true) |
|||
.OfType<DisableFeatureCheckAttribute>() |
|||
.Any(); |
|||
} |
|||
|
|||
protected virtual IEnumerable<RequiresFeatureAttribute> GetRequiredFeatureAttributes(MethodInfo methodInfo) |
|||
{ |
|||
var attributes = methodInfo |
|||
.GetCustomAttributes(true) |
|||
.OfType<RequiresFeatureAttribute>(); |
|||
|
|||
if (methodInfo.IsPublic) |
|||
{ |
|||
attributes = attributes |
|||
.Union( |
|||
methodInfo.DeclaringType |
|||
.GetCustomAttributes(true) |
|||
.OfType<RequiresFeatureAttribute>() |
|||
); |
|||
} |
|||
|
|||
return attributes; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
[Dependency(TryRegister = true)] |
|||
public class NullFeatureStore : IFeatureStore, ISingletonDependency |
|||
{ |
|||
public ILogger<NullFeatureStore> Logger { get; set; } |
|||
|
|||
public NullFeatureStore() |
|||
{ |
|||
Logger = NullLogger<NullFeatureStore>.Instance; |
|||
} |
|||
|
|||
public Task<string> GetOrNullAsync(string name, string providerName, string providerKey) |
|||
{ |
|||
return Task.FromResult((string) null); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
/// <summary>
|
|||
/// This attribute can be used on a class/method to declare that given class/method is available
|
|||
/// only if required feature(s) are enabled.
|
|||
/// </summary>
|
|||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] |
|||
public class RequiresFeatureAttribute : Attribute |
|||
{ |
|||
/// <summary>
|
|||
/// A list of features to be checked if they are enabled.
|
|||
/// </summary>
|
|||
public string[] Features { get; } |
|||
|
|||
/// <summary>
|
|||
/// If this property is set to true, all of the <see cref="Features"/> must be enabled.
|
|||
/// If it's false, at least one of the <see cref="Features"/> must be enabled.
|
|||
/// Default: false.
|
|||
/// </summary>
|
|||
public bool RequiresAll { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Creates a new instance of <see cref="RequiresFeatureAttribute"/> class.
|
|||
/// </summary>
|
|||
/// <param name="features">A list of features to be checked if they are enabled</param>
|
|||
public RequiresFeatureAttribute(params string[] features) |
|||
{ |
|||
Features = features ?? Array.Empty<string>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.Features |
|||
{ |
|||
public class TenantFeatureValueProvider : FeatureValueProvider |
|||
{ |
|||
public const string ProviderName = "Tenant"; |
|||
|
|||
public override string Name => ProviderName; |
|||
|
|||
protected ICurrentTenant CurrentTenant { get; } |
|||
|
|||
public TenantFeatureValueProvider(IFeatureStore featureStore, ICurrentTenant currentTenant) |
|||
: base(featureStore) |
|||
{ |
|||
CurrentTenant = currentTenant; |
|||
} |
|||
|
|||
public override async Task<string> GetOrNullAsync(FeatureDefinition feature) |
|||
{ |
|||
return await FeatureStore.GetOrNullAsync(feature.Name, Name, CurrentTenant.Id?.ToString()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Validation.StringValues |
|||
{ |
|||
[Serializable] |
|||
[ValueValidator("NULL")] |
|||
public class AlwaysValidValueValidator : ValueValidatorBase |
|||
{ |
|||
public override bool IsValid(object value) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Validation.StringValues |
|||
{ |
|||
[Serializable] |
|||
[ValueValidator("BOOLEAN")] |
|||
public class BooleanValueValidator : ValueValidatorBase |
|||
{ |
|||
public override bool IsValid(object value) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
if (value is bool) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return bool.TryParse(value.ToString(), out _); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Validation.StringValues |
|||
{ |
|||
[Serializable] |
|||
[StringValueType("FREE_TEXT")] |
|||
public class FreeTextStringValueType : StringValueTypeBase |
|||
{ |
|||
public FreeTextStringValueType() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public FreeTextStringValueType(IValueValidator validator) |
|||
: base(validator) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue