mirror of
https://github.com/bitwarden/server
synced 2025-12-27 21:53:24 +00:00
Initial db work (#1687)
* Add organization sponsorship databases to all providers * Generalize create and update for database, specialize in code
This commit is contained in:
20
src/Core/Models/EntityFramework/OrganizationSponsorship.cs
Normal file
20
src/Core/Models/EntityFramework/OrganizationSponsorship.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using AutoMapper;
|
||||
|
||||
namespace Bit.Core.Models.EntityFramework
|
||||
{
|
||||
public class OrganizationSponsorship : Table.OrganizationSponsorship
|
||||
{
|
||||
public virtual Installation Installation { get; set; }
|
||||
public virtual Organization SponsoringOrganization { get; set; }
|
||||
public virtual Organization SponsoredOrganization { get; set; }
|
||||
}
|
||||
|
||||
public class OrganizationSponsorshipMapperProfile : Profile
|
||||
{
|
||||
public OrganizationSponsorshipMapperProfile()
|
||||
{
|
||||
CreateMap<Table.OrganizationSponsorship, OrganizationSponsorship>().ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,15 +7,14 @@ namespace Bit.Core.Models.Table
|
||||
public class OrganizationSponsorship : ITableObject<Guid>
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid InstallationId { get; set; }
|
||||
public Guid? InstallationId { get; set; }
|
||||
[Required]
|
||||
public Guid SponsoringOrganizationId { get; set; }
|
||||
[Required]
|
||||
public Guid SponsoringOrganizationUserId { get; set; }
|
||||
public Guid SponsoringUserId { get; set; }
|
||||
public Guid? SponsoredOrganizationId { get; set; }
|
||||
[MaxLength(256)]
|
||||
public string OfferedToEmail { get; set; }
|
||||
public Guid? SponsoredOrganizationId { get; set; }
|
||||
[Required]
|
||||
public bool CloudSponsor { get; set; }
|
||||
public DateTime? LastSyncDate { get; set; }
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
public DbSet<GroupUser> GroupUsers { get; set; }
|
||||
public DbSet<Installation> Installations { get; set; }
|
||||
public DbSet<Organization> Organizations { get; set; }
|
||||
public DbSet<OrganizationSponsorship> organizationSponsorships { get; set; }
|
||||
public DbSet<OrganizationUser> OrganizationUsers { get; set; }
|
||||
public DbSet<Policy> Policies { get; set; }
|
||||
public DbSet<Provider> Providers { get; set; }
|
||||
@@ -55,6 +56,7 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
var eGroupUser = builder.Entity<GroupUser>();
|
||||
var eInstallation = builder.Entity<Installation>();
|
||||
var eOrganization = builder.Entity<Organization>();
|
||||
var eOrganizationSponsorship = builder.Entity<OrganizationSponsorship>();
|
||||
var eOrganizationUser = builder.Entity<OrganizationUser>();
|
||||
var ePolicy = builder.Entity<Policy>();
|
||||
var eProvider = builder.Entity<Provider>();
|
||||
@@ -76,6 +78,7 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
eGroup.Property(c => c.Id).ValueGeneratedNever();
|
||||
eInstallation.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganization.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationSponsorship.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
ePolicy.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProvider.Property(c => c.Id).ValueGeneratedNever();
|
||||
@@ -115,6 +118,7 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
eGroupUser.ToTable(nameof(GroupUser));
|
||||
eInstallation.ToTable(nameof(Installation));
|
||||
eOrganization.ToTable(nameof(Organization));
|
||||
eOrganizationSponsorship.ToTable(nameof(OrganizationSponsorship));
|
||||
eOrganizationUser.ToTable(nameof(OrganizationUser));
|
||||
ePolicy.ToTable(nameof(Policy));
|
||||
eProvider.ToTable(nameof(Provider));
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using EFModel = Bit.Core.Models.EntityFramework;
|
||||
using TableModel = Bit.Core.Models.Table;
|
||||
|
||||
namespace Bit.Core.Repositories.EntityFramework
|
||||
{
|
||||
public class OrganizationSponsorshipRepository : Repository<TableModel.OrganizationSponsorship, EFModel.OrganizationSponsorship, Guid>, IOrganizationSponsorshipRepository
|
||||
{
|
||||
public OrganizationSponsorshipRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) :
|
||||
base(serviceScopeFactory, mapper, (DatabaseContext context) => context.organizationSponsorships)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<TableModel.OrganizationSponsorship> GetByOfferedToEmailAsync(string email)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.OfferedToEmail == email)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
public async Task<TableModel.OrganizationSponsorship> GetBySponsoredOrganizationIdAsync(Guid sponsoredOrganizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.SponsoredOrganizationId == sponsoredOrganizationId)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
public async Task<TableModel.OrganizationSponsorship> GetBySponsoringOrganizationUserIdAsync(Guid sponsoringOrganizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.SponsoringOrganizationUserId == sponsoringOrganizationUserId)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,7 @@ namespace Bit.Core.Utilities
|
||||
services.AddSingleton<IInstallationRepository, EntityFrameworkRepos.InstallationRepository>();
|
||||
services.AddSingleton<IMaintenanceRepository, EntityFrameworkRepos.MaintenanceRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, EntityFrameworkRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationSponsorshipRepository, EntityFrameworkRepos.OrganizationSponsorshipRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, EntityFrameworkRepos.OrganizationUserRepository>();
|
||||
services.AddSingleton<IPolicyRepository, EntityFrameworkRepos.PolicyRepository>();
|
||||
services.AddSingleton<ISendRepository, EntityFrameworkRepos.SendRepository>();
|
||||
@@ -127,6 +128,7 @@ namespace Bit.Core.Utilities
|
||||
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
|
||||
services.AddSingleton<IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationSponsorshipRepository, SqlServerRepos.OrganizationSponsorshipRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
||||
services.AddSingleton<IPolicyRepository, SqlServerRepos.PolicyRepository>();
|
||||
services.AddSingleton<ISendRepository, SqlServerRepos.SendRepository>();
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_Create]
|
||||
@Id UNIQUEIDENTIFIER OUTPUT,
|
||||
@InstallationId UNIQUEIDENTIFIER,
|
||||
@SponsoringOrganizationId UNIQUEIDENTIFIER,
|
||||
@SponsoringOrganizationUserID UNIQUEIDENTIFIER,
|
||||
@SponsoredOrganizationId UNIQUEIDENTIFIER,
|
||||
@OfferedToEmail NVARCHAR(256),
|
||||
@CloudSponsor BIT,
|
||||
@LastSyncDate DATETIME2 (7),
|
||||
@TimesRenewedWithoutValidation TINYINT,
|
||||
@SponsorshipLapsedDate DATETIME2 (7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
INSERT INTO [dbo].[OrganizationSponsorship]
|
||||
(
|
||||
[Id],
|
||||
[InstallationId],
|
||||
[SponsoringOrganizationId],
|
||||
[SponsoringOrganizationUserID],
|
||||
[SponsoredOrganizationId],
|
||||
[OfferedToEmail],
|
||||
[CloudSponsor],
|
||||
[LastSyncDate],
|
||||
[TimesRenewedWithoutValidation],
|
||||
[SponsorshipLapsedDate]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@Id,
|
||||
@InstallationId,
|
||||
@SponsoringOrganizationId,
|
||||
@SponsoringOrganizationUserID,
|
||||
@SponsoredOrganizationId,
|
||||
@OfferedToEmail,
|
||||
@CloudSponsor,
|
||||
@LastSyncDate,
|
||||
@TimesRenewedWithoutValidation,
|
||||
@SponsorshipLapsedDate
|
||||
)
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,17 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_DeleteById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
BEGIN TRANSACTION OrgSponsorship_DeleteById
|
||||
|
||||
DELETE
|
||||
FROM
|
||||
[dbo].[OrganizationSponsorship]
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
|
||||
COMMIT TRANSACTION OrgSponsorship_DeleteById
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_ReadById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[OrganizationSponsorshipView]
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_ReadByOfferedToEmail]
|
||||
@OfferedToEmail NVARCHAR (256) -- Should not be null
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[OrganizationSponsorshipView]
|
||||
WHERE
|
||||
[OfferedToEmail] = @OfferedToEmail
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_ReadBySponsoredOrganizationId]
|
||||
@SponsoredOrganizationId UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[OrganizationSponsorshipView]
|
||||
WHERE
|
||||
[SponsoredOrganizationId] = @SponsoredOrganizationId
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_ReadBySponsoringOrganizationUserId]
|
||||
@SponsoringOrganizationUserId UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[OrganizationSponsorshipView]
|
||||
WHERE
|
||||
[SponsoringOrganizationUserId] = @SponsoringOrganizationUserId
|
||||
END
|
||||
GO
|
||||
@@ -0,0 +1,31 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationSponsorship_Update]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@InstallationId UNIQUEIDENTIFIER,
|
||||
@SponsoringOrganizationId UNIQUEIDENTIFIER,
|
||||
@SponsoringOrganizationUserID UNIQUEIDENTIFIER,
|
||||
@SponsoredOrganizationId UNIQUEIDENTIFIER,
|
||||
@OfferedToEmail NVARCHAR(256),
|
||||
@CloudSponsor BIT,
|
||||
@LastSyncDate DATETIME2 (7),
|
||||
@TimesRenewedWithoutValidation TINYINT,
|
||||
@SponsorshipLapsedDate DATETIME2 (7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
UPDATE
|
||||
[dbo].[OrganizationSponsorship]
|
||||
SET
|
||||
[InstallationId] = @InstallationId,
|
||||
[SponsoringOrganizationId] = @SponsoringOrganizationId,
|
||||
[SponsoringOrganizationUserID] = @SponsoringOrganizationUserID,
|
||||
[SponsoredOrganizationId] = @SponsoredOrganizationId,
|
||||
[OfferedToEmail] = @OfferedToEmail,
|
||||
[CloudSponsor] = @CloudSponsor,
|
||||
[LastSyncDate] = @LastSyncDate,
|
||||
[TimesRenewedWithoutValidation] = @TimesRenewedWithoutValidation,
|
||||
[SponsorshipLapsedDate] = @SponsorshipLapsedDate
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
||||
GO
|
||||
41
src/Sql/dbo/Tables/OrganizationSponsorship.sql
Normal file
41
src/Sql/dbo/Tables/OrganizationSponsorship.sql
Normal file
@@ -0,0 +1,41 @@
|
||||
CREATE TABLE [dbo].[OrganizationSponsorship] (
|
||||
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||
[InstallationId] UNIQUEIDENTIFIER NULL,
|
||||
[SponsoringOrganizationId] UNIQUEIDENTIFIER NOT NULL,
|
||||
[SponsorginOrganizationUserID] UNIQUEIDENTIFIER NOT NULL,
|
||||
[SponsoredOrganizationId] UNIQUEIDENTIFIER NULL,
|
||||
[OfferedToEmail] NVARCHAR (256) NULL,
|
||||
[CloudSponsor] BIT NULL,
|
||||
[LastSyncDate] DATETIME2 (7) NULL,
|
||||
[TimesRenewedWithoutValidation] TINYINT DEFAULT 0,
|
||||
[SponsorshipLapsedDate] DATETIME2 (7) NULL,
|
||||
CONSTRAINT [PK_OrganizationSponsorship] PRIMARY KEY CLUSTERED ([Id] ASC),
|
||||
CONSTRAINT [FK_OrganizationSponsorship_InstallationId] FOREIGN KEY ([InstallationId]) REFERENCES [dbo].[Installation] ([Id]),
|
||||
CONSTRAINT [FK_OrganizationSponsorship_SponsoringOrg] FOREIGN KEY ([SponsoringOrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
|
||||
CONSTRAINT [FK_OrganizationSponsorship_SponsoredOrg] FOREIGN KEY ([SponsoredOrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
|
||||
);
|
||||
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationSponsorship_InstallationId]
|
||||
ON [dbo].[Organization]([Id] ASC, [InstallationId] ASC)
|
||||
WHERE [InstallationId] IS NOT NULL;
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationSponsorship_SponsoringOrganizationId]
|
||||
ON [dbo].[Organization]([Id] ASC, [SponsoringOrganizationId] ASC)
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationSponsorship_SponsoringOrganizationUserId]
|
||||
ON [dbo].[Organization]([Id] ASC, [SponsorginOrganizationUserID] ASC)
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationSponsorship_OfferedToEmail]
|
||||
ON [dbo].[Organization]([Id] ASC, [OfferedToEmail] ASC)
|
||||
WHERE [OfferedToEmail] IS NOT NULL;
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationSponsorship_SponsoredOrganizationID]
|
||||
ON [dbo].[Organization]([Id] ASC, [SponsoredOrganizationId] ASC)
|
||||
WHERE [SponsoredOrganizationId] IS NOT NULL;
|
||||
|
||||
Reference in New Issue
Block a user