mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[PM-1012] Feature access using context (#2764)
* Document online method * Feature accessors with context * Direct null assertion * Establish a constants class for flag keys
This commit is contained in:
@ -22,3 +22,8 @@ public static class AuthenticationSchemes
|
||||
{
|
||||
public const string BitwardenExternalCookieAuthenticationScheme = "bw.external";
|
||||
}
|
||||
|
||||
public static class FeatureFlagKeys
|
||||
{
|
||||
public const string SecretsManager = "secrets-manager";
|
||||
}
|
||||
|
@ -1,6 +1,39 @@
|
||||
namespace Bit.Core.Services;
|
||||
using Bit.Core.Context;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
public interface IFeatureService
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether online access to feature status is available.
|
||||
/// </summary>
|
||||
/// <returns>True if the service is online, otherwise false.</returns>
|
||||
bool IsOnline();
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given feature is enabled.
|
||||
/// </summary>
|
||||
/// <param name="key">The key of the feature to check.</param>
|
||||
/// <param name="currentContext">A context providing information that can be used to evaluate whether a feature should be on or off.</param>
|
||||
/// <param name="defaultValue">The default value for the feature.</param>
|
||||
/// <returns>True if the feature is enabled, otherwise false.</returns>
|
||||
bool IsEnabled(string key, ICurrentContext currentContext, bool defaultValue = false);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the integer variation of a feature.
|
||||
/// </summary>
|
||||
/// <param name="key">The key of the feature to check.</param>
|
||||
/// <param name="currentContext">A context providing information that can be used to evaluate the feature value.</param>
|
||||
/// <param name="defaultValue">The default value for the feature.</param>
|
||||
/// <returns>The feature variation value.</returns>
|
||||
int GetIntVariation(string key, ICurrentContext currentContext, int defaultValue = 0);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string variation of a feature.
|
||||
/// </summary>
|
||||
/// <param name="key">The key of the feature to check.</param>
|
||||
/// <param name="currentContext">A context providing information that can be used to evaluate the feature value.</param>
|
||||
/// <param name="defaultValue">The default value for the feature.</param>
|
||||
/// <returns>The feature variation value.</returns>
|
||||
string GetStringVariation(string key, ICurrentContext currentContext, string defaultValue = null);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Settings;
|
||||
using LaunchDarkly.Sdk.Server;
|
||||
using LaunchDarkly.Sdk.Server.Integrations;
|
||||
|
||||
@ -47,8 +48,44 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
|
||||
return _client.Initialized && !_client.IsOffline();
|
||||
}
|
||||
|
||||
public bool IsEnabled(string key, ICurrentContext currentContext, bool defaultValue = false)
|
||||
{
|
||||
return _client.BoolVariation(key, BuildContext(currentContext), defaultValue);
|
||||
}
|
||||
|
||||
public int GetIntVariation(string key, ICurrentContext currentContext, int defaultValue = 0)
|
||||
{
|
||||
return _client.IntVariation(key, BuildContext(currentContext), defaultValue);
|
||||
}
|
||||
|
||||
public string GetStringVariation(string key, ICurrentContext currentContext, string defaultValue = null)
|
||||
{
|
||||
return _client.StringVariation(key, BuildContext(currentContext), defaultValue);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client?.Dispose();
|
||||
}
|
||||
|
||||
private LaunchDarkly.Sdk.Context BuildContext(ICurrentContext currentContext)
|
||||
{
|
||||
var builder = LaunchDarkly.Sdk.Context.MultiBuilder();
|
||||
|
||||
if (currentContext.UserId.HasValue)
|
||||
{
|
||||
var user = LaunchDarkly.Sdk.Context.Builder(currentContext.UserId.Value.ToString());
|
||||
user.Kind(LaunchDarkly.Sdk.ContextKind.Default);
|
||||
builder.Add(user.Build());
|
||||
}
|
||||
|
||||
if (currentContext.OrganizationId.HasValue)
|
||||
{
|
||||
var org = LaunchDarkly.Sdk.Context.Builder(currentContext.OrganizationId.Value.ToString());
|
||||
org.Kind("org");
|
||||
builder.Add(org.Build());
|
||||
}
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user