|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +// |
| 4 | + |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | +using Microsoft.AspNetCore.Http.HttpResults; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace Microsoft.FeatureManagement.AspNetCore |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// An endpoint filter that controls access based on feature flag states. |
| 17 | + /// </summary> |
| 18 | + internal sealed class FeatureGateEndpointFilter : IEndpointFilter |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Gets the collection of feature flags to evaluate. |
| 22 | + /// </summary> |
| 23 | + private readonly IEnumerable<string> _features; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the type of requirement (All or Any) for feature evaluation. |
| 27 | + /// </summary> |
| 28 | + private readonly RequirementType _requirementType; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets whether the feature evaluation result should be negated. |
| 32 | + /// </summary> |
| 33 | + private readonly bool _negate; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Initializes a new instance of the <see cref="FeatureGateEndpointFilter"/> class. |
| 37 | + /// </summary> |
| 38 | + /// <param name="features">The collection of feature flags to evaluate.</param> |
| 39 | + /// <exception cref="ArgumentNullException">Thrown when features collection is null or empty.</exception> |
| 40 | + public FeatureGateEndpointFilter(params string[] features) |
| 41 | + : this(RequirementType.All, negate: false, features) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Creates a new instance of the <see cref="FeatureGateEndpointFilter"/> class. |
| 47 | + /// </summary> |
| 48 | + /// <param name="requirementType">Specifies whether all or any of the provided features should be enabled in order to pass.</param> |
| 49 | + /// <param name="features">The collection of feature flags to evaluate.</param> |
| 50 | + /// <exception cref="ArgumentNullException">Thrown when features collection is null or empty.</exception> |
| 51 | + public FeatureGateEndpointFilter(RequirementType requirementType, params string[] features) |
| 52 | + : this(requirementType, negate: false, features) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Creates a new instance of the <see cref="FeatureGateEndpointFilter"/> class. |
| 58 | + /// </summary> |
| 59 | + /// <param name="requirementType">Specifies whether all or any of the provided features should be enabled in order to pass.</param> |
| 60 | + /// <param name="negate">Specifies whether the feature evaluation result should be negated.</param> |
| 61 | + /// <param name="features">The collection of feature flags to evaluate.</param> |
| 62 | + /// <exception cref="ArgumentNullException">Thrown when features collection is null or empty.</exception> |
| 63 | + public FeatureGateEndpointFilter(RequirementType requirementType, bool negate, params string[] features) |
| 64 | + { |
| 65 | + if (features == null || features.Length == 0) |
| 66 | + { |
| 67 | + throw new ArgumentNullException(nameof(features)); |
| 68 | + } |
| 69 | + |
| 70 | + _features = features; |
| 71 | + _requirementType = requirementType; |
| 72 | + _negate = negate; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Invokes the feature flag filter to control endpoint access based on feature states. |
| 77 | + /// </summary> |
| 78 | + /// <param name="context">The endpoint filter invocation context.</param> |
| 79 | + /// <param name="next">The delegate representing the next filter in the pipeline.</param> |
| 80 | + /// <returns> |
| 81 | + /// A <see cref="NotFound"/> result if access is denied, otherwise continues the pipeline. |
| 82 | + /// </returns> |
| 83 | + public async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) |
| 84 | + { |
| 85 | + IVariantFeatureManager fm = context.HttpContext.RequestServices.GetRequiredService<IVariantFeatureManagerSnapshot>(); |
| 86 | + |
| 87 | + bool enabled = _requirementType == RequirementType.All |
| 88 | + ? await _features.All(async feature => await fm.IsEnabledAsync(feature).ConfigureAwait(false)) |
| 89 | + : await _features.Any(async feature => await fm.IsEnabledAsync(feature).ConfigureAwait(false)); |
| 90 | + |
| 91 | + bool isAllowed = _negate ? !enabled : enabled; |
| 92 | + |
| 93 | + return isAllowed |
| 94 | + ? await next(context).ConfigureAwait(false) |
| 95 | + : Results.NotFound(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments