From 186afbc162edd437661d38b0e8e31b21103d1436 Mon Sep 17 00:00:00 2001 From: Alex Morask <144709477+amorask-bitwarden@users.noreply.github.com> Date: Thu, 25 Apr 2024 15:27:00 -0400 Subject: [PATCH] Updated CB to use both flag and provider status. (#4005) --- .../Providers/CreateProviderCommand.cs | 4 +-- .../AdminConsole/Services/ProviderService.cs | 5 ++- .../Services/ProviderServiceTests.cs | 6 ++++ .../Controllers/ProvidersController.cs | 32 ++++++++++++------- .../AdminConsole/Views/Providers/Edit.cshtml | 4 +-- .../Providers/ProfileProviderResponseModel.cs | 2 ++ 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/bitwarden_license/src/Commercial.Core/AdminConsole/Providers/CreateProviderCommand.cs b/bitwarden_license/src/Commercial.Core/AdminConsole/Providers/CreateProviderCommand.cs index 720317578..1f96202d8 100644 --- a/bitwarden_license/src/Commercial.Core/AdminConsole/Providers/CreateProviderCommand.cs +++ b/bitwarden_license/src/Commercial.Core/AdminConsole/Providers/CreateProviderCommand.cs @@ -40,7 +40,6 @@ public class CreateProviderCommand : ICreateProviderCommand public async Task CreateMspAsync(Provider provider, string ownerEmail, int teamsMinimumSeats, int enterpriseMinimumSeats) { - var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); var owner = await _userRepository.GetByEmailAsync(ownerEmail); if (owner == null) { @@ -57,6 +56,8 @@ public class CreateProviderCommand : ICreateProviderCommand Status = ProviderUserStatusType.Confirmed, }; + var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); + if (isConsolidatedBillingEnabled) { var providerPlans = new List @@ -73,7 +74,6 @@ public class CreateProviderCommand : ICreateProviderCommand await _providerUserRepository.CreateAsync(providerUser); await _providerService.SendProviderSetupInviteEmailAsync(provider, owner.Email); - } public async Task CreateResellerAsync(Provider provider) diff --git a/bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs b/bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs index 503d290f6..516e43420 100644 --- a/bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs +++ b/bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs @@ -7,6 +7,7 @@ using Bit.Core.AdminConsole.Models.Business.Provider; using Bit.Core.AdminConsole.Models.Business.Tokenables; using Bit.Core.AdminConsole.Repositories; using Bit.Core.AdminConsole.Services; +using Bit.Core.Billing.Extensions; using Bit.Core.Context; using Bit.Core.Entities; using Bit.Core.Enums; @@ -518,7 +519,9 @@ public class ProviderService : IProviderService public async Task CreateOrganizationAsync(Guid providerId, OrganizationSignup organizationSignup, string clientOwnerEmail, User user) { - var consolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); + var provider = await _providerRepository.GetByIdAsync(providerId); + + var consolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling) && provider.IsBillable(); ThrowOnInvalidPlanType(organizationSignup.Plan, consolidatedBillingEnabled); diff --git a/bitwarden_license/test/Commercial.Core.Test/AdminConsole/Services/ProviderServiceTests.cs b/bitwarden_license/test/Commercial.Core.Test/AdminConsole/Services/ProviderServiceTests.cs index ac216ce8f..274af2d9d 100644 --- a/bitwarden_license/test/Commercial.Core.Test/AdminConsole/Services/ProviderServiceTests.cs +++ b/bitwarden_license/test/Commercial.Core.Test/AdminConsole/Services/ProviderServiceTests.cs @@ -652,6 +652,9 @@ public class ProviderServiceTests { sutProvider.GetDependency().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling).Returns(true); + provider.Type = ProviderType.Msp; + provider.Status = ProviderStatusType.Billable; + organizationSignup.Plan = PlanType.EnterpriseAnnually; sutProvider.GetDependency().GetByIdAsync(provider.Id).Returns(provider); @@ -678,6 +681,9 @@ public class ProviderServiceTests { sutProvider.GetDependency().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling).Returns(true); + provider.Type = ProviderType.Msp; + provider.Status = ProviderStatusType.Billable; + organizationSignup.Plan = PlanType.EnterpriseMonthly; sutProvider.GetDependency().GetByIdAsync(provider.Id).Returns(provider); diff --git a/src/Admin/AdminConsole/Controllers/ProvidersController.cs b/src/Admin/AdminConsole/Controllers/ProvidersController.cs index 408fc5d31..bfb19e1cd 100644 --- a/src/Admin/AdminConsole/Controllers/ProvidersController.cs +++ b/src/Admin/AdminConsole/Controllers/ProvidersController.cs @@ -10,6 +10,7 @@ using Bit.Core.AdminConsole.Providers.Interfaces; using Bit.Core.AdminConsole.Repositories; using Bit.Core.AdminConsole.Services; using Bit.Core.Billing.Entities; +using Bit.Core.Billing.Extensions; using Bit.Core.Billing.Repositories; using Bit.Core.Exceptions; using Bit.Core.Repositories; @@ -149,7 +150,6 @@ public class ProvidersController : Controller [SelfHosted(NotSelfHostedOnly = true)] public async Task Edit(Guid id) { - var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); var provider = await _providerRepository.GetByIdAsync(id); if (provider == null) { @@ -158,12 +158,16 @@ public class ProvidersController : Controller var users = await _providerUserRepository.GetManyDetailsByProviderAsync(id); var providerOrganizations = await _providerOrganizationRepository.GetManyDetailsByProviderAsync(id); - if (isConsolidatedBillingEnabled) + + var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); + + if (!isConsolidatedBillingEnabled || !provider.IsBillable()) { - var providerPlan = await _providerPlanRepository.GetByProviderId(id); - return View(new ProviderEditModel(provider, users, providerOrganizations, providerPlan)); + return View(new ProviderEditModel(provider, users, providerOrganizations, new List())); } - return View(new ProviderEditModel(provider, users, providerOrganizations, new List())); + + var providerPlan = await _providerPlanRepository.GetByProviderId(id); + return View(new ProviderEditModel(provider, users, providerOrganizations, providerPlan)); } [HttpPost] @@ -172,7 +176,6 @@ public class ProvidersController : Controller [RequirePermission(Permission.Provider_Edit)] public async Task Edit(Guid id, ProviderEditModel model) { - var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); var providerPlans = await _providerPlanRepository.GetByProviderId(id); var provider = await _providerRepository.GetByIdAsync(id); if (provider == null) @@ -183,13 +186,18 @@ public class ProvidersController : Controller model.ToProvider(provider); await _providerRepository.ReplaceAsync(provider); await _applicationCacheService.UpsertProviderAbilityAsync(provider); - if (isConsolidatedBillingEnabled) + + var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling); + + if (!isConsolidatedBillingEnabled || !provider.IsBillable()) { - model.ToProviderPlan(providerPlans); - foreach (var providerPlan in providerPlans) - { - await _providerPlanRepository.ReplaceAsync(providerPlan); - } + return RedirectToAction("Edit", new { id }); + } + + model.ToProviderPlan(providerPlans); + foreach (var providerPlan in providerPlans) + { + await _providerPlanRepository.ReplaceAsync(providerPlan); } return RedirectToAction("Edit", new { id }); diff --git a/src/Admin/AdminConsole/Views/Providers/Edit.cshtml b/src/Admin/AdminConsole/Views/Providers/Edit.cshtml index f95fdc46e..8572c361a 100644 --- a/src/Admin/AdminConsole/Views/Providers/Edit.cshtml +++ b/src/Admin/AdminConsole/Views/Providers/Edit.cshtml @@ -1,6 +1,6 @@ @using Bit.Admin.Enums; @using Bit.Core -@using Bit.Core.AdminConsole.Enums.Provider +@using Bit.Core.Billing.Extensions @inject Bit.Admin.Services.IAccessControlService AccessControlService @inject Bit.Core.Services.IFeatureService FeatureService @@ -43,7 +43,7 @@ - @if (FeatureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling) && Model.Provider.Type == ProviderType.Msp) + @if (FeatureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling) && Model.Provider.IsBillable()) {
diff --git a/src/Api/AdminConsole/Models/Response/Providers/ProfileProviderResponseModel.cs b/src/Api/AdminConsole/Models/Response/Providers/ProfileProviderResponseModel.cs index 9e6927b9e..bdfec2624 100644 --- a/src/Api/AdminConsole/Models/Response/Providers/ProfileProviderResponseModel.cs +++ b/src/Api/AdminConsole/Models/Response/Providers/ProfileProviderResponseModel.cs @@ -21,6 +21,7 @@ public class ProfileProviderResponseModel : ResponseModel Permissions = CoreHelpers.LoadClassFromJsonData(provider.Permissions); UserId = provider.UserId; UseEvents = provider.UseEvents; + ProviderStatus = provider.ProviderStatus; } public Guid Id { get; set; } @@ -33,4 +34,5 @@ public class ProfileProviderResponseModel : ResponseModel public Permissions Permissions { get; set; } public Guid? UserId { get; set; } public bool UseEvents { get; set; } + public ProviderStatusType ProviderStatus { get; set; } }