1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +01:00

Add self-host eligibility to organization metadata (#4867)

This commit is contained in:
Alex Morask 2024-10-21 14:02:21 -04:00 committed by GitHub
parent 2c4dd3ea12
commit a2109175b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 10 deletions

View File

@ -3,8 +3,11 @@
namespace Bit.Api.Billing.Models.Responses; namespace Bit.Api.Billing.Models.Responses;
public record OrganizationMetadataResponse( public record OrganizationMetadataResponse(
bool IsEligibleForSelfHost,
bool IsOnSecretsManagerStandalone) bool IsOnSecretsManagerStandalone)
{ {
public static OrganizationMetadataResponse From(OrganizationMetadata metadata) public static OrganizationMetadataResponse From(OrganizationMetadata metadata)
=> new(metadata.IsOnSecretsManagerStandalone); => new(
metadata.IsEligibleForSelfHost,
metadata.IsOnSecretsManagerStandalone);
} }

View File

@ -1,8 +1,10 @@
namespace Bit.Core.Billing.Models; namespace Bit.Core.Billing.Models;
public record OrganizationMetadata( public record OrganizationMetadata(
bool IsEligibleForSelfHost,
bool IsOnSecretsManagerStandalone) bool IsOnSecretsManagerStandalone)
{ {
public static OrganizationMetadata Default() => new( public static OrganizationMetadata Default() => new(
IsOnSecretsManagerStandalone: default); IsEligibleForSelfHost: false,
IsOnSecretsManagerStandalone: false);
} }

View File

@ -1,4 +1,5 @@
using Bit.Core.AdminConsole.Entities; using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Billing.Caches; using Bit.Core.Billing.Caches;
using Bit.Core.Billing.Constants; using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Models; using Bit.Core.Billing.Models;
@ -26,6 +27,7 @@ public class OrganizationBillingService(
IGlobalSettings globalSettings, IGlobalSettings globalSettings,
ILogger<OrganizationBillingService> logger, ILogger<OrganizationBillingService> logger,
IOrganizationRepository organizationRepository, IOrganizationRepository organizationRepository,
IProviderRepository providerRepository,
ISetupIntentCache setupIntentCache, ISetupIntentCache setupIntentCache,
IStripeAdapter stripeAdapter, IStripeAdapter stripeAdapter,
ISubscriberService subscriberService) : IOrganizationBillingService ISubscriberService subscriberService) : IOrganizationBillingService
@ -69,14 +71,11 @@ public class OrganizationBillingService(
var subscription = await subscriberService.GetSubscription(organization); var subscription = await subscriberService.GetSubscription(organization);
if (customer == null || subscription == null) var isEligibleForSelfHost = await IsEligibleForSelfHost(organization, subscription);
{
return OrganizationMetadata.Default();
}
var isOnSecretsManagerStandalone = IsOnSecretsManagerStandalone(organization, customer, subscription); var isOnSecretsManagerStandalone = IsOnSecretsManagerStandalone(organization, customer, subscription);
return new OrganizationMetadata(isOnSecretsManagerStandalone); return new OrganizationMetadata(isEligibleForSelfHost, isOnSecretsManagerStandalone);
} }
public async Task UpdatePaymentMethod( public async Task UpdatePaymentMethod(
@ -340,11 +339,38 @@ public class OrganizationBillingService(
return await stripeAdapter.SubscriptionCreateAsync(subscriptionCreateOptions); return await stripeAdapter.SubscriptionCreateAsync(subscriptionCreateOptions);
} }
private async Task<bool> IsEligibleForSelfHost(
Organization organization,
Subscription? organizationSubscription)
{
if (organization.Status != OrganizationStatusType.Managed)
{
return organization.Plan.Contains("Families") ||
organization.Plan.Contains("Enterprise") && IsActive(organizationSubscription);
}
var provider = await providerRepository.GetByOrganizationIdAsync(organization.Id);
var providerSubscription = await subscriberService.GetSubscriptionOrThrow(provider);
return organization.Plan.Contains("Enterprise") && IsActive(providerSubscription);
bool IsActive(Subscription? subscription) => subscription?.Status is
StripeConstants.SubscriptionStatus.Active or
StripeConstants.SubscriptionStatus.Trialing or
StripeConstants.SubscriptionStatus.PastDue;
}
private static bool IsOnSecretsManagerStandalone( private static bool IsOnSecretsManagerStandalone(
Organization organization, Organization organization,
Customer customer, Customer? customer,
Subscription subscription) Subscription? subscription)
{ {
if (customer == null || subscription == null)
{
return false;
}
var plan = StaticStore.GetPlan(organization.PlanType); var plan = StaticStore.GetPlan(organization.PlanType);
if (!plan.SupportsSecretsManager) if (!plan.SupportsSecretsManager)

View File

@ -52,7 +52,7 @@ public class OrganizationBillingControllerTests
{ {
sutProvider.GetDependency<ICurrentContext>().AccessMembersTab(organizationId).Returns(true); sutProvider.GetDependency<ICurrentContext>().AccessMembersTab(organizationId).Returns(true);
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId) sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId)
.Returns(new OrganizationMetadata(true)); .Returns(new OrganizationMetadata(true, true));
var result = await sutProvider.Sut.GetMetadataAsync(organizationId); var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
@ -60,6 +60,7 @@ public class OrganizationBillingControllerTests
var organizationMetadataResponse = ((Ok<OrganizationMetadataResponse>)result).Value; var organizationMetadataResponse = ((Ok<OrganizationMetadataResponse>)result).Value;
Assert.True(organizationMetadataResponse.IsEligibleForSelfHost);
Assert.True(organizationMetadataResponse.IsOnSecretsManagerStandalone); Assert.True(organizationMetadataResponse.IsOnSecretsManagerStandalone);
} }