2023-10-19 01:27:56 +10:00
|
|
|
|
using Bit.Api.AdminConsole.Models.Request.Providers;
|
|
|
|
|
|
using Bit.Api.AdminConsole.Models.Response.Providers;
|
2024-04-10 14:10:53 -04:00
|
|
|
|
using Bit.Core;
|
2023-10-27 03:38:29 +10:00
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
|
|
|
|
using Bit.Core.AdminConsole.Services;
|
2024-05-23 10:17:00 -04:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
2024-04-10 14:10:53 -04:00
|
|
|
|
using Bit.Core.Models.Business;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
using Bit.Core.Services;
|
2021-07-15 16:37:27 +02:00
|
|
|
|
using Bit.Core.Settings;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
2023-10-19 01:27:56 +10:00
|
|
|
|
namespace Bit.Api.AdminConsole.Controllers;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
[Route("providers")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class ProvidersController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IProviderRepository _providerRepository;
|
|
|
|
|
|
private readonly IProviderService _providerService;
|
|
|
|
|
|
private readonly ICurrentContext _currentContext;
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2024-04-10 14:10:53 -04:00
|
|
|
|
private readonly IFeatureService _featureService;
|
|
|
|
|
|
private readonly ILogger<ProvidersController> _logger;
|
2024-05-23 10:17:00 -04:00
|
|
|
|
private readonly IProviderBillingService _providerBillingService;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
public ProvidersController(IUserService userService, IProviderRepository providerRepository,
|
2024-04-10 14:10:53 -04:00
|
|
|
|
IProviderService providerService, ICurrentContext currentContext, GlobalSettings globalSettings,
|
2024-05-23 10:17:00 -04:00
|
|
|
|
IFeatureService featureService, ILogger<ProvidersController> logger,
|
|
|
|
|
|
IProviderBillingService providerBillingService)
|
2021-06-30 09:35:26 +02:00
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_providerRepository = providerRepository;
|
|
|
|
|
|
_providerService = providerService;
|
|
|
|
|
|
_currentContext = currentContext;
|
2021-07-15 16:37:27 +02:00
|
|
|
|
_globalSettings = globalSettings;
|
2024-04-10 14:10:53 -04:00
|
|
|
|
_featureService = featureService;
|
|
|
|
|
|
_logger = logger;
|
2024-05-23 10:17:00 -04:00
|
|
|
|
_providerBillingService = providerBillingService;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2021-06-30 09:35:26 +02:00
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id:guid}")]
|
|
|
|
|
|
public async Task<ProviderResponseModel> Get(Guid id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-07-15 16:37:27 +02:00
|
|
|
|
if (!_currentContext.ProviderUser(id))
|
2021-06-30 09:35:26 +02:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new ProviderResponseModel(provider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-15 16:37:27 +02:00
|
|
|
|
[HttpPut("{id:guid}")]
|
|
|
|
|
|
[HttpPost("{id:guid}")]
|
2021-06-30 09:35:26 +02:00
|
|
|
|
public async Task<ProviderResponseModel> Put(Guid id, [FromBody] ProviderUpdateRequestModel model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-06-30 09:35:26 +02:00
|
|
|
|
if (!_currentContext.ProviderProviderAdmin(id))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-06-30 09:35:26 +02:00
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-07-15 16:37:27 +02:00
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _providerService.UpdateAsync(model.ToProvider(provider, _globalSettings));
|
|
|
|
|
|
return new ProviderResponseModel(provider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("{id:guid}/setup")]
|
|
|
|
|
|
public async Task<ProviderResponseModel> Setup(Guid id, [FromBody] ProviderSetupRequestModel model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-07-15 16:37:27 +02:00
|
|
|
|
if (!_currentContext.ProviderProviderAdmin(id))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-07-15 16:37:27 +02:00
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var response =
|
|
|
|
|
|
await _providerService.CompleteSetupAsync(model.ToProvider(provider), userId, model.Token, model.Key);
|
|
|
|
|
|
|
2024-04-10 14:10:53 -04:00
|
|
|
|
if (_featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling))
|
|
|
|
|
|
{
|
|
|
|
|
|
var taxInfo = new TaxInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
BillingAddressCountry = model.TaxInfo.Country,
|
|
|
|
|
|
BillingAddressPostalCode = model.TaxInfo.PostalCode,
|
|
|
|
|
|
TaxIdNumber = model.TaxInfo.TaxId,
|
|
|
|
|
|
BillingAddressLine1 = model.TaxInfo.Line1,
|
|
|
|
|
|
BillingAddressLine2 = model.TaxInfo.Line2,
|
|
|
|
|
|
BillingAddressCity = model.TaxInfo.City,
|
|
|
|
|
|
BillingAddressState = model.TaxInfo.State
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-05-23 10:17:00 -04:00
|
|
|
|
await _providerBillingService.CreateCustomer(provider, taxInfo);
|
|
|
|
|
|
|
|
|
|
|
|
await _providerBillingService.StartSubscription(provider);
|
2024-04-10 14:10:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// We don't want to trap the user on the setup page, so we'll let this go through but the provider will be in an un-billable state.
|
|
|
|
|
|
_logger.LogError("Failed to create subscription for provider with ID {ID} during setup", provider.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
return new ProviderResponseModel(response);
|
|
|
|
|
|
}
|
2024-04-17 10:09:53 +01:00
|
|
|
|
|
|
|
|
|
|
[HttpPost("{id}/delete-recover-token")]
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task PostDeleteRecoverToken(Guid id, [FromBody] ProviderVerifyDeleteRecoverRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
await _providerService.DeleteAsync(provider, model.Token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
[HttpPost("{id}/delete")]
|
|
|
|
|
|
public async Task Delete(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_currentContext.ProviderProviderAdmin(id))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UnauthorizedAccessException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _providerService.DeleteAsync(provider);
|
|
|
|
|
|
}
|
2021-06-30 09:35:26 +02:00
|
|
|
|
}
|