Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SG-696] Organization Domain Claiming DB Objects and Migrations #2394

Merged
18 changes: 18 additions & 0 deletions src/Core/Entities/OrganizationDomain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities;

namespace Bit.Core.Entities;

public class OrganizationDomain : ITableObject<Guid>
{
public Guid Id { get; set; }
public Guid OrganizationId { get; set; }
public string Txt { get; set; }
[MaxLength(255)]
public string DomainName { get; set; }
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
public DateTime? VerifiedDate { get; set; }
public DateTime NextRunDate { get; set; }
public int NextRunCount { get; set; }
public void SetNewId() => Id = CoreHelpers.GenerateComb();
}
8 changes: 8 additions & 0 deletions src/Core/Repositories/IOrganizationDomainRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Bit.Core.Entities;

namespace Bit.Core.Repositories;

public interface IOrganizationDomainRepository : IRepository<OrganizationDomain, Guid>
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Bit.Core.Settings;

namespace Bit.Infrastructure.Dapper.Repositories;

public class OrganizationDomainRepository : Repository<OrganizationDomain, Guid>, IOrganizationDomainRepository
{
public OrganizationDomainRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
{ }

public OrganizationDomainRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString)
{ }
}
1 change: 1 addition & 0 deletions src/Infrastructure.EntityFramework/Models/Organization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Organization : Core.Entities.Organization
public virtual ICollection<Transaction> Transactions { get; set; }
public virtual ICollection<OrganizationApiKey> ApiKeys { get; set; }
public virtual ICollection<OrganizationConnection> Connections { get; set; }
public virtual ICollection<OrganizationDomain> Domains { get; set; }
}

public class OrganizationMapperProfile : Profile
Expand Down
16 changes: 16 additions & 0 deletions src/Infrastructure.EntityFramework/Models/OrganizationDomain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using AutoMapper;

namespace Bit.Infrastructure.EntityFramework.Models;

public class OrganizationDomain : Core.Entities.OrganizationDomain
{
public virtual Organization Organization { get; set; }
}

public class OrganizationDomainMapperProfile : Profile
{
public OrganizationDomainMapperProfile()
{
CreateMap<Core.Entities.OrganizationDomain, OrganizationDomain>().ReverseMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public DatabaseContext(DbContextOptions<DatabaseContext> options)
public DbSet<Transaction> Transactions { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<AuthRequest> AuthRequests { get; set; }
public DbSet<OrganizationDomain> OrganizationDomains { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
Expand Down Expand Up @@ -73,6 +74,7 @@ protected override void OnModelCreating(ModelBuilder builder)
var eOrganizationApiKey = builder.Entity<OrganizationApiKey>();
var eOrganizationConnection = builder.Entity<OrganizationConnection>();
var eAuthRequest = builder.Entity<AuthRequest>();
var eOrganizationDomain = builder.Entity<OrganizationDomain>();

eCipher.Property(c => c.Id).ValueGeneratedNever();
eCollection.Property(c => c.Id).ValueGeneratedNever();
Expand All @@ -94,6 +96,7 @@ protected override void OnModelCreating(ModelBuilder builder)
eOrganizationApiKey.Property(c => c.Id).ValueGeneratedNever();
eOrganizationConnection.Property(c => c.Id).ValueGeneratedNever();
eAuthRequest.Property(ar => ar.Id).ValueGeneratedNever();
eOrganizationDomain.Property(ar => ar.Id).ValueGeneratedNever();

eCollectionCipher.HasKey(cc => new { cc.CollectionId, cc.CipherId });
eCollectionUser.HasKey(cu => new { cu.CollectionId, cu.OrganizationUserId });
Expand Down Expand Up @@ -140,6 +143,7 @@ protected override void OnModelCreating(ModelBuilder builder)
eOrganizationApiKey.ToTable(nameof(OrganizationApiKey));
eOrganizationConnection.ToTable(nameof(OrganizationConnection));
eAuthRequest.ToTable(nameof(AuthRequest));
eOrganizationDomain.ToTable(nameof(OrganizationDomain));

ConfigureDateTimeUTCQueries(builder);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Sql/Sql.sqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,12 @@
<Build Include="dbo\Views\TransactionView.sql" />
<Build Include="dbo\Views\UserView.sql" />
</ItemGroup>
<ItemGroup>
<Content Include="dbo\Stored Procedures\OrganizationDomain_Create.sql" />
<Content Include="dbo\Stored Procedures\OrganizationDomain_DeleteById.sql" />
<Content Include="dbo\Stored Procedures\OrganizationDomain_ReadById.sql" />
<Content Include="dbo\Stored Procedures\OrganizationDomain_Update.sql" />
<Content Include="dbo\Tables\OrganizationDomain.sql" />
<Content Include="dbo\Views\OrganizationDomainView.sql" />
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions src/Sql/dbo/Stored Procedures/OrganizationDomain_Create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE PROCEDURE [dbo].[OrganizationDomain_Create]
@Id UNIQUEIDENTIFIER OUTPUT,
@OrganizationId UNIQUEIDENTIFIER,
@Txt VARCHAR(MAX),
@DomainName NVARCHAR(255),
@CreationDate DATETIME2(7),
@VerifiedDate DATETIME2(7),
@NextRunDate DATETIME2(7),
@NextRunCount TINYINT
AS
BEGIN
SET NOCOUNT ON

INSERT INTO [dbo].[OrganizationDomain]
(
[Id],
[OrganizationId],
[Txt],
[DomainName],
[CreationDate],
[VerifiedDate],
[NextRunDate],
[NextRunCount]
)
VALUES
(
@Id,
@OrganizationId,
@Txt,
@DomainName,
@CreationDate,
@VerifiedDate,
@NextRunDate,
@NextRunCount
)
END
12 changes: 12 additions & 0 deletions src/Sql/dbo/Stored Procedures/OrganizationDomain_DeleteById.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE PROCEDURE [dbo].[OrganizationDomain_DeleteById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

DELETE
FROM
[dbo].[OrganizationDomain]
WHERE
[Id] = @Id
END
13 changes: 13 additions & 0 deletions src/Sql/dbo/Stored Procedures/OrganizationDomain_ReadById.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[OrganizationDomain_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

SELECT
*
FROM
[dbo].[OrganizationDomain]
WHERE
[Id] = @Id
END
18 changes: 18 additions & 0 deletions src/Sql/dbo/Stored Procedures/OrganizationDomain_Update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE PROCEDURE [dbo].[OrganizationDomain_Update]
@Id UNIQUEIDENTIFIER OUTPUT,
@VerifiedDate DATETIME2(7),
@NextRunDate DATETIME2(7),
@NextRunCount TINYINT
AS
BEGIN
SET NOCOUNT ON

UPDATE
[dbo].[OrganizationDomain]
SET
[VerifiedDate] = @VerifiedDate,
[NextRunDate] = @NextRunDate,
[NextRunCount] = @NextRunCount
WHERE
[Id] = @Id
END
14 changes: 14 additions & 0 deletions src/Sql/dbo/Tables/OrganizationDomain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE [dbo].[OrganizationDomain] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[OrganizationId] UNIQUEIDENTIFIER NOT NULL,
[Txt] VARCHAR(MAX) NOT NULL,
[DomainName] NVARCHAR(255) NOT NULL,
[CreationDate] DATETIME2(7) NOT NULL,
[VerifiedDate] DATETIME2(7) NULL,
[NextRunDate] DATETIME2(7) NOT NULL,
[NextRunCount] TINYINT NOT NULL
CONSTRAINT [PK_OrganizationDomain] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_OrganzationDomain_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id])
);

GO
6 changes: 6 additions & 0 deletions src/Sql/dbo/Views/OrganizationDomainView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE VIEW [dbo].[OrganizationDomainView]
AS
SELECT
*
FROM
[dbo].[OrganizationDomain]
124 changes: 124 additions & 0 deletions util/Migrator/DbScripts/2022-11-03_00_OrganizationDomainInit.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
-- Create Organization Domain table
IF OBJECT_ID('[dbo].[OrganizationDomain]') IS NOT NULL
BEGIN
DROP TABLE [dbo].[OrganizationDomain]
END
GO

IF OBJECT_ID('[dbo].[OrganizationDomain]') IS NULL
BEGIN
CREATE TABLE [dbo].[OrganizationDomain] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[OrganizationId] UNIQUEIDENTIFIER NOT NULL,
[Txt] VARCHAR(MAX) NOT NULL,
[DomainName] NVARCHAR(255) NOT NULL,
[CreationDate] DATETIME2(7) NOT NULL,
[VerifiedDate] DATETIME2(7) NULL,
[NextRunDate] DATETIME2(7) NOT NULL,
[NextRunCount] TINYINT NOT NULL
CONSTRAINT [PK_OrganizationDomain] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_OrganzationDomain_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id])
)
END
GO

-- Create View
CREATE OR ALTER VIEW [dbo].[OrganizationDomainView]
AS
SELECT
*
FROM
[dbo].[OrganizationDomain]
GO

-- Organization Domain CRUD SPs
-- Create
CREATE OR ALTER PROCEDURE [dbo].[OrganizationDomain_Create]
@Id UNIQUEIDENTIFIER OUTPUT,
@OrganizationId UNIQUEIDENTIFIER,
@Txt VARCHAR(MAX),
@DomainName NVARCHAR(255),
@CreationDate DATETIME2(7),
@VerifiedDate DATETIME2(7),
@NextRunDate DATETIME2(7),
@NextRunCount TINYINT
AS
BEGIN
SET NOCOUNT ON

INSERT INTO [dbo].[OrganizationDomain]
(
[Id],
[OrganizationId],
[Txt],
[DomainName],
[CreationDate],
[VerifiedDate],
[NextRunDate],
[NextRunCount]
)
VALUES
(
@Id,
@OrganizationId,
@Txt,
@DomainName,
@CreationDate,
@VerifiedDate,
@NextRunDate,
@NextRunCount
)
END
GO

--Update
CREATE OR ALTER PROCEDURE [dbo].[OrganizationDomain_Update]
@Id UNIQUEIDENTIFIER OUTPUT,
@VerifiedDate DATETIME2(7),
@NextRunDate DATETIME2(7),
@NextRunCount TINYINT
AS
BEGIN
SET NOCOUNT ON

UPDATE
[dbo].[OrganizationDomain]
SET
[VerifiedDate] = @VerifiedDate,
[NextRunDate] = @NextRunDate,
[NextRunCount] = @NextRunCount
WHERE
[Id] = @Id
END
GO

--Read
CREATE OR ALTER PROCEDURE [dbo].[OrganizationDomain_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

SELECT
*
FROM
[dbo].[OrganizationDomain]
WHERE
[Id] = @Id
END
GO

--Delete
CREATE OR ALTER PROCEDURE [dbo].[OrganizationDomain_DeleteById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

DELETE
FROM
[dbo].[OrganizationDomain]
WHERE
[Id] = @Id
END
GO
Loading