1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

Establish IFeatureService as scoped (#3679)

* Establish IFeatureService as scoped

* Lint

* Feedback around injection
This commit is contained in:
Matt Bishop
2024-01-18 09:47:34 -05:00
committed by GitHub
parent cd006f3779
commit 974d23efdd
26 changed files with 96 additions and 131 deletions

View File

@ -56,7 +56,7 @@ public class CollectionService : ICollectionService
var usersList = users?.ToList();
// If using Flexible Collections - a collection should always have someone with Can Manage permissions
if (_featureService.IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1, _currentContext))
if (_featureService.IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1))
{
var groupHasManageAccess = groupsList?.Any(g => g.Manage) ?? false;
var userHasManageAccess = usersList?.Any(u => u.Manage) ?? false;
@ -124,7 +124,7 @@ public class CollectionService : ICollectionService
{
var collections = await _collectionRepository.GetManyByUserIdAsync(
_currentContext.UserId.Value,
_featureService.IsEnabled(FeatureFlagKeys.FlexibleCollections, _currentContext)
_featureService.IsEnabled(FeatureFlagKeys.FlexibleCollections)
);
orgCollections = collections.Where(c => c.OrganizationId == organizationId);
}

View File

@ -4,16 +4,25 @@ using Bit.Core.Utilities;
using LaunchDarkly.Logging;
using LaunchDarkly.Sdk.Server;
using LaunchDarkly.Sdk.Server.Integrations;
using LaunchDarkly.Sdk.Server.Interfaces;
namespace Bit.Core.Services;
public class LaunchDarklyFeatureService : IFeatureService, IDisposable
public class LaunchDarklyFeatureService : IFeatureService
{
private readonly LdClient _client;
private readonly ILdClient _client;
private readonly ICurrentContext _currentContext;
private const string _anonymousUser = "25a15cac-58cf-4ac0-ad0f-b17c4bd92294";
public LaunchDarklyFeatureService(
IGlobalSettings globalSettings)
ILdClient client,
ICurrentContext currentContext)
{
_client = client;
_currentContext = currentContext;
}
public static Configuration GetConfiguredClient(GlobalSettings globalSettings)
{
var ldConfig = Configuration.Builder(globalSettings.LaunchDarkly?.SdkKey);
ldConfig.Logging(Components.Logging().Level(LogLevel.Error));
@ -64,7 +73,7 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
ldConfig.Offline(true);
}
_client = new LdClient(ldConfig.Build());
return ldConfig.Build();
}
public bool IsOnline()
@ -72,28 +81,28 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
return _client.Initialized && !_client.IsOffline();
}
public bool IsEnabled(string key, ICurrentContext currentContext, bool defaultValue = false)
public bool IsEnabled(string key, bool defaultValue = false)
{
return _client.BoolVariation(key, BuildContext(currentContext), defaultValue);
return _client.BoolVariation(key, BuildContext(), defaultValue);
}
public int GetIntVariation(string key, ICurrentContext currentContext, int defaultValue = 0)
public int GetIntVariation(string key, int defaultValue = 0)
{
return _client.IntVariation(key, BuildContext(currentContext), defaultValue);
return _client.IntVariation(key, BuildContext(), defaultValue);
}
public string GetStringVariation(string key, ICurrentContext currentContext, string defaultValue = null)
public string GetStringVariation(string key, string defaultValue = null)
{
return _client.StringVariation(key, BuildContext(currentContext), defaultValue);
return _client.StringVariation(key, BuildContext(), defaultValue);
}
public Dictionary<string, object> GetAll(ICurrentContext currentContext)
public Dictionary<string, object> GetAll()
{
var results = new Dictionary<string, object>();
var keys = FeatureFlagKeys.GetAllKeys();
var values = _client.AllFlagsState(BuildContext(currentContext));
var values = _client.AllFlagsState(BuildContext());
if (values.Valid)
{
foreach (var key in keys)
@ -119,23 +128,18 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
return results;
}
public void Dispose()
{
_client?.Dispose();
}
private LaunchDarkly.Sdk.Context BuildContext(ICurrentContext currentContext)
private LaunchDarkly.Sdk.Context BuildContext()
{
var builder = LaunchDarkly.Sdk.Context.MultiBuilder();
switch (currentContext.ClientType)
switch (_currentContext.ClientType)
{
case Identity.ClientType.User:
{
LaunchDarkly.Sdk.ContextBuilder ldUser;
if (currentContext.UserId.HasValue)
if (_currentContext.UserId.HasValue)
{
ldUser = LaunchDarkly.Sdk.Context.Builder(currentContext.UserId.Value.ToString());
ldUser = LaunchDarkly.Sdk.Context.Builder(_currentContext.UserId.Value.ToString());
}
else
{
@ -146,9 +150,9 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
ldUser.Kind(LaunchDarkly.Sdk.ContextKind.Default);
if (currentContext.Organizations?.Any() ?? false)
if (_currentContext.Organizations?.Any() ?? false)
{
var ldOrgs = currentContext.Organizations.Select(o => LaunchDarkly.Sdk.LdValue.Of(o.Id.ToString()));
var ldOrgs = _currentContext.Organizations.Select(o => LaunchDarkly.Sdk.LdValue.Of(o.Id.ToString()));
ldUser.Set("organizations", LaunchDarkly.Sdk.LdValue.ArrayFrom(ldOrgs));
}
@ -158,9 +162,9 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
case Identity.ClientType.Organization:
{
if (currentContext.OrganizationId.HasValue)
if (_currentContext.OrganizationId.HasValue)
{
var ldOrg = LaunchDarkly.Sdk.Context.Builder(currentContext.OrganizationId.Value.ToString());
var ldOrg = LaunchDarkly.Sdk.Context.Builder(_currentContext.OrganizationId.Value.ToString());
ldOrg.Kind("organization");
builder.Add(ldOrg.Build());
}
@ -169,16 +173,16 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
case Identity.ClientType.ServiceAccount:
{
if (currentContext.UserId.HasValue)
if (_currentContext.UserId.HasValue)
{
var ldServiceAccount = LaunchDarkly.Sdk.Context.Builder(currentContext.UserId.Value.ToString());
var ldServiceAccount = LaunchDarkly.Sdk.Context.Builder(_currentContext.UserId.Value.ToString());
ldServiceAccount.Kind("service-account");
builder.Add(ldServiceAccount.Build());
}
if (currentContext.OrganizationId.HasValue)
if (_currentContext.OrganizationId.HasValue)
{
var ldOrg = LaunchDarkly.Sdk.Context.Builder(currentContext.OrganizationId.Value.ToString());
var ldOrg = LaunchDarkly.Sdk.Context.Builder(_currentContext.OrganizationId.Value.ToString());
ldOrg.Kind("organization");
builder.Add(ldOrg.Build());
}
@ -189,7 +193,7 @@ public class LaunchDarklyFeatureService : IFeatureService, IDisposable
return builder.Build();
}
private TestData BuildDataSource(Dictionary<string, string> values)
private static TestData BuildDataSource(Dictionary<string, string> values)
{
var source = TestData.DataSource();
foreach (var kvp in values)