From 39a81af3e935604d6b4b1198e2eb864eff371c47 Mon Sep 17 00:00:00 2001 From: Matt Portune Date: Thu, 25 Jun 2020 16:42:29 -0400 Subject: [PATCH] DAL & CRUD for SSO --- src/Core/Enums/SsoType.cs | 8 + .../Api/Request/SsoConfigRequestModel.cs | 12 + .../Api/Response/SsoConfigResponseModel.cs | 27 +++ src/Core/Models/Table/Organization.cs | 1 + src/Core/Models/Table/SsoConfig.cs | 14 ++ src/Sql/Sql.sqlproj | 2 + .../Stored Procedures/Organization_Create.sql | 3 + .../Stored Procedures/Organization_Update.sql | 2 + .../Stored Procedures/SsoConfig_Create.sql | 30 +++ .../Stored Procedures/SsoConfig_Update.sql | 22 ++ src/Sql/dbo/Tables/SsoConfig.sql | 1 - .../DbScripts/2020-06-23_00_OrgIdentifier.sql | 213 ++++++++++++++++++ ...Config.sql => 2020-06-24_00_SsoConfig.sql} | 69 +++++- ...ceId.sql => 2020-06-25_00_ReferenceId.sql} | 5 + 14 files changed, 407 insertions(+), 2 deletions(-) create mode 100644 src/Core/Enums/SsoType.cs create mode 100644 src/Core/Models/Api/Request/SsoConfigRequestModel.cs create mode 100644 src/Core/Models/Api/Response/SsoConfigResponseModel.cs create mode 100644 src/Core/Models/Table/SsoConfig.cs create mode 100644 src/Sql/dbo/Stored Procedures/SsoConfig_Create.sql create mode 100644 src/Sql/dbo/Stored Procedures/SsoConfig_Update.sql rename util/Migrator/DbScripts/{2020-06-23_01_SsoConfig.sql => 2020-06-24_00_SsoConfig.sql} (57%) rename util/Migrator/DbScripts/{2020-06-23_00_ReferenceId.sql => 2020-06-25_00_ReferenceId.sql} (98%) diff --git a/src/Core/Enums/SsoType.cs b/src/Core/Enums/SsoType.cs new file mode 100644 index 0000000000..38a0be4990 --- /dev/null +++ b/src/Core/Enums/SsoType.cs @@ -0,0 +1,8 @@ +namespace Bit.Core.Enums +{ + public enum SsoType : byte + { + // TODO proper SsoType values + Test = 1 + } +} diff --git a/src/Core/Models/Api/Request/SsoConfigRequestModel.cs b/src/Core/Models/Api/Request/SsoConfigRequestModel.cs new file mode 100644 index 0000000000..565d846ff5 --- /dev/null +++ b/src/Core/Models/Api/Request/SsoConfigRequestModel.cs @@ -0,0 +1,12 @@ +using System; + +namespace Bit.Core.Models.Api +{ + public class SsoConfigRequestModel + { + public long Id { get; set; } + public bool Enabled { get; set; } + public Guid OrganizationId { get; set; } + public string Data { get; set; } + } +} diff --git a/src/Core/Models/Api/Response/SsoConfigResponseModel.cs b/src/Core/Models/Api/Response/SsoConfigResponseModel.cs new file mode 100644 index 0000000000..e746239ed1 --- /dev/null +++ b/src/Core/Models/Api/Response/SsoConfigResponseModel.cs @@ -0,0 +1,27 @@ +using System; +using Bit.Core.Models.Table; + +namespace Bit.Core.Models.Api +{ + public class SsoConfigResponseModel : ResponseModel + { + public SsoConfigResponseModel(SsoConfig ssoConfig, string obj = "ssoconfig") + : base(obj) + { + if (ssoConfig == null) + { + throw new ArgumentNullException(nameof(ssoConfig)); + } + + Id = ssoConfig.Id; + Enabled = ssoConfig.Enabled; + OrganizationId = ssoConfig.OrganizationId; + Data = ssoConfig.Data; + } + + public long Id { get; set; } + public bool Enabled { get; set; } + public Guid OrganizationId { get; set; } + public string Data { get; set; } + } +} diff --git a/src/Core/Models/Table/Organization.cs b/src/Core/Models/Table/Organization.cs index 78a2121ae2..e32e1a9b3c 100644 --- a/src/Core/Models/Table/Organization.cs +++ b/src/Core/Models/Table/Organization.cs @@ -12,6 +12,7 @@ namespace Bit.Core.Models.Table private Dictionary _twoFactorProviders; public Guid Id { get; set; } + public string Identifier { get; set; } public string Name { get; set; } public string BusinessName { get; set; } public string BusinessAddress1 { get; set; } diff --git a/src/Core/Models/Table/SsoConfig.cs b/src/Core/Models/Table/SsoConfig.cs new file mode 100644 index 0000000000..a3f965e857 --- /dev/null +++ b/src/Core/Models/Table/SsoConfig.cs @@ -0,0 +1,14 @@ +using System; + +namespace Bit.Core.Models.Table +{ + public class SsoConfig + { + public long Id { get; set; } + public bool Enabled { get; set; } = true; + public Guid OrganizationId { get; set; } + public string Data { get; set; } + public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; + public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow; + } +} diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 6db42f89f0..1fcaf8afa4 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -69,8 +69,10 @@ + + diff --git a/src/Sql/dbo/Stored Procedures/Organization_Create.sql b/src/Sql/dbo/Stored Procedures/Organization_Create.sql index 83e73893cd..1e62905223 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_Create.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_Create.sql @@ -1,5 +1,6 @@ CREATE PROCEDURE [dbo].[Organization_Create] @Id UNIQUEIDENTIFIER, + @Identifier NVARCHAR(50), @Name NVARCHAR(50), @BusinessName NVARCHAR(50), @BusinessAddress1 NVARCHAR(50), @@ -41,6 +42,7 @@ BEGIN INSERT INTO [dbo].[Organization] ( [Id], + [Identifier], [Name], [BusinessName], [BusinessAddress1], @@ -79,6 +81,7 @@ BEGIN VALUES ( @Id, + @Identifier, @Name, @BusinessName, @BusinessAddress1, diff --git a/src/Sql/dbo/Stored Procedures/Organization_Update.sql b/src/Sql/dbo/Stored Procedures/Organization_Update.sql index dec1942768..f32b5c06b8 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_Update.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_Update.sql @@ -1,5 +1,6 @@ CREATE PROCEDURE [dbo].[Organization_Update] @Id UNIQUEIDENTIFIER, + @Identifier NVARCHAR(50), @Name NVARCHAR(50), @BusinessName NVARCHAR(50), @BusinessAddress1 NVARCHAR(50), @@ -41,6 +42,7 @@ BEGIN UPDATE [dbo].[Organization] SET + [Identifier] = @Identifier, [Name] = @Name, [BusinessName] = @BusinessName, [BusinessAddress1] = @BusinessAddress1, diff --git a/src/Sql/dbo/Stored Procedures/SsoConfig_Create.sql b/src/Sql/dbo/Stored Procedures/SsoConfig_Create.sql new file mode 100644 index 0000000000..0e707fd47c --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/SsoConfig_Create.sql @@ -0,0 +1,30 @@ +CREATE PROCEDURE [dbo].[SsoConfig_Create] + @Id BIGINT, + @Enabled BIT, + @OrganizationId UNIQUEIDENTIFIER, + @Data NVARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[SsoConfig] + ( + [Id], + [Enabled], + [OrganizationId], + [Data], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @Enabled, + @OrganizationId, + @Data, + @CreationDate, + @RevisionDate + ) +END diff --git a/src/Sql/dbo/Stored Procedures/SsoConfig_Update.sql b/src/Sql/dbo/Stored Procedures/SsoConfig_Update.sql new file mode 100644 index 0000000000..62faf5a854 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/SsoConfig_Update.sql @@ -0,0 +1,22 @@ +CREATE PROCEDURE [dbo].[SsoConfig_Update] + @Id BIGINT, + @Enabled BIT, + @OrganizationId UNIQUEIDENTIFIER, + @Data NVARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[SsoConfig] + SET + [Enabled] = @Enabled, + [OrganizationId] = @OrganizationId, + [Data] = @Data, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Tables/SsoConfig.sql b/src/Sql/dbo/Tables/SsoConfig.sql index e425300eb9..ad1e0bb280 100644 --- a/src/Sql/dbo/Tables/SsoConfig.sql +++ b/src/Sql/dbo/Tables/SsoConfig.sql @@ -2,7 +2,6 @@ [Id] BIGINT IDENTITY (1, 1) NOT NULL, [Enabled] BIT NOT NULL, [OrganizationId] UNIQUEIDENTIFIER NOT NULL, - [Identifier] NVARCHAR (50) NULL, [Data] NVARCHAR (MAX) NULL, [CreationDate] DATETIME2 (7) NOT NULL, [RevisionDate] DATETIME2 (7) NOT NULL, diff --git a/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql b/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql index 04ed6b7089..e91ac0771c 100644 --- a/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql +++ b/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql @@ -17,3 +17,216 @@ BEGIN WHERE [Identifier] IS NOT NULL; END GO + +IF OBJECT_ID('[dbo].[Organization_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Organization_Create] +END +GO + +CREATE PROCEDURE [dbo].[Organization_Create] + @Id UNIQUEIDENTIFIER, + @Identifier NVARCHAR(50), + @Name NVARCHAR(50), + @BusinessName NVARCHAR(50), + @BusinessAddress1 NVARCHAR(50), + @BusinessAddress2 NVARCHAR(50), + @BusinessAddress3 NVARCHAR(50), + @BusinessCountry VARCHAR(2), + @BusinessTaxNumber NVARCHAR(30), + @BillingEmail NVARCHAR(50), + @Plan NVARCHAR(50), + @PlanType TINYINT, + @Seats SMALLINT, + @MaxCollections SMALLINT, + @UsePolicies BIT, + @UseGroups BIT, + @UseDirectory BIT, + @UseEvents BIT, + @UseTotp BIT, + @Use2fa BIT, + @UseApi BIT, + @SelfHost BIT, + @UsersGetPremium BIT, + @Storage BIGINT, + @MaxStorageGb SMALLINT, + @Gateway TINYINT, + @GatewayCustomerId VARCHAR(50), + @GatewaySubscriptionId VARCHAR(50), + @Enabled BIT, + @LicenseKey VARCHAR(100), + @ApiKey VARCHAR(30), + @TwoFactorProviders NVARCHAR(MAX), + @ExpirationDate DATETIME2(7), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[Organization] + ( + [Id], + [Identifier], + [Name], + [BusinessName], + [BusinessAddress1], + [BusinessAddress2], + [BusinessAddress3], + [BusinessCountry], + [BusinessTaxNumber], + [BillingEmail], + [Plan], + [PlanType], + [Seats], + [MaxCollections], + [UsePolicies], + [UseGroups], + [UseDirectory], + [UseEvents], + [UseTotp], + [Use2fa], + [UseApi], + [SelfHost], + [UsersGetPremium], + [Storage], + [MaxStorageGb], + [Gateway], + [GatewayCustomerId], + [GatewaySubscriptionId], + [Enabled], + [LicenseKey], + [ApiKey], + [TwoFactorProviders], + [ExpirationDate], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @Identifier, + @Name, + @BusinessName, + @BusinessAddress1, + @BusinessAddress2, + @BusinessAddress3, + @BusinessCountry, + @BusinessTaxNumber, + @BillingEmail, + @Plan, + @PlanType, + @Seats, + @MaxCollections, + @UsePolicies, + @UseGroups, + @UseDirectory, + @UseEvents, + @UseTotp, + @Use2fa, + @UseApi, + @SelfHost, + @UsersGetPremium, + @Storage, + @MaxStorageGb, + @Gateway, + @GatewayCustomerId, + @GatewaySubscriptionId, + @Enabled, + @LicenseKey, + @ApiKey, + @TwoFactorProviders, + @ExpirationDate, + @CreationDate, + @RevisionDate + ) +END +GO + +IF OBJECT_ID('[dbo].[Organization_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Organization_Update] +END +GO + +CREATE PROCEDURE [dbo].[Organization_Update] + @Id UNIQUEIDENTIFIER, + @Identifier NVARCHAR(50), + @Name NVARCHAR(50), + @BusinessName NVARCHAR(50), + @BusinessAddress1 NVARCHAR(50), + @BusinessAddress2 NVARCHAR(50), + @BusinessAddress3 NVARCHAR(50), + @BusinessCountry VARCHAR(2), + @BusinessTaxNumber NVARCHAR(30), + @BillingEmail NVARCHAR(50), + @Plan NVARCHAR(50), + @PlanType TINYINT, + @Seats SMALLINT, + @MaxCollections SMALLINT, + @UsePolicies BIT, + @UseGroups BIT, + @UseDirectory BIT, + @UseEvents BIT, + @UseTotp BIT, + @Use2fa BIT, + @UseApi BIT, + @SelfHost BIT, + @UsersGetPremium BIT, + @Storage BIGINT, + @MaxStorageGb SMALLINT, + @Gateway TINYINT, + @GatewayCustomerId VARCHAR(50), + @GatewaySubscriptionId VARCHAR(50), + @Enabled BIT, + @LicenseKey VARCHAR(100), + @ApiKey VARCHAR(30), + @TwoFactorProviders NVARCHAR(MAX), + @ExpirationDate DATETIME2(7), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[Organization] + SET + [Identifier] = @Identifier, + [Name] = @Name, + [BusinessName] = @BusinessName, + [BusinessAddress1] = @BusinessAddress1, + [BusinessAddress2] = @BusinessAddress2, + [BusinessAddress3] = @BusinessAddress3, + [BusinessCountry] = @BusinessCountry, + [BusinessTaxNumber] = @BusinessTaxNumber, + [BillingEmail] = @BillingEmail, + [Plan] = @Plan, + [PlanType] = @PlanType, + [Seats] = @Seats, + [MaxCollections] = @MaxCollections, + [UsePolicies] = @UsePolicies, + [UseGroups] = @UseGroups, + [UseDirectory] = @UseDirectory, + [UseEvents] = @UseEvents, + [UseTotp] = @UseTotp, + [Use2fa] = @Use2fa, + [UseApi] = @UseApi, + [SelfHost] = @SelfHost, + [UsersGetPremium] = @UsersGetPremium, + [Storage] = @Storage, + [MaxStorageGb] = @MaxStorageGb, + [Gateway] = @Gateway, + [GatewayCustomerId] = @GatewayCustomerId, + [GatewaySubscriptionId] = @GatewaySubscriptionId, + [Enabled] = @Enabled, + [LicenseKey] = @LicenseKey, + [ApiKey] = @ApiKey, + [TwoFactorProviders] = @TwoFactorProviders, + [ExpirationDate] = @ExpirationDate, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END +GO \ No newline at end of file diff --git a/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql b/util/Migrator/DbScripts/2020-06-24_00_SsoConfig.sql similarity index 57% rename from util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql rename to util/Migrator/DbScripts/2020-06-24_00_SsoConfig.sql index 05f94384da..af7736a93e 100644 --- a/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql +++ b/util/Migrator/DbScripts/2020-06-24_00_SsoConfig.sql @@ -4,7 +4,6 @@ BEGIN [Id] BIGINT IDENTITY (1, 1) NOT NULL, [Enabled] BIT NOT NULL, [OrganizationId] UNIQUEIDENTIFIER NOT NULL, - [Identifier] NVARCHAR (50) NULL, [Data] NVARCHAR (MAX) NULL, [CreationDate] DATETIME2 (7) NOT NULL, [RevisionDate] DATETIME2 (7) NOT NULL, @@ -28,6 +27,74 @@ FROM [dbo].[SsoConfig] GO +IF OBJECT_ID('[dbo].[SsoConfig_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[SsoConfig_Create] +END +GO + +CREATE PROCEDURE [dbo].[SsoConfig_Create] + @Id BIGINT, + @Enabled BIT, + @OrganizationId UNIQUEIDENTIFIER, + @Data NVARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[SsoConfig] + ( + [Id], + [Enabled], + [OrganizationId], + [Data], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @Enabled, + @OrganizationId, + @Data, + @CreationDate, + @RevisionDate + ) +END +GO + +IF OBJECT_ID('[dbo].[SsoConfig_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[SsoConfig_Update] +END +GO + +CREATE PROCEDURE [dbo].[SsoConfig_Update] + @Id BIGINT, + @Enabled BIT, + @OrganizationId UNIQUEIDENTIFIER, + @Data NVARCHAR(MAX), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[SsoConfig] + SET + [Enabled] = @Enabled, + [OrganizationId] = @OrganizationId, + [Data] = @Data, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END +GO + IF OBJECT_ID('[dbo].[SsoConfig_ReadByIdentifier]') IS NOT NULL BEGIN DROP PROCEDURE [dbo].[SsoConfig_ReadByIdentifier] diff --git a/util/Migrator/DbScripts/2020-06-23_00_ReferenceId.sql b/util/Migrator/DbScripts/2020-06-25_00_ReferenceId.sql similarity index 98% rename from util/Migrator/DbScripts/2020-06-23_00_ReferenceId.sql rename to util/Migrator/DbScripts/2020-06-25_00_ReferenceId.sql index 601bec1311..5778ba4105 100644 --- a/util/Migrator/DbScripts/2020-06-23_00_ReferenceId.sql +++ b/util/Migrator/DbScripts/2020-06-25_00_ReferenceId.sql @@ -242,6 +242,7 @@ GO CREATE PROCEDURE [dbo].[Organization_Create] @Id UNIQUEIDENTIFIER, + @Identifier NVARCHAR(50), @Name NVARCHAR(50), @BusinessName NVARCHAR(50), @BusinessAddress1 NVARCHAR(50), @@ -283,6 +284,7 @@ BEGIN INSERT INTO [dbo].[Organization] ( [Id], + [Identifier], [Name], [BusinessName], [BusinessAddress1], @@ -321,6 +323,7 @@ BEGIN VALUES ( @Id, + @Identifier, @Name, @BusinessName, @BusinessAddress1, @@ -367,6 +370,7 @@ GO CREATE PROCEDURE [dbo].[Organization_Update] @Id UNIQUEIDENTIFIER, + @Identifier NVARCHAR(50), @Name NVARCHAR(50), @BusinessName NVARCHAR(50), @BusinessAddress1 NVARCHAR(50), @@ -408,6 +412,7 @@ BEGIN UPDATE [dbo].[Organization] SET + [Identifier] = @Identifier, [Name] = @Name, [BusinessName] = @BusinessName, [BusinessAddress1] = @BusinessAddress1,