1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00
bitwarden-server/src/Core/Utilities/RequireFeatureAttribute.cs
Matt Bishop 974d23efdd
Establish IFeatureService as scoped (#3679)
* Establish IFeatureService as scoped

* Lint

* Feedback around injection
2024-01-18 09:47:34 -05:00

35 lines
1.2 KiB
C#

using Bit.Core.Exceptions;
using Bit.Core.Services;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Core.Utilities;
/// <summary>
/// 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 <see cref="FeatureUnavailableException"/> is thrown
/// </summary>
public class RequireFeatureAttribute : ActionFilterAttribute
{
private readonly string _featureFlagKey;
/// <summary>
/// Initializes a new instance of the <see cref="RequireFeatureAttribute"/> class with the specified feature flag key.
/// </summary>
/// <param name="featureFlagKey">The name of the feature flag to require.</param>
public RequireFeatureAttribute(string featureFlagKey)
{
_featureFlagKey = featureFlagKey;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var featureService = context.HttpContext.RequestServices.GetRequiredService<IFeatureService>();
if (!featureService.IsEnabled(_featureFlagKey))
{
throw new FeatureUnavailableException();
}
}
}