1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 23:52:50 -05:00

[AC-1900] Update Vault DB to support provider billing (#3875)

* Add Gateway columns to Provider table

* Add ProviderId column to Transaction table

* Create ProviderPlan table

* Matt's feedback

* Rui's feedback

* Fixed Gateway parameter on Provider
This commit is contained in:
Alex Morask
2024-03-21 11:15:49 -04:00
committed by GitHub
parent 43ee5a24ec
commit 9f7e05869e
44 changed files with 9139 additions and 45 deletions

View File

@ -1,6 +1,7 @@
using System.Net;
using Bit.Core.AdminConsole.Enums.Provider;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Utilities;
namespace Bit.Core.AdminConsole.Entities.Provider;
@ -29,6 +30,9 @@ public class Provider : ITableObject<Guid>
public bool Enabled { get; set; } = true;
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public GatewayType? Gateway { get; set; }
public string GatewayCustomerId { get; set; }
public string GatewaySubscriptionId { get; set; }
public void SetNewId()
{

View File

@ -0,0 +1,23 @@
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Utilities;
namespace Bit.Core.Billing.Entities;
public class ProviderPlan : ITableObject<Guid>
{
public Guid Id { get; set; }
public Guid ProviderId { get; set; }
public PlanType PlanType { get; set; }
public int? SeatMinimum { get; set; }
public int? PurchasedSeats { get; set; }
public int? AllocatedSeats { get; set; }
public void SetNewId()
{
if (Id == default)
{
Id = CoreHelpers.GenerateComb();
}
}
}

View File

@ -0,0 +1,9 @@
using Bit.Core.Billing.Entities;
using Bit.Core.Repositories;
namespace Bit.Core.Billing.Repositories;
public interface IProviderPlanRepository : IRepository<ProviderPlan, Guid>
{
Task<ProviderPlan> GetByProviderId(Guid providerId);
}

View File

@ -20,6 +20,7 @@ public class Transaction : ITableObject<Guid>
[MaxLength(50)]
public string GatewayId { get; set; }
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
public Guid? ProviderId { get; set; }
public void SetNewId()
{

View File

@ -7,5 +7,6 @@ public interface ITransactionRepository : IRepository<Transaction, Guid>
{
Task<ICollection<Transaction>> GetManyByUserIdAsync(Guid userId);
Task<ICollection<Transaction>> GetManyByOrganizationIdAsync(Guid organizationId);
Task<ICollection<Transaction>> GetManyByProviderIdAsync(Guid providerId);
Task<Transaction> GetByGatewayIdAsync(GatewayType gatewayType, string gatewayId);
}

View File

@ -0,0 +1,28 @@
using System.Data;
using Bit.Core.Billing.Entities;
using Bit.Core.Billing.Repositories;
using Bit.Core.Settings;
using Bit.Infrastructure.Dapper.Repositories;
using Dapper;
using Microsoft.Data.SqlClient;
namespace Bit.Infrastructure.Dapper.Billing.Repositories;
public class ProviderPlanRepository(
GlobalSettings globalSettings)
: Repository<ProviderPlan, Guid>(
globalSettings.SqlServer.ConnectionString,
globalSettings.SqlServer.ReadOnlyConnectionString), IProviderPlanRepository
{
public async Task<ProviderPlan> GetByProviderId(Guid providerId)
{
var sqlConnection = new SqlConnection(ConnectionString);
var results = await sqlConnection.QueryAsync<ProviderPlan>(
"[dbo].[ProviderPlan_ReadByProviderId]",
new { ProviderId = providerId },
commandType: CommandType.StoredProcedure);
return results.FirstOrDefault();
}
}

View File

@ -1,11 +1,13 @@
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Auth.Repositories;
using Bit.Core.Billing.Repositories;
using Bit.Core.Repositories;
using Bit.Core.SecretsManager.Repositories;
using Bit.Core.Tools.Repositories;
using Bit.Core.Vault.Repositories;
using Bit.Infrastructure.Dapper.AdminConsole.Repositories;
using Bit.Infrastructure.Dapper.Auth.Repositories;
using Bit.Infrastructure.Dapper.Billing.Repositories;
using Bit.Infrastructure.Dapper.Repositories;
using Bit.Infrastructure.Dapper.SecretsManager.Repositories;
using Bit.Infrastructure.Dapper.Tools.Repositories;
@ -48,6 +50,7 @@ public static class DapperServiceCollectionExtensions
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<IOrganizationDomainRepository, OrganizationDomainRepository>();
services.AddSingleton<IWebAuthnCredentialRepository, WebAuthnCredentialRepository>();
services.AddSingleton<IProviderPlanRepository, ProviderPlanRepository>();
if (selfHosted)
{

View File

@ -44,6 +44,16 @@ public class TransactionRepository : Repository<Transaction, Guid>, ITransaction
}
}
public async Task<ICollection<Transaction>> GetManyByProviderIdAsync(Guid providerId)
{
await using var sqlConnection = new SqlConnection(ConnectionString);
var results = await sqlConnection.QueryAsync<Transaction>(
$"[{Schema}].[Transaction_ReadByProviderId]",
new { ProviderId = providerId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
public async Task<Transaction> GetByGatewayIdAsync(GatewayType gatewayType, string gatewayId)
{
using (var connection = new SqlConnection(ConnectionString))

View File

@ -0,0 +1,21 @@
using Bit.Infrastructure.EntityFramework.Billing.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Bit.Infrastructure.EntityFramework.Billing.Configurations;
public class ProviderPlanEntityTypeConfiguration : IEntityTypeConfiguration<ProviderPlan>
{
public void Configure(EntityTypeBuilder<ProviderPlan> builder)
{
builder
.Property(t => t.Id)
.ValueGeneratedNever();
builder
.HasIndex(providerPlan => new { providerPlan.Id, providerPlan.PlanType })
.IsUnique();
builder.ToTable(nameof(ProviderPlan));
}
}

View File

@ -0,0 +1,17 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
namespace Bit.Infrastructure.EntityFramework.Billing.Models;
public class ProviderPlan : Core.Billing.Entities.ProviderPlan
{
public virtual Provider Provider { get; set; }
}
public class ProviderPlanMapperProfile : Profile
{
public ProviderPlanMapperProfile()
{
CreateMap<Core.Billing.Entities.ProviderPlan, ProviderPlan>().ReverseMap();
}
}

View File

@ -0,0 +1,29 @@
using AutoMapper;
using Bit.Core.Billing.Entities;
using Bit.Core.Billing.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using EFProviderPlan = Bit.Infrastructure.EntityFramework.Billing.Models.ProviderPlan;
namespace Bit.Infrastructure.EntityFramework.Billing.Repositories;
public class ProviderPlanRepository(
IMapper mapper,
IServiceScopeFactory serviceScopeFactory)
: Repository<ProviderPlan, EFProviderPlan, Guid>(
serviceScopeFactory,
mapper,
context => context.ProviderPlans), IProviderPlanRepository
{
public async Task<ProviderPlan> GetByProviderId(Guid providerId)
{
using var serviceScope = ServiceScopeFactory.CreateScope();
var databaseContext = GetDatabaseContext(serviceScope);
var query =
from providerPlan in databaseContext.ProviderPlans
where providerPlan.ProviderId == providerId
select providerPlan;
return await query.FirstOrDefaultAsync();
}
}

View File

@ -1,5 +1,6 @@
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Auth.Repositories;
using Bit.Core.Billing.Repositories;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.SecretsManager.Repositories;
@ -7,6 +8,7 @@ using Bit.Core.Tools.Repositories;
using Bit.Core.Vault.Repositories;
using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
using Bit.Infrastructure.EntityFramework.Auth.Repositories;
using Bit.Infrastructure.EntityFramework.Billing.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.SecretsManager.Repositories;
using Bit.Infrastructure.EntityFramework.Tools.Repositories;
@ -85,6 +87,7 @@ public static class EntityFrameworkServiceCollectionExtensions
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<IOrganizationDomainRepository, OrganizationDomainRepository>();
services.AddSingleton<IWebAuthnCredentialRepository, WebAuthnCredentialRepository>();
services.AddSingleton<IProviderPlanRepository, ProviderPlanRepository>();
if (selfHosted)
{

View File

@ -1,5 +1,6 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
namespace Bit.Infrastructure.EntityFramework.Models;
@ -7,6 +8,7 @@ public class Transaction : Core.Entities.Transaction
{
public virtual Organization Organization { get; set; }
public virtual User User { get; set; }
public virtual Provider Provider { get; set; }
}
public class TransactionMapperProfile : Profile

View File

@ -2,6 +2,7 @@
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
using Bit.Infrastructure.EntityFramework.Auth.Models;
using Bit.Infrastructure.EntityFramework.Billing.Models;
using Bit.Infrastructure.EntityFramework.Converters;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.SecretsManager.Models;
@ -65,6 +66,7 @@ public class DatabaseContext : DbContext
public DbSet<AuthRequest> AuthRequests { get; set; }
public DbSet<OrganizationDomain> OrganizationDomains { get; set; }
public DbSet<WebAuthnCredential> WebAuthnCredentials { get; set; }
public DbSet<ProviderPlan> ProviderPlans { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{

View File

@ -47,4 +47,14 @@ public class TransactionRepository : Repository<Core.Entities.Transaction, Trans
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
}
public async Task<ICollection<Core.Entities.Transaction>> GetManyByProviderIdAsync(Guid providerId)
{
using var serviceScope = ServiceScopeFactory.CreateScope();
var databaseContext = GetDatabaseContext(serviceScope);
var results = await databaseContext.Transactions
.Where(transaction => transaction.ProviderId == providerId)
.ToListAsync();
return Mapper.Map<List<Core.Entities.Transaction>>(results);
}
}

View File

@ -0,0 +1,30 @@
CREATE PROCEDURE [dbo].[ProviderPlan_Create]
@Id UNIQUEIDENTIFIER OUTPUT,
@ProviderId UNIQUEIDENTIFIER,
@PlanType TINYINT,
@SeatMinimum INT,
@PurchasedSeats INT,
@AllocatedSeats INT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[ProviderPlan]
(
[Id],
[ProviderId],
[PlanType],
[SeatMinimum],
[PurchasedSeats],
[AllocatedSeats]
)
VALUES
(
@Id,
@ProviderId,
@PlanType,
@SeatMinimum,
@PurchasedSeats,
@AllocatedSeats
)
END

View File

@ -0,0 +1,12 @@
CREATE PROCEDURE [dbo].[ProviderPlan_DeleteById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DELETE
FROM
[dbo].[ProviderPlan]
WHERE
[Id] = @Id
END

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[ProviderPlan_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[ProviderPlanView]
WHERE
[Id] = @Id
END

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[ProviderPlan_ReadByProviderId]
@ProviderId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[ProviderPlanView]
WHERE
[ProviderId] = @ProviderId
END

View File

@ -0,0 +1,22 @@
CREATE PROCEDURE [dbo].[ProviderPlan_Update]
@Id UNIQUEIDENTIFIER,
@ProviderId UNIQUEIDENTIFIER,
@PlanType TINYINT,
@SeatMinimum INT,
@PurchasedSeats INT,
@AllocatedSeats INT
AS
BEGIN
SET NOCOUNT ON
UPDATE
[dbo].[ProviderPlan]
SET
[ProviderId] = @ProviderId,
[PlanType] = @PlanType,
[SeatMinimum] = @SeatMinimum,
[PurchasedSeats] = @PurchasedSeats,
[AllocatedSeats] = @AllocatedSeats
WHERE
[Id] = @Id
END

View File

@ -0,0 +1,11 @@
CREATE TABLE [dbo].[ProviderPlan] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[ProviderId] UNIQUEIDENTIFIER NOT NULL,
[PlanType] TINYINT NOT NULL,
[SeatMinimum] INT NULL,
[PurchasedSeats] INT NULL,
[AllocatedSeats] INT NULL,
CONSTRAINT [PK_ProviderPlan] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_ProviderPlan_Provider] FOREIGN KEY ([ProviderId]) REFERENCES [dbo].[Provider] ([Id]) ON DELETE CASCADE,
CONSTRAINT [PK_ProviderPlanType] UNIQUE ([ProviderId], [PlanType])
);

View File

@ -0,0 +1,6 @@
CREATE VIEW [dbo].[ProviderPlanView]
AS
SELECT
*
FROM
[dbo].[ProviderPlan]

View File

@ -14,7 +14,10 @@
@UseEvents BIT,
@Enabled BIT,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@Gateway TINYINT = 0,
@GatewayCustomerId VARCHAR(50) = NULL,
@GatewaySubscriptionId VARCHAR(50) = NULL
AS
BEGIN
SET NOCOUNT ON
@ -36,7 +39,10 @@ BEGIN
[UseEvents],
[Enabled],
[CreationDate],
[RevisionDate]
[RevisionDate],
[Gateway],
[GatewayCustomerId],
[GatewaySubscriptionId]
)
VALUES
(
@ -55,6 +61,9 @@ BEGIN
@UseEvents,
@Enabled,
@CreationDate,
@RevisionDate
@RevisionDate,
@Gateway,
@GatewayCustomerId,
@GatewaySubscriptionId
)
END

View File

@ -14,7 +14,10 @@
@UseEvents BIT,
@Enabled BIT,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@Gateway TINYINT = 0,
@GatewayCustomerId VARCHAR(50) = NULL,
@GatewaySubscriptionId VARCHAR(50) = NULL
AS
BEGIN
SET NOCOUNT ON
@ -36,7 +39,10 @@ BEGIN
[UseEvents] = @UseEvents,
[Enabled] = @Enabled,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[Gateway] = @Gateway,
[GatewayCustomerId] = @GatewayCustomerId,
[GatewaySubscriptionId] = @GatewaySubscriptionId
WHERE
[Id] = @Id
END

View File

@ -10,7 +10,8 @@
@PaymentMethodType TINYINT,
@Gateway TINYINT,
@GatewayId VARCHAR(50),
@CreationDate DATETIME2(7)
@CreationDate DATETIME2(7),
@ProviderId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
@ -28,7 +29,8 @@ BEGIN
[PaymentMethodType],
[Gateway],
[GatewayId],
[CreationDate]
[CreationDate],
[ProviderId]
)
VALUES
(
@ -43,6 +45,7 @@ BEGIN
@PaymentMethodType,
@Gateway,
@GatewayId,
@CreationDate
@CreationDate,
@ProviderId
)
END

View File

@ -9,4 +9,4 @@ BEGIN
[dbo].[Transaction]
WHERE
[Id] = @Id
END
END

View File

@ -12,4 +12,4 @@ BEGIN
WHERE
[Gateway] = @Gateway
AND [GatewayId] = @GatewayId
END
END

View File

@ -10,4 +10,4 @@ BEGIN
[dbo].[TransactionView]
WHERE
[Id] = @Id
END
END

View File

@ -11,4 +11,4 @@ BEGIN
WHERE
[UserId] IS NULL
AND [OrganizationId] = @OrganizationId
END
END

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[Transaction_ReadByProviderId]
@ProviderId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[TransactionView]
WHERE
[ProviderId] = @ProviderId
END

View File

@ -10,4 +10,4 @@ BEGIN
[dbo].[TransactionView]
WHERE
[UserId] = @UserId
END
END

View File

@ -10,7 +10,8 @@
@PaymentMethodType TINYINT,
@Gateway TINYINT,
@GatewayId VARCHAR(50),
@CreationDate DATETIME2(7)
@CreationDate DATETIME2(7),
@ProviderId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
@ -28,7 +29,8 @@ BEGIN
[PaymentMethodType] = @PaymentMethodType,
[Gateway] = @Gateway,
[GatewayId] = @GatewayId,
[CreationDate] = @CreationDate
[CreationDate] = @CreationDate,
[ProviderId] = @ProviderId
WHERE
[Id] = @Id
END
END

View File

@ -1,19 +1,22 @@
CREATE TABLE [dbo].[Provider] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (50) NULL,
[BusinessName] NVARCHAR (50) NULL,
[BusinessAddress1] NVARCHAR (50) NULL,
[BusinessAddress2] NVARCHAR (50) NULL,
[BusinessAddress3] NVARCHAR (50) NULL,
[BusinessCountry] VARCHAR (2) NULL,
[BusinessTaxNumber] NVARCHAR (30) NULL,
[BillingEmail] NVARCHAR (256) NULL,
[BillingPhone] NVARCHAR (50) NULL,
[Status] TINYINT NOT NULL,
[UseEvents] BIT NOT NULL,
[Type] TINYINT NOT NULL CONSTRAINT DF_Provider_Type DEFAULT (0),
[Enabled] BIT NOT NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (50) NULL,
[BusinessName] NVARCHAR (50) NULL,
[BusinessAddress1] NVARCHAR (50) NULL,
[BusinessAddress2] NVARCHAR (50) NULL,
[BusinessAddress3] NVARCHAR (50) NULL,
[BusinessCountry] VARCHAR (2) NULL,
[BusinessTaxNumber] NVARCHAR (30) NULL,
[BillingEmail] NVARCHAR (256) NULL,
[BillingPhone] NVARCHAR (50) NULL,
[Status] TINYINT NOT NULL,
[UseEvents] BIT NOT NULL,
[Type] TINYINT NOT NULL CONSTRAINT DF_Provider_Type DEFAULT (0),
[Enabled] BIT NOT NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
[Gateway] TINYINT NULL,
[GatewayCustomerId] VARCHAR (50) NULL,
[GatewaySubscriptionId] VARCHAR (50) NULL,
CONSTRAINT [PK_Provider] PRIMARY KEY CLUSTERED ([Id] ASC)
);

View File

@ -11,19 +11,18 @@
[Gateway] TINYINT NULL,
[GatewayId] VARCHAR(50) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[ProviderId] UNIQUEIDENTIFIER NULL,
CONSTRAINT [PK_Transaction] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Transaction_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Transaction_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE
CONSTRAINT [FK_Transaction_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Transaction_Provider] FOREIGN KEY ([ProviderId]) REFERENCES [dbo].[Provider] ([Id]) ON DELETE CASCADE
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_Transaction_Gateway_GatewayId]
ON [dbo].[Transaction]([Gateway] ASC, [GatewayId] ASC)
WHERE [Gateway] IS NOT NULL AND [GatewayId] IS NOT NULL;
GO
CREATE NONCLUSTERED INDEX [IX_Transaction_UserId_OrganizationId_CreationDate]
ON [dbo].[Transaction]([UserId] ASC, [OrganizationId] ASC, [CreationDate] ASC);