Skip to content

Commit

Permalink
Add self-host eligibility to organization metadata (#4867)
Browse files Browse the repository at this point in the history
  • Loading branch information
amorask-bitwarden authored Oct 21, 2024
1 parent 2c4dd3e commit a210917
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Bit.Api.Billing.Models.Responses;

public record OrganizationMetadataResponse(
bool IsEligibleForSelfHost,
bool IsOnSecretsManagerStandalone)
{
public static OrganizationMetadataResponse From(OrganizationMetadata metadata)
=> new(metadata.IsOnSecretsManagerStandalone);
=> new(
metadata.IsEligibleForSelfHost,
metadata.IsOnSecretsManagerStandalone);
}
4 changes: 3 additions & 1 deletion src/Core/Billing/Models/OrganizationMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Bit.Core.Billing.Models;

public record OrganizationMetadata(
bool IsEligibleForSelfHost,
bool IsOnSecretsManagerStandalone)
{
public static OrganizationMetadata Default() => new(
IsOnSecretsManagerStandalone: default);
IsEligibleForSelfHost: false,
IsOnSecretsManagerStandalone: false);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Billing.Caches;
using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Models;
Expand Down Expand Up @@ -26,6 +27,7 @@ public class OrganizationBillingService(
IGlobalSettings globalSettings,
ILogger<OrganizationBillingService> logger,
IOrganizationRepository organizationRepository,
IProviderRepository providerRepository,
ISetupIntentCache setupIntentCache,
IStripeAdapter stripeAdapter,
ISubscriberService subscriberService) : IOrganizationBillingService
Expand Down Expand Up @@ -69,14 +71,11 @@ public async Task Finalize(OrganizationSale sale)

var subscription = await subscriberService.GetSubscription(organization);

if (customer == null || subscription == null)
{
return OrganizationMetadata.Default();
}
var isEligibleForSelfHost = await IsEligibleForSelfHost(organization, subscription);

var isOnSecretsManagerStandalone = IsOnSecretsManagerStandalone(organization, customer, subscription);

return new OrganizationMetadata(isOnSecretsManagerStandalone);
return new OrganizationMetadata(isEligibleForSelfHost, isOnSecretsManagerStandalone);
}

public async Task UpdatePaymentMethod(
Expand Down Expand Up @@ -340,11 +339,38 @@ private async Task<Subscription> CreateSubscriptionAsync(
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(
Organization organization,
Customer customer,
Subscription subscription)
Customer? customer,
Subscription? subscription)
{
if (customer == null || subscription == null)
{
return false;
}

var plan = StaticStore.GetPlan(organization.PlanType);

if (!plan.SupportsSecretsManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public async Task GetMetadataAsync_OK(
{
sutProvider.GetDependency<ICurrentContext>().AccessMembersTab(organizationId).Returns(true);
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId)
.Returns(new OrganizationMetadata(true));
.Returns(new OrganizationMetadata(true, true));

var result = await sutProvider.Sut.GetMetadataAsync(organizationId);

Assert.IsType<Ok<OrganizationMetadataResponse>>(result);

var organizationMetadataResponse = ((Ok<OrganizationMetadataResponse>)result).Value;

Assert.True(organizationMetadataResponse.IsEligibleForSelfHost);
Assert.True(organizationMetadataResponse.IsOnSecretsManagerStandalone);
}

Expand Down

0 comments on commit a210917

Please sign in to comment.