using Bit.Core.Exceptions; using Bit.Core.Services; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; namespace Bit.Core.Utilities; /// /// Specifies that the class or method that this attribute is applied to requires the specified boolean feature flag /// to be enabled. If the feature flag is not enabled, a is thrown /// public class RequireFeatureAttribute : ActionFilterAttribute { private readonly string _featureFlagKey; /// /// Initializes a new instance of the class with the specified feature flag key. /// /// The name of the feature flag to require. public RequireFeatureAttribute(string featureFlagKey) { _featureFlagKey = featureFlagKey; } public override void OnActionExecuting(ActionExecutingContext context) { var featureService = context.HttpContext.RequestServices.GetRequiredService(); if (!featureService.IsEnabled(_featureFlagKey)) { throw new FeatureUnavailableException(); } } }