From aea85ea0eb7b9f2b0529b724b4fd75f333aee655 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Thu, 18 Mar 2021 21:43:49 +0100 Subject: [PATCH] Fixes #1101: Extend email column length to 256 characters (MSSQL) (#1191) * Fixes bitwarden/server/#1101 - Extended length of Email column to 256 characters - Installation * Fixes bitwarden/server/#1101 - Extended length of Email column to 256 characters - User * Fixes bitwarden/server/#1101 - Extended length of BillingEmail column to 256 characters - Organization * Fixes bitwarden/server/#1101 - Extended length of Email column to 256 characters - OrganizationUser * Fixes bitwarden/server/#1101 - Extended length of Email column to 256 characters - EmergencyAccess * Fixes bitwarden/server/bitwarden#1101 - Fixed issues after PR review --- .../EmergencyAccess_Create.sql | 2 +- ...rgencyAccess_ReadCountByGrantorIdEmail.sql | 2 +- .../EmergencyAccess_Update.sql | 2 +- .../Stored Procedures/Installation_Create.sql | 2 +- .../Stored Procedures/Installation_Update.sql | 2 +- .../OrganizationUser_Create.sql | 2 +- ...OrganizationUser_CreateWithCollections.sql | 2 +- ...nizationUser_ReadByOrganizationIdEmail.sql | 2 +- ...ionUser_ReadCountByOrganizationIdEmail.sql | 2 +- .../OrganizationUser_Update.sql | 2 +- ...OrganizationUser_UpdateWithCollections.sql | 2 +- .../Stored Procedures/Organization_Create.sql | 2 +- .../Stored Procedures/Organization_Search.sql | 2 +- .../Stored Procedures/Organization_Update.sql | 2 +- src/Sql/dbo/Stored Procedures/User_Create.sql | 2 +- .../Stored Procedures/User_ReadByEmail.sql | 2 +- .../Stored Procedures/User_ReadKdfByEmail.sql | 2 +- src/Sql/dbo/Stored Procedures/User_Search.sql | 4 +- src/Sql/dbo/Stored Procedures/User_Update.sql | 2 +- src/Sql/dbo/Tables/EmergencyAccess.sql | 2 +- src/Sql/dbo/Tables/Installation.sql | 2 +- src/Sql/dbo/Tables/Organization.sql | 2 +- src/Sql/dbo/Tables/OrganizationUser.sql | 2 +- src/Sql/dbo/Tables/User.sql | 2 +- ...4_00_Installation_Enlarge_Email_Column.sql | 72 +++++ ...021-03-04_01_User_Enlarge_Email_Column.sql | 277 ++++++++++++++++ ...4_02_Organization_Enlarge_Email_Column.sql | 300 +++++++++++++++++ ..._OrganizationUser_Enlarge_Email_Column.sql | 304 ++++++++++++++++++ ...4_EmergencyAccess_Enlarge_Email_Column.sql | 140 ++++++++ 29 files changed, 1118 insertions(+), 25 deletions(-) create mode 100644 util/Migrator/DbScripts/2021-03-04_00_Installation_Enlarge_Email_Column.sql create mode 100644 util/Migrator/DbScripts/2021-03-04_01_User_Enlarge_Email_Column.sql create mode 100644 util/Migrator/DbScripts/2021-03-04_02_Organization_Enlarge_Email_Column.sql create mode 100644 util/Migrator/DbScripts/2021-03-04_03_OrganizationUser_Enlarge_Email_Column.sql create mode 100644 util/Migrator/DbScripts/2021-03-04_04_EmergencyAccess_Enlarge_Email_Column.sql diff --git a/src/Sql/dbo/Stored Procedures/EmergencyAccess_Create.sql b/src/Sql/dbo/Stored Procedures/EmergencyAccess_Create.sql index d37c4738d..b949b7bf7 100644 --- a/src/Sql/dbo/Stored Procedures/EmergencyAccess_Create.sql +++ b/src/Sql/dbo/Stored Procedures/EmergencyAccess_Create.sql @@ -2,7 +2,7 @@ @Id UNIQUEIDENTIFIER, @GrantorId UNIQUEIDENTIFIER, @GranteeId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @KeyEncrypted VARCHAR(MAX), @Type TINYINT, @Status TINYINT, diff --git a/src/Sql/dbo/Stored Procedures/EmergencyAccess_ReadCountByGrantorIdEmail.sql b/src/Sql/dbo/Stored Procedures/EmergencyAccess_ReadCountByGrantorIdEmail.sql index 0f1ec535a..2ad7d25ff 100644 --- a/src/Sql/dbo/Stored Procedures/EmergencyAccess_ReadCountByGrantorIdEmail.sql +++ b/src/Sql/dbo/Stored Procedures/EmergencyAccess_ReadCountByGrantorIdEmail.sql @@ -1,6 +1,6 @@ CREATE PROCEDURE [dbo].[EmergencyAccess_ReadCountByGrantorIdEmail] @GrantorId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @OnlyUsers BIT AS BEGIN diff --git a/src/Sql/dbo/Stored Procedures/EmergencyAccess_Update.sql b/src/Sql/dbo/Stored Procedures/EmergencyAccess_Update.sql index 0831c7dc7..a64af8041 100644 --- a/src/Sql/dbo/Stored Procedures/EmergencyAccess_Update.sql +++ b/src/Sql/dbo/Stored Procedures/EmergencyAccess_Update.sql @@ -2,7 +2,7 @@ @Id UNIQUEIDENTIFIER, @GrantorId UNIQUEIDENTIFIER, @GranteeId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @KeyEncrypted VARCHAR(MAX), @Type TINYINT, @Status TINYINT, diff --git a/src/Sql/dbo/Stored Procedures/Installation_Create.sql b/src/Sql/dbo/Stored Procedures/Installation_Create.sql index cc862c647..121ed30b2 100644 --- a/src/Sql/dbo/Stored Procedures/Installation_Create.sql +++ b/src/Sql/dbo/Stored Procedures/Installation_Create.sql @@ -1,6 +1,6 @@ CREATE PROCEDURE [dbo].[Installation_Create] @Id UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Key VARCHAR(150), @Enabled BIT, @CreationDate DATETIME2(7) diff --git a/src/Sql/dbo/Stored Procedures/Installation_Update.sql b/src/Sql/dbo/Stored Procedures/Installation_Update.sql index b56a35150..af2fd8737 100644 --- a/src/Sql/dbo/Stored Procedures/Installation_Update.sql +++ b/src/Sql/dbo/Stored Procedures/Installation_Update.sql @@ -1,6 +1,6 @@ CREATE PROCEDURE [dbo].[Installation_Update] @Id UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Key VARCHAR(150), @Enabled BIT, @CreationDate DATETIME2(7) diff --git a/src/Sql/dbo/Stored Procedures/OrganizationUser_Create.sql b/src/Sql/dbo/Stored Procedures/OrganizationUser_Create.sql index ed90d822c..53d6400f2 100644 --- a/src/Sql/dbo/Stored Procedures/OrganizationUser_Create.sql +++ b/src/Sql/dbo/Stored Procedures/OrganizationUser_Create.sql @@ -2,7 +2,7 @@ @Id UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Key VARCHAR(MAX), @Status TINYINT, @Type TINYINT, diff --git a/src/Sql/dbo/Stored Procedures/OrganizationUser_CreateWithCollections.sql b/src/Sql/dbo/Stored Procedures/OrganizationUser_CreateWithCollections.sql index ab82106eb..b6eee6ae6 100644 --- a/src/Sql/dbo/Stored Procedures/OrganizationUser_CreateWithCollections.sql +++ b/src/Sql/dbo/Stored Procedures/OrganizationUser_CreateWithCollections.sql @@ -2,7 +2,7 @@ @Id UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Key VARCHAR(MAX), @Status TINYINT, @Type TINYINT, diff --git a/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadByOrganizationIdEmail.sql b/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadByOrganizationIdEmail.sql index 313deff1b..6193ed927 100644 --- a/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadByOrganizationIdEmail.sql +++ b/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadByOrganizationIdEmail.sql @@ -1,6 +1,6 @@ CREATE PROCEDURE [dbo].[OrganizationUser_ReadByOrganizationIdEmail] @OrganizationId UNIQUEIDENTIFIER, - @Email NVARCHAR(50) + @Email NVARCHAR(256) AS BEGIN SET NOCOUNT ON diff --git a/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadCountByOrganizationIdEmail.sql b/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadCountByOrganizationIdEmail.sql index 0808543ae..75be1aba6 100644 --- a/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadCountByOrganizationIdEmail.sql +++ b/src/Sql/dbo/Stored Procedures/OrganizationUser_ReadCountByOrganizationIdEmail.sql @@ -1,6 +1,6 @@ CREATE PROCEDURE [dbo].[OrganizationUser_ReadCountByOrganizationIdEmail] @OrganizationId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @OnlyUsers BIT AS BEGIN diff --git a/src/Sql/dbo/Stored Procedures/OrganizationUser_Update.sql b/src/Sql/dbo/Stored Procedures/OrganizationUser_Update.sql index abb4fefbf..7890615d2 100644 --- a/src/Sql/dbo/Stored Procedures/OrganizationUser_Update.sql +++ b/src/Sql/dbo/Stored Procedures/OrganizationUser_Update.sql @@ -2,7 +2,7 @@ @Id UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Key VARCHAR(MAX), @Status TINYINT, @Type TINYINT, diff --git a/src/Sql/dbo/Stored Procedures/OrganizationUser_UpdateWithCollections.sql b/src/Sql/dbo/Stored Procedures/OrganizationUser_UpdateWithCollections.sql index 10a456401..5c4a5204c 100644 --- a/src/Sql/dbo/Stored Procedures/OrganizationUser_UpdateWithCollections.sql +++ b/src/Sql/dbo/Stored Procedures/OrganizationUser_UpdateWithCollections.sql @@ -2,7 +2,7 @@ @Id UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER, - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Key VARCHAR(MAX), @Status TINYINT, @Type TINYINT, diff --git a/src/Sql/dbo/Stored Procedures/Organization_Create.sql b/src/Sql/dbo/Stored Procedures/Organization_Create.sql index 55394f622..af8bf5620 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_Create.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_Create.sql @@ -8,7 +8,7 @@ @BusinessAddress3 NVARCHAR(50), @BusinessCountry VARCHAR(2), @BusinessTaxNumber NVARCHAR(30), - @BillingEmail NVARCHAR(50), + @BillingEmail NVARCHAR(256), @Plan NVARCHAR(50), @PlanType TINYINT, @Seats SMALLINT, diff --git a/src/Sql/dbo/Stored Procedures/Organization_Search.sql b/src/Sql/dbo/Stored Procedures/Organization_Search.sql index 0dd441a7b..85c3664c1 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_Search.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_Search.sql @@ -1,6 +1,6 @@ CREATE PROCEDURE [dbo].[Organization_Search] @Name NVARCHAR(50), - @UserEmail NVARCHAR(50), + @UserEmail NVARCHAR(256), @Paid BIT, @Skip INT = 0, @Take INT = 25 diff --git a/src/Sql/dbo/Stored Procedures/Organization_Update.sql b/src/Sql/dbo/Stored Procedures/Organization_Update.sql index 828c5581e..668135128 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_Update.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_Update.sql @@ -8,7 +8,7 @@ @BusinessAddress3 NVARCHAR(50), @BusinessCountry VARCHAR(2), @BusinessTaxNumber NVARCHAR(30), - @BillingEmail NVARCHAR(50), + @BillingEmail NVARCHAR(256), @Plan NVARCHAR(50), @PlanType TINYINT, @Seats SMALLINT, diff --git a/src/Sql/dbo/Stored Procedures/User_Create.sql b/src/Sql/dbo/Stored Procedures/User_Create.sql index 98a176c52..cdb8bda18 100644 --- a/src/Sql/dbo/Stored Procedures/User_Create.sql +++ b/src/Sql/dbo/Stored Procedures/User_Create.sql @@ -1,7 +1,7 @@ CREATE PROCEDURE [dbo].[User_Create] @Id UNIQUEIDENTIFIER, @Name NVARCHAR(50), - @Email NVARCHAR(50), + @Email NVARCHAR(256), @EmailVerified BIT, @MasterPassword NVARCHAR(300), @MasterPasswordHint NVARCHAR(50), diff --git a/src/Sql/dbo/Stored Procedures/User_ReadByEmail.sql b/src/Sql/dbo/Stored Procedures/User_ReadByEmail.sql index 0343576b3..6abd7fbef 100644 --- a/src/Sql/dbo/Stored Procedures/User_ReadByEmail.sql +++ b/src/Sql/dbo/Stored Procedures/User_ReadByEmail.sql @@ -1,5 +1,5 @@ CREATE PROCEDURE [dbo].[User_ReadByEmail] - @Email NVARCHAR(50) + @Email NVARCHAR(256) AS BEGIN SET NOCOUNT ON diff --git a/src/Sql/dbo/Stored Procedures/User_ReadKdfByEmail.sql b/src/Sql/dbo/Stored Procedures/User_ReadKdfByEmail.sql index a58634e0a..790abcaf2 100644 --- a/src/Sql/dbo/Stored Procedures/User_ReadKdfByEmail.sql +++ b/src/Sql/dbo/Stored Procedures/User_ReadKdfByEmail.sql @@ -1,5 +1,5 @@ CREATE PROCEDURE [dbo].[User_ReadKdfByEmail] - @Email NVARCHAR(50) + @Email NVARCHAR(256) AS BEGIN SET NOCOUNT ON diff --git a/src/Sql/dbo/Stored Procedures/User_Search.sql b/src/Sql/dbo/Stored Procedures/User_Search.sql index 54cd85015..5e21cb146 100644 --- a/src/Sql/dbo/Stored Procedures/User_Search.sql +++ b/src/Sql/dbo/Stored Procedures/User_Search.sql @@ -1,12 +1,12 @@ CREATE PROCEDURE [dbo].[User_Search] - @Email NVARCHAR(50), + @Email NVARCHAR(256), @Skip INT = 0, @Take INT = 25 WITH RECOMPILE AS BEGIN SET NOCOUNT ON - DECLARE @EmailLikeSearch NVARCHAR(55) = @Email + '%' + DECLARE @EmailLikeSearch NVARCHAR(261) = @Email + '%' SELECT * diff --git a/src/Sql/dbo/Stored Procedures/User_Update.sql b/src/Sql/dbo/Stored Procedures/User_Update.sql index 25e2eca38..90c4bb89e 100644 --- a/src/Sql/dbo/Stored Procedures/User_Update.sql +++ b/src/Sql/dbo/Stored Procedures/User_Update.sql @@ -1,7 +1,7 @@ CREATE PROCEDURE [dbo].[User_Update] @Id UNIQUEIDENTIFIER, @Name NVARCHAR(50), - @Email NVARCHAR(50), + @Email NVARCHAR(256), @EmailVerified BIT, @MasterPassword NVARCHAR(300), @MasterPasswordHint NVARCHAR(50), diff --git a/src/Sql/dbo/Tables/EmergencyAccess.sql b/src/Sql/dbo/Tables/EmergencyAccess.sql index c1ba95267..fc02344de 100644 --- a/src/Sql/dbo/Tables/EmergencyAccess.sql +++ b/src/Sql/dbo/Tables/EmergencyAccess.sql @@ -3,7 +3,7 @@ [Id] UNIQUEIDENTIFIER NOT NULL, [GrantorId] UNIQUEIDENTIFIER NOT NULL, [GranteeId] UNIQUEIDENTIFIER NULL, - [Email] NVARCHAR (50) NULL, + [Email] NVARCHAR (256) NULL, [KeyEncrypted] VARCHAR (MAX) NULL, [WaitTimeDays] SMALLINT NULL, [Type] TINYINT NOT NULL, diff --git a/src/Sql/dbo/Tables/Installation.sql b/src/Sql/dbo/Tables/Installation.sql index 17304e623..df4b7260e 100644 --- a/src/Sql/dbo/Tables/Installation.sql +++ b/src/Sql/dbo/Tables/Installation.sql @@ -1,6 +1,6 @@ CREATE TABLE [dbo].[Installation] ( [Id] UNIQUEIDENTIFIER NOT NULL, - [Email] NVARCHAR (50) NOT NULL, + [Email] NVARCHAR (256) NOT NULL, [Key] VARCHAR (150) NOT NULL, [Enabled] BIT NOT NULL, [CreationDate] DATETIME2 (7) NOT NULL, diff --git a/src/Sql/dbo/Tables/Organization.sql b/src/Sql/dbo/Tables/Organization.sql index 43242f287..f4da5aa66 100644 --- a/src/Sql/dbo/Tables/Organization.sql +++ b/src/Sql/dbo/Tables/Organization.sql @@ -8,7 +8,7 @@ [BusinessAddress3] NVARCHAR (50) NULL, [BusinessCountry] VARCHAR (2) NULL, [BusinessTaxNumber] NVARCHAR (30) NULL, - [BillingEmail] NVARCHAR (50) NOT NULL, + [BillingEmail] NVARCHAR (256) NOT NULL, [Plan] NVARCHAR (50) NOT NULL, [PlanType] TINYINT NOT NULL, [Seats] SMALLINT NULL, diff --git a/src/Sql/dbo/Tables/OrganizationUser.sql b/src/Sql/dbo/Tables/OrganizationUser.sql index eb4211316..1047db635 100644 --- a/src/Sql/dbo/Tables/OrganizationUser.sql +++ b/src/Sql/dbo/Tables/OrganizationUser.sql @@ -2,7 +2,7 @@ [Id] UNIQUEIDENTIFIER NOT NULL, [OrganizationId] UNIQUEIDENTIFIER NOT NULL, [UserId] UNIQUEIDENTIFIER NULL, - [Email] NVARCHAR (50) NULL, + [Email] NVARCHAR (256) NULL, [Key] VARCHAR (MAX) NULL, [Status] TINYINT NOT NULL, [Type] TINYINT NOT NULL, diff --git a/src/Sql/dbo/Tables/User.sql b/src/Sql/dbo/Tables/User.sql index b11657c6f..3e3237aef 100644 --- a/src/Sql/dbo/Tables/User.sql +++ b/src/Sql/dbo/Tables/User.sql @@ -1,7 +1,7 @@ CREATE TABLE [dbo].[User] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Name] NVARCHAR (50) NULL, - [Email] NVARCHAR (50) NOT NULL, + [Email] NVARCHAR (256) NOT NULL, [EmailVerified] BIT NOT NULL, [MasterPassword] NVARCHAR (300) NULL, [MasterPasswordHint] NVARCHAR (50) NULL, diff --git a/util/Migrator/DbScripts/2021-03-04_00_Installation_Enlarge_Email_Column.sql b/util/Migrator/DbScripts/2021-03-04_00_Installation_Enlarge_Email_Column.sql new file mode 100644 index 000000000..4c0250ff4 --- /dev/null +++ b/util/Migrator/DbScripts/2021-03-04_00_Installation_Enlarge_Email_Column.sql @@ -0,0 +1,72 @@ +-- Check email column is 50 characters long (100 bytes) +IF COL_LENGTH('[dbo].[Installation]', 'Email') = 100 +BEGIN + ALTER TABLE [dbo].[Installation] + ALTER COLUMN + Email NVARCHAR(256) NOT NULL +END +GO + +-- Recreate procedure Installation_Create +IF OBJECT_ID('[dbo].[Installation_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Installation_Create] +END +GO + +CREATE PROCEDURE [dbo].[Installation_Create] + @Id UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @Key VARCHAR(150), + @Enabled BIT, + @CreationDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[Installation] + ( + [Id], + [Email], + [Key], + [Enabled], + [CreationDate] + ) + VALUES + ( + @Id, + @Email, + @Key, + @Enabled, + @CreationDate + ) +END +GO + +-- Recreate procedure Installation_Update +IF OBJECT_ID('[dbo].[Installation_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Installation_Update] +END +GO + +CREATE PROCEDURE [dbo].[Installation_Update] + @Id UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @Key VARCHAR(150), + @Enabled BIT, + @CreationDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[Installation] + SET + [Email] = @Email, + [Key] = @Key, + [Enabled] = @Enabled, + [CreationDate] = @CreationDate + WHERE + [Id] = @Id +END \ No newline at end of file diff --git a/util/Migrator/DbScripts/2021-03-04_01_User_Enlarge_Email_Column.sql b/util/Migrator/DbScripts/2021-03-04_01_User_Enlarge_Email_Column.sql new file mode 100644 index 000000000..9ef016130 --- /dev/null +++ b/util/Migrator/DbScripts/2021-03-04_01_User_Enlarge_Email_Column.sql @@ -0,0 +1,277 @@ +-- Check email column is 50 characters long (100 bytes) +IF COL_LENGTH('[dbo].[User]', 'Email') = 100 +BEGIN + ALTER TABLE [dbo].[User] + ALTER COLUMN + Email NVARCHAR(256) NOT NULL +END +GO + +-- Recreate procedure User_Create +IF OBJECT_ID('[dbo].[User_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[User_Create] +END +GO + +CREATE PROCEDURE [dbo].[User_Create] + @Id UNIQUEIDENTIFIER, + @Name NVARCHAR(50), + @Email NVARCHAR(256), + @EmailVerified BIT, + @MasterPassword NVARCHAR(300), + @MasterPasswordHint NVARCHAR(50), + @Culture NVARCHAR(10), + @SecurityStamp NVARCHAR(50), + @TwoFactorProviders NVARCHAR(MAX), + @TwoFactorRecoveryCode NVARCHAR(32), + @EquivalentDomains NVARCHAR(MAX), + @ExcludedGlobalEquivalentDomains NVARCHAR(MAX), + @AccountRevisionDate DATETIME2(7), + @Key NVARCHAR(MAX), + @PublicKey NVARCHAR(MAX), + @PrivateKey NVARCHAR(MAX), + @Premium BIT, + @PremiumExpirationDate DATETIME2(7), + @RenewalReminderDate DATETIME2(7), + @Storage BIGINT, + @MaxStorageGb SMALLINT, + @Gateway TINYINT, + @GatewayCustomerId VARCHAR(50), + @GatewaySubscriptionId VARCHAR(50), + @ReferenceData VARCHAR(MAX), + @LicenseKey VARCHAR(100), + @Kdf TINYINT, + @KdfIterations INT, + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7), + @ApiKey VARCHAR(30) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[User] + ( + [Id], + [Name], + [Email], + [EmailVerified], + [MasterPassword], + [MasterPasswordHint], + [Culture], + [SecurityStamp], + [TwoFactorProviders], + [TwoFactorRecoveryCode], + [EquivalentDomains], + [ExcludedGlobalEquivalentDomains], + [AccountRevisionDate], + [Key], + [PublicKey], + [PrivateKey], + [Premium], + [PremiumExpirationDate], + [RenewalReminderDate], + [Storage], + [MaxStorageGb], + [Gateway], + [GatewayCustomerId], + [GatewaySubscriptionId], + [ReferenceData], + [LicenseKey], + [Kdf], + [KdfIterations], + [CreationDate], + [RevisionDate], + [ApiKey] + ) + VALUES + ( + @Id, + @Name, + @Email, + @EmailVerified, + @MasterPassword, + @MasterPasswordHint, + @Culture, + @SecurityStamp, + @TwoFactorProviders, + @TwoFactorRecoveryCode, + @EquivalentDomains, + @ExcludedGlobalEquivalentDomains, + @AccountRevisionDate, + @Key, + @PublicKey, + @PrivateKey, + @Premium, + @PremiumExpirationDate, + @RenewalReminderDate, + @Storage, + @MaxStorageGb, + @Gateway, + @GatewayCustomerId, + @GatewaySubscriptionId, + @ReferenceData, + @LicenseKey, + @Kdf, + @KdfIterations, + @CreationDate, + @RevisionDate, + @ApiKey + ) +END +GO + +-- Recreate procedure User_Update +IF OBJECT_ID('[dbo].[User_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[User_Update] +END +GO + +CREATE PROCEDURE [dbo].[User_Update] + @Id UNIQUEIDENTIFIER, + @Name NVARCHAR(50), + @Email NVARCHAR(256), + @EmailVerified BIT, + @MasterPassword NVARCHAR(300), + @MasterPasswordHint NVARCHAR(50), + @Culture NVARCHAR(10), + @SecurityStamp NVARCHAR(50), + @TwoFactorProviders NVARCHAR(MAX), + @TwoFactorRecoveryCode NVARCHAR(32), + @EquivalentDomains NVARCHAR(MAX), + @ExcludedGlobalEquivalentDomains NVARCHAR(MAX), + @AccountRevisionDate DATETIME2(7), + @Key NVARCHAR(MAX), + @PublicKey NVARCHAR(MAX), + @PrivateKey NVARCHAR(MAX), + @Premium BIT, + @PremiumExpirationDate DATETIME2(7), + @RenewalReminderDate DATETIME2(7), + @Storage BIGINT, + @MaxStorageGb SMALLINT, + @Gateway TINYINT, + @GatewayCustomerId VARCHAR(50), + @GatewaySubscriptionId VARCHAR(50), + @ReferenceData VARCHAR(MAX), + @LicenseKey VARCHAR(100), + @Kdf TINYINT, + @KdfIterations INT, + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7), + @ApiKey VARCHAR(30) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[User] + SET + [Name] = @Name, + [Email] = @Email, + [EmailVerified] = @EmailVerified, + [MasterPassword] = @MasterPassword, + [MasterPasswordHint] = @MasterPasswordHint, + [Culture] = @Culture, + [SecurityStamp] = @SecurityStamp, + [TwoFactorProviders] = @TwoFactorProviders, + [TwoFactorRecoveryCode] = @TwoFactorRecoveryCode, + [EquivalentDomains] = @EquivalentDomains, + [ExcludedGlobalEquivalentDomains] = @ExcludedGlobalEquivalentDomains, + [AccountRevisionDate] = @AccountRevisionDate, + [Key] = @Key, + [PublicKey] = @PublicKey, + [PrivateKey] = @PrivateKey, + [Premium] = @Premium, + [PremiumExpirationDate] = @PremiumExpirationDate, + [RenewalReminderDate] = @RenewalReminderDate, + [Storage] = @Storage, + [MaxStorageGb] = @MaxStorageGb, + [Gateway] = @Gateway, + [GatewayCustomerId] = @GatewayCustomerId, + [GatewaySubscriptionId] = @GatewaySubscriptionId, + [ReferenceData] = @ReferenceData, + [LicenseKey] = @LicenseKey, + [Kdf] = @Kdf, + [KdfIterations] = @KdfIterations, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate, + [ApiKey] = @ApiKey + WHERE + [Id] = @Id +END +GO + +-- Recreate procedure User_Search +IF OBJECT_ID('[dbo].[User_Search]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[User_Search] +END +GO + +CREATE PROCEDURE [dbo].[User_Search] + @Email NVARCHAR(256), + @Skip INT = 0, + @Take INT = 25 +WITH RECOMPILE +AS +BEGIN + SET NOCOUNT ON + DECLARE @EmailLikeSearch NVARCHAR(261) = @Email + '%' + + SELECT + * + FROM + [dbo].[UserView] + WHERE + (@Email IS NULL OR [Email] LIKE @EmailLikeSearch) + ORDER BY [Email] ASC + OFFSET @Skip ROWS + FETCH NEXT @Take ROWS ONLY +END +GO + +-- Recreate procedure User_ReadByEmail +IF OBJECT_ID('[dbo].[User_ReadByEmail]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[User_ReadByEmail] +END +GO + +CREATE PROCEDURE [dbo].[User_ReadByEmail] + @Email NVARCHAR(256) +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[UserView] + WHERE + [Email] = @Email +END +GO + +-- Recreate procedure User_ReadKdfByEmail +IF OBJECT_ID('[dbo].[User_ReadKdfByEmail]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[User_ReadKdfByEmail] +END +GO + +CREATE PROCEDURE [dbo].[User_ReadKdfByEmail] + @Email NVARCHAR(256) +AS +BEGIN + SET NOCOUNT ON + + SELECT + [Kdf], + [KdfIterations] + FROM + [dbo].[User] + WHERE + [Email] = @Email +END +GO \ No newline at end of file diff --git a/util/Migrator/DbScripts/2021-03-04_02_Organization_Enlarge_Email_Column.sql b/util/Migrator/DbScripts/2021-03-04_02_Organization_Enlarge_Email_Column.sql new file mode 100644 index 000000000..aa066ac00 --- /dev/null +++ b/util/Migrator/DbScripts/2021-03-04_02_Organization_Enlarge_Email_Column.sql @@ -0,0 +1,300 @@ +-- Check email column is 50 characters long (100 bytes) +IF COL_LENGTH('[dbo].[Organization]', 'BillingEmail') = 100 +BEGIN + ALTER TABLE [dbo].[Organization] + ALTER COLUMN + BillingEmail NVARCHAR(256) NOT NULL +END +GO + +-- Recreate procedure Organization_Create +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(256), + @Plan NVARCHAR(50), + @PlanType TINYINT, + @Seats SMALLINT, + @MaxCollections SMALLINT, + @UsePolicies BIT, + @UseSso 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), + @ReferenceData VARCHAR(MAX), + @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], + [UseSso], + [UseGroups], + [UseDirectory], + [UseEvents], + [UseTotp], + [Use2fa], + [UseApi], + [SelfHost], + [UsersGetPremium], + [Storage], + [MaxStorageGb], + [Gateway], + [GatewayCustomerId], + [GatewaySubscriptionId], + [ReferenceData], + [Enabled], + [LicenseKey], + [ApiKey], + [TwoFactorProviders], + [ExpirationDate], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @Identifier, + @Name, + @BusinessName, + @BusinessAddress1, + @BusinessAddress2, + @BusinessAddress3, + @BusinessCountry, + @BusinessTaxNumber, + @BillingEmail, + @Plan, + @PlanType, + @Seats, + @MaxCollections, + @UsePolicies, + @UseSso, + @UseGroups, + @UseDirectory, + @UseEvents, + @UseTotp, + @Use2fa, + @UseApi, + @SelfHost, + @UsersGetPremium, + @Storage, + @MaxStorageGb, + @Gateway, + @GatewayCustomerId, + @GatewaySubscriptionId, + @ReferenceData, + @Enabled, + @LicenseKey, + @ApiKey, + @TwoFactorProviders, + @ExpirationDate, + @CreationDate, + @RevisionDate + ) +END +GO + +-- Recreate procedure Organization_Update +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(256), + @Plan NVARCHAR(50), + @PlanType TINYINT, + @Seats SMALLINT, + @MaxCollections SMALLINT, + @UsePolicies BIT, + @UseSso 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), + @ReferenceData VARCHAR(MAX), + @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, + [UseSso] = @UseSso, + [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, + [ReferenceData] = @ReferenceData, + [Enabled] = @Enabled, + [LicenseKey] = @LicenseKey, + [ApiKey] = @ApiKey, + [TwoFactorProviders] = @TwoFactorProviders, + [ExpirationDate] = @ExpirationDate, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id +END +GO + +-- Recreate procedure Organization_Search +IF OBJECT_ID('[dbo].[Organization_Search]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Organization_Search] +END +GO + +CREATE PROCEDURE [dbo].[Organization_Search] + @Name NVARCHAR(50), + @UserEmail NVARCHAR(256), + @Paid BIT, + @Skip INT = 0, + @Take INT = 25 +WITH RECOMPILE +AS +BEGIN + SET NOCOUNT ON + DECLARE @NameLikeSearch NVARCHAR(55) = '%' + @Name + '%' + + IF @UserEmail IS NOT NULL + BEGIN + SELECT + O.* + FROM + [dbo].[OrganizationView] O + INNER JOIN + [dbo].[OrganizationUser] OU ON O.[Id] = OU.[OrganizationId] + INNER JOIN + [dbo].[User] U ON U.[Id] = OU.[UserId] + WHERE + (@Name IS NULL OR O.[Name] LIKE @NameLikeSearch) + AND (@UserEmail IS NULL OR U.[Email] = @UserEmail) + AND + ( + @Paid IS NULL OR + ( + (@Paid = 1 AND O.[GatewaySubscriptionId] IS NOT NULL) OR + (@Paid = 0 AND O.[GatewaySubscriptionId] IS NULL) + ) + ) + ORDER BY O.[CreationDate] DESC + OFFSET @Skip ROWS + FETCH NEXT @Take ROWS ONLY + END + ELSE + BEGIN + SELECT + O.* + FROM + [dbo].[OrganizationView] O + WHERE + (@Name IS NULL OR O.[Name] LIKE @NameLikeSearch) + AND + ( + @Paid IS NULL OR + ( + (@Paid = 1 AND O.[GatewaySubscriptionId] IS NOT NULL) OR + (@Paid = 0 AND O.[GatewaySubscriptionId] IS NULL) + ) + ) + ORDER BY O.[CreationDate] DESC + OFFSET @Skip ROWS + FETCH NEXT @Take ROWS ONLY + END +END +GO \ No newline at end of file diff --git a/util/Migrator/DbScripts/2021-03-04_03_OrganizationUser_Enlarge_Email_Column.sql b/util/Migrator/DbScripts/2021-03-04_03_OrganizationUser_Enlarge_Email_Column.sql new file mode 100644 index 000000000..36bae407b --- /dev/null +++ b/util/Migrator/DbScripts/2021-03-04_03_OrganizationUser_Enlarge_Email_Column.sql @@ -0,0 +1,304 @@ +-- Check email column is 50 characters long (100 bytes) +IF COL_LENGTH('[dbo].[OrganizationUser]', 'Email') = 100 +BEGIN + ALTER TABLE [dbo].[OrganizationUser] + ALTER COLUMN + Email NVARCHAR(256) NOT NULL +END +GO + +-- Recreate procedure OrganizationUser_Create +IF OBJECT_ID('[dbo].[OrganizationUser_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationUser_Create] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationUser_Create] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @UserId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @Key VARCHAR(MAX), + @Status TINYINT, + @Type TINYINT, + @AccessAll BIT, + @ExternalId NVARCHAR(300), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7), + @Permissions NVARCHAR(MAX) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[OrganizationUser] + ( + [Id], + [OrganizationId], + [UserId], + [Email], + [Key], + [Status], + [Type], + [AccessAll], + [ExternalId], + [CreationDate], + [RevisionDate], + [Permissions] + ) + VALUES + ( + @Id, + @OrganizationId, + @UserId, + @Email, + @Key, + @Status, + @Type, + @AccessAll, + @ExternalId, + @CreationDate, + @RevisionDate, + @Permissions + ) +END +GO + +-- Recreate procedure OrganizationUser_Update +IF OBJECT_ID('[dbo].[OrganizationUser_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationUser_Update] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationUser_Update] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @UserId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @Key VARCHAR(MAX), + @Status TINYINT, + @Type TINYINT, + @AccessAll BIT, + @ExternalId NVARCHAR(300), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7), + @Permissions NVARCHAR(MAX) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[OrganizationUser] + SET + [OrganizationId] = @OrganizationId, + [UserId] = @UserId, + [Email] = @Email, + [Key] = @Key, + [Status] = @Status, + [Type] = @Type, + [AccessAll] = @AccessAll, + [ExternalId] = @ExternalId, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate, + [Permissions] = @Permissions + WHERE + [Id] = @Id + + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId +END +GO + +-- Recreate procedure OrganizationUser_CreateWithCollections +IF OBJECT_ID('[dbo].[OrganizationUser_CreateWithCollections]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationUser_CreateWithCollections] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationUser_CreateWithCollections] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @UserId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @Key VARCHAR(MAX), + @Status TINYINT, + @Type TINYINT, + @AccessAll BIT, + @ExternalId NVARCHAR(300), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7), + @Permissions NVARCHAR(MAX), + @Collections AS [dbo].[SelectionReadOnlyArray] READONLY +AS +BEGIN + SET NOCOUNT ON + + EXEC [dbo].[OrganizationUser_Create] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions + + ;WITH [AvailableCollectionsCTE] AS( + SELECT + [Id] + FROM + [dbo].[Collection] + WHERE + [OrganizationId] = @OrganizationId + ) + INSERT INTO [dbo].[CollectionUser] + ( + [CollectionId], + [OrganizationUserId], + [ReadOnly], + [HidePasswords] + ) + SELECT + [Id], + @Id, + [ReadOnly], + [HidePasswords] + FROM + @Collections + WHERE + [Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) +END +GO + +-- Recreate procedure OrganizationUser_UpdateWithCollections +IF OBJECT_ID('[dbo].[OrganizationUser_UpdateWithCollections]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationUser_UpdateWithCollections] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @UserId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @Key VARCHAR(MAX), + @Status TINYINT, + @Type TINYINT, + @AccessAll BIT, + @ExternalId NVARCHAR(300), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7), + @Permissions NVARCHAR(MAX), + @Collections AS [dbo].[SelectionReadOnlyArray] READONLY +AS +BEGIN + SET NOCOUNT ON + + EXEC [dbo].[OrganizationUser_Update] @Id, @OrganizationId, @UserId, @Email, @Key, @Status, @Type, @AccessAll, @ExternalId, @CreationDate, @RevisionDate, @Permissions + -- Update + UPDATE + [Target] + SET + [Target].[ReadOnly] = [Source].[ReadOnly], + [Target].[HidePasswords] = [Source].[HidePasswords] + FROM + [dbo].[CollectionUser] AS [Target] + INNER JOIN + @Collections AS [Source] ON [Source].[Id] = [Target].[CollectionId] + WHERE + [Target].[OrganizationUserId] = @Id + AND ( + [Target].[ReadOnly] != [Source].[ReadOnly] + OR [Target].[HidePasswords] != [Source].[HidePasswords] + ) + + -- Insert + INSERT INTO + [dbo].[CollectionUser] + SELECT + [Source].[Id], + @Id, + [Source].[ReadOnly], + [Source].[HidePasswords] + FROM + @Collections AS [Source] + INNER JOIN + [dbo].[Collection] C ON C.[Id] = [Source].[Id] AND C.[OrganizationId] = @OrganizationId + WHERE + NOT EXISTS ( + SELECT + 1 + FROM + [dbo].[CollectionUser] + WHERE + [CollectionId] = [Source].[Id] + AND [OrganizationUserId] = @Id + ) + + -- Delete + DELETE + CU + FROM + [dbo].[CollectionUser] CU + WHERE + CU.[OrganizationUserId] = @Id + AND NOT EXISTS ( + SELECT + 1 + FROM + @Collections + WHERE + [Id] = CU.[CollectionId] + ) +END +GO + +-- Recreate procedure OrganizationUser_ReadByOrganizationIdEmail +IF OBJECT_ID('[dbo].[OrganizationUser_ReadByOrganizationIdEmail]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationUser_ReadByOrganizationIdEmail] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationUser_ReadByOrganizationIdEmail] + @OrganizationId UNIQUEIDENTIFIER, + @Email NVARCHAR(256) +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[OrganizationUserView] + WHERE + [OrganizationId] = @OrganizationId + AND [Email] IS NOT NULL + AND @Email IS NOT NULL + AND [Email] = @Email +END +GO + +-- Recreate procedure OrganizationUser_ReadCountByOrganizationIdEmail +IF OBJECT_ID('[dbo].[OrganizationUser_ReadCountByOrganizationIdEmail]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationUser_ReadCountByOrganizationIdEmail] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationUser_ReadCountByOrganizationIdEmail] + @OrganizationId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @OnlyUsers BIT +AS +BEGIN + SET NOCOUNT ON + + SELECT + COUNT(1) + FROM + [dbo].[OrganizationUser] OU + LEFT JOIN + [dbo].[User] U ON OU.[UserId] = U.[Id] + WHERE + OU.[OrganizationId] = @OrganizationId + AND ( + (@OnlyUsers = 0 AND (OU.[Email] = @Email OR U.[Email] = @Email)) + OR (@OnlyUsers = 1 AND U.[Email] = @Email) + ) +END +GO \ No newline at end of file diff --git a/util/Migrator/DbScripts/2021-03-04_04_EmergencyAccess_Enlarge_Email_Column.sql b/util/Migrator/DbScripts/2021-03-04_04_EmergencyAccess_Enlarge_Email_Column.sql new file mode 100644 index 000000000..864057741 --- /dev/null +++ b/util/Migrator/DbScripts/2021-03-04_04_EmergencyAccess_Enlarge_Email_Column.sql @@ -0,0 +1,140 @@ +-- Check email column is 50 characters long (100 bytes) +IF COL_LENGTH('[dbo].[EmergencyAccess]', 'Email') = 100 +BEGIN + ALTER TABLE [dbo].[EmergencyAccess] + ALTER COLUMN + Email NVARCHAR(256) NOT NULL +END +GO + +-- Recreate procedure EmergencyAccess_Create +IF OBJECT_ID('[dbo].[EmergencyAccess_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[EmergencyAccess_Create] +END +GO + +CREATE PROCEDURE [dbo].[EmergencyAccess_Create] + @Id UNIQUEIDENTIFIER, + @GrantorId UNIQUEIDENTIFIER, + @GranteeId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @KeyEncrypted VARCHAR(MAX), + @Type TINYINT, + @Status TINYINT, + @WaitTimeDays SMALLINT, + @RecoveryInitiatedDate DATETIME2(7), + @LastNotificationDate DATETIME2(7), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[EmergencyAccess] + ( + [Id], + [GrantorId], + [GranteeId], + [Email], + [KeyEncrypted], + [Type], + [Status], + [WaitTimeDays], + [RecoveryInitiatedDate], + [LastNotificationDate], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @GrantorId, + @GranteeId, + @Email, + @KeyEncrypted, + @Type, + @Status, + @WaitTimeDays, + @RecoveryInitiatedDate, + @LastNotificationDate, + @CreationDate, + @RevisionDate + ) +END +GO + +-- Recreate procedure EmergencyAccess_Update +IF OBJECT_ID('[dbo].[EmergencyAccess_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[EmergencyAccess_Update] +END +GO + +CREATE PROCEDURE [dbo].[EmergencyAccess_Update] + @Id UNIQUEIDENTIFIER, + @GrantorId UNIQUEIDENTIFIER, + @GranteeId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @KeyEncrypted VARCHAR(MAX), + @Type TINYINT, + @Status TINYINT, + @WaitTimeDays SMALLINT, + @RecoveryInitiatedDate DATETIME2(7), + @LastNotificationDate DATETIME2(7), + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[EmergencyAccess] + SET + [GrantorId] = @GrantorId, + [GranteeId] = @GranteeId, + [Email] = @Email, + [KeyEncrypted] = @KeyEncrypted, + [Type] = @Type, + [Status] = @Status, + [WaitTimeDays] = @WaitTimeDays, + [RecoveryInitiatedDate] = @RecoveryInitiatedDate, + [LastNotificationDate] = @LastNotificationDate, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id + + EXEC [dbo].[User_BumpAccountRevisionDate] @GranteeId +END +GO + +-- Recreate procedure EmergencyAccess_ReadCountByGrantorIdEmail +IF OBJECT_ID('[dbo].[EmergencyAccess_ReadCountByGrantorIdEmail]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[EmergencyAccess_ReadCountByGrantorIdEmail] +END +GO + +CREATE PROCEDURE [dbo].[EmergencyAccess_ReadCountByGrantorIdEmail] + @GrantorId UNIQUEIDENTIFIER, + @Email NVARCHAR(256), + @OnlyUsers BIT +AS +BEGIN + SET NOCOUNT ON + + SELECT + COUNT(1) + FROM + [dbo].[EmergencyAccess] EA + LEFT JOIN + [dbo].[User] U ON EA.[GranteeId] = U.[Id] + WHERE + EA.[GrantorId] = @GrantorId + AND ( + (@OnlyUsers = 0 AND (EA.[Email] = @Email OR U.[Email] = @Email)) + OR (@OnlyUsers = 1 AND U.[Email] = @Email) + ) +END +GO \ No newline at end of file