diff --git a/src/Sql/dbo/Stored Procedures/Organization_Create.sql b/src/Sql/dbo/Stored Procedures/Organization_Create.sql
index a12b2bf117..4ddb006234 100644
--- a/src/Sql/dbo/Stored Procedures/Organization_Create.sql
+++ b/src/Sql/dbo/Stored Procedures/Organization_Create.sql
@@ -17,6 +17,7 @@
@UseEvents BIT,
@UseTotp BIT,
@Use2fa BIT,
+ @UseApi BIT,
@SelfHost BIT,
@UsersGetPremium BIT,
@Storage BIGINT,
@@ -26,6 +27,7 @@
@GatewaySubscriptionId VARCHAR(50),
@Enabled BIT,
@LicenseKey VARCHAR(100),
+ @ApiKey VARCHAR(30),
@TwoFactorProviders NVARCHAR(MAX),
@ExpirationDate DATETIME2(7),
@CreationDate DATETIME2(7),
@@ -54,6 +56,7 @@ BEGIN
[UseEvents],
[UseTotp],
[Use2fa],
+ [UseApi],
[SelfHost],
[UsersGetPremium],
[Storage],
@@ -63,6 +66,7 @@ BEGIN
[GatewaySubscriptionId],
[Enabled],
[LicenseKey],
+ [ApiKey],
[TwoFactorProviders],
[ExpirationDate],
[CreationDate],
@@ -88,6 +92,7 @@ BEGIN
@UseEvents,
@UseTotp,
@Use2fa,
+ @UseApi,
@SelfHost,
@UsersGetPremium,
@Storage,
@@ -97,6 +102,7 @@ BEGIN
@GatewaySubscriptionId,
@Enabled,
@LicenseKey,
+ @ApiKey,
@TwoFactorProviders,
@ExpirationDate,
@CreationDate,
diff --git a/src/Sql/dbo/Stored Procedures/Organization_Update.sql b/src/Sql/dbo/Stored Procedures/Organization_Update.sql
index bd933c9bb3..0d9f64a655 100644
--- a/src/Sql/dbo/Stored Procedures/Organization_Update.sql
+++ b/src/Sql/dbo/Stored Procedures/Organization_Update.sql
@@ -17,6 +17,7 @@
@UseEvents BIT,
@UseTotp BIT,
@Use2fa BIT,
+ @UseApi BIT,
@SelfHost BIT,
@UsersGetPremium BIT,
@Storage BIGINT,
@@ -26,6 +27,7 @@
@GatewaySubscriptionId VARCHAR(50),
@Enabled BIT,
@LicenseKey VARCHAR(100),
+ @ApiKey VARCHAR(30),
@TwoFactorProviders NVARCHAR(MAX),
@ExpirationDate DATETIME2(7),
@CreationDate DATETIME2(7),
@@ -54,6 +56,7 @@ BEGIN
[UseEvents] = @UseEvents,
[UseTotp] = @UseTotp,
[Use2fa] = @Use2fa,
+ [UseApi] = @UseApi,
[SelfHost] = @SelfHost,
[UsersGetPremium] = @UsersGetPremium,
[Storage] = @Storage,
@@ -63,6 +66,7 @@ BEGIN
[GatewaySubscriptionId] = @GatewaySubscriptionId,
[Enabled] = @Enabled,
[LicenseKey] = @LicenseKey,
+ [ApiKey] = @ApiKey,
[TwoFactorProviders] = @TwoFactorProviders,
[ExpirationDate] = @ExpirationDate,
[CreationDate] = @CreationDate,
diff --git a/src/Sql/dbo/Tables/Organization.sql b/src/Sql/dbo/Tables/Organization.sql
index 6601fe6830..72ff576ff1 100644
--- a/src/Sql/dbo/Tables/Organization.sql
+++ b/src/Sql/dbo/Tables/Organization.sql
@@ -17,6 +17,7 @@
[UseEvents] BIT NOT NULL,
[UseTotp] BIT NOT NULL,
[Use2fa] BIT NOT NULL,
+ [UseApi] BIT NOT NULL,
[SelfHost] BIT NOT NULL,
[UsersGetPremium] BIT NOT NULL,
[Storage] BIGINT NULL,
@@ -26,6 +27,7 @@
[GatewaySubscriptionId] VARCHAR (50) NULL,
[Enabled] BIT NOT NULL,
[LicenseKey] VARCHAR (100) NULL,
+ [ApiKey] VARCHAR (30) NOT NULL,
[TwoFactorProviders] NVARCHAR (MAX) NULL,
[ExpirationDate] DATETIME2 (7) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
diff --git a/src/Sql/dbo/Views/OrganizationUserOrganizationDetailsView.sql b/src/Sql/dbo/Views/OrganizationUserOrganizationDetailsView.sql
index bf8bd6c589..4b24cd4342 100644
--- a/src/Sql/dbo/Views/OrganizationUserOrganizationDetailsView.sql
+++ b/src/Sql/dbo/Views/OrganizationUserOrganizationDetailsView.sql
@@ -10,6 +10,7 @@ SELECT
O.[UseEvents],
O.[UseTotp],
O.[Use2fa],
+ O.[UseApi],
O.[SelfHost],
O.[UsersGetPremium],
O.[Seats],
diff --git a/util/Setup/DbScripts/2019-03-01_00_OrgApi.sql b/util/Setup/DbScripts/2019-03-01_00_OrgApi.sql
new file mode 100644
index 0000000000..ca0b9cc5e1
--- /dev/null
+++ b/util/Setup/DbScripts/2019-03-01_00_OrgApi.sql
@@ -0,0 +1,339 @@
+-- Setup for random string generation
+
+CREATE VIEW [dbo].[SecureRandomBytes]
+AS
+SELECT [RandBytes] = CRYPT_GEN_RANDOM(2)
+GO
+
+CREATE FUNCTION [dbo].[SecureRandomString]()
+RETURNS varchar(30)
+AS
+BEGIN
+ declare @sLength tinyint
+ declare @randomString varchar(30)
+ declare @counter tinyint
+ declare @nextChar char(1)
+ declare @rnd as float
+ declare @bytes binary(2)
+
+ set @sLength = 30
+ set @counter = 1
+ set @randomString = ''
+
+ while @counter <= @sLength
+ begin
+ select @bytes = [RandBytes] from [dbo].[SecureRandomBytes]
+ select @rnd = cast(cast(cast(@bytes as int) as float) / 65535 as float)
+ select @nextChar = char(48 + convert(int, (122-48+1) * @rnd))
+ if ascii(@nextChar) not in (58,59,60,61,62,63,64,91,92,93,94,95,96)
+ begin
+ select @randomString = @randomString + @nextChar
+ set @counter = @counter + 1
+ end
+ end
+ return @randomString
+END
+GO
+
+-- End setup
+
+IF COL_LENGTH('[dbo].[Organization]', 'UseApi') IS NULL
+BEGIN
+ ALTER TABLE
+ [dbo].[Organization]
+ ADD
+ [UseApi] BIT NULL
+END
+GO
+
+UPDATE
+ [dbo].[Organization]
+SET
+ [UseApi] = (CASE WHEN [PlanType] = 5 OR [PlanType] = 4 THEN 1 ELSE 0 END)
+GO
+
+ALTER TABLE
+ [dbo].[Organization]
+ALTER COLUMN
+ [UseApi] BIT NOT NULL
+GO
+
+IF COL_LENGTH('[dbo].[Organization]', 'ApiKey') IS NULL
+BEGIN
+ ALTER TABLE
+ [dbo].[Organization]
+ ADD
+ [ApiKey] VARCHAR(30) NULL
+END
+GO
+
+UPDATE
+ [dbo].[Organization]
+SET
+ [ApiKey] = (SELECT [dbo].[SecureRandomString]())
+GO
+
+ALTER TABLE
+ [dbo].[Organization]
+ALTER COLUMN
+ [ApiKey] VARCHAR(30) NOT NULL
+GO
+
+-- Cleanup random string generation
+
+DROP VIEW [dbo].[SecureRandomBytes]
+GO
+DROP FUNCTION [dbo].[SecureRandomString]
+GO
+
+-- End
+
+IF OBJECT_ID('[dbo].[Organization_Create]') IS NOT NULL
+BEGIN
+ DROP PROCEDURE [dbo].[Organization_Create]
+END
+GO
+
+CREATE PROCEDURE [dbo].[Organization_Create]
+ @Id UNIQUEIDENTIFIER,
+ @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,
+ @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],
+ [Name],
+ [BusinessName],
+ [BusinessAddress1],
+ [BusinessAddress2],
+ [BusinessAddress3],
+ [BusinessCountry],
+ [BusinessTaxNumber],
+ [BillingEmail],
+ [Plan],
+ [PlanType],
+ [Seats],
+ [MaxCollections],
+ [UseGroups],
+ [UseDirectory],
+ [UseEvents],
+ [UseTotp],
+ [Use2fa],
+ [UseApi],
+ [SelfHost],
+ [UsersGetPremium],
+ [Storage],
+ [MaxStorageGb],
+ [Gateway],
+ [GatewayCustomerId],
+ [GatewaySubscriptionId],
+ [Enabled],
+ [LicenseKey],
+ [ApiKey],
+ [TwoFactorProviders],
+ [ExpirationDate],
+ [CreationDate],
+ [RevisionDate]
+ )
+ VALUES
+ (
+ @Id,
+ @Name,
+ @BusinessName,
+ @BusinessAddress1,
+ @BusinessAddress2,
+ @BusinessAddress3,
+ @BusinessCountry,
+ @BusinessTaxNumber,
+ @BillingEmail,
+ @Plan,
+ @PlanType,
+ @Seats,
+ @MaxCollections,
+ @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,
+ @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,
+ @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
+ [Name] = @Name,
+ [BusinessName] = @BusinessName,
+ [BusinessAddress1] = @BusinessAddress1,
+ [BusinessAddress2] = @BusinessAddress2,
+ [BusinessAddress3] = @BusinessAddress3,
+ [BusinessCountry] = @BusinessCountry,
+ [BusinessTaxNumber] = @BusinessTaxNumber,
+ [BillingEmail] = @BillingEmail,
+ [Plan] = @Plan,
+ [PlanType] = @PlanType,
+ [Seats] = @Seats,
+ [MaxCollections] = @MaxCollections,
+ [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
+
+IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'OrganizationView')
+BEGIN
+ DROP VIEW [dbo].[OrganizationView]
+END
+GO
+
+CREATE VIEW [dbo].[OrganizationView]
+AS
+SELECT
+ *
+FROM
+ [dbo].[Organization]
+GO
+
+IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'OrganizationUserOrganizationDetailsView')
+BEGIN
+ DROP VIEW [dbo].[OrganizationUserOrganizationDetailsView]
+END
+GO
+
+CREATE VIEW [dbo].[OrganizationUserOrganizationDetailsView]
+AS
+SELECT
+ OU.[UserId],
+ OU.[OrganizationId],
+ O.[Name],
+ O.[Enabled],
+ O.[UseGroups],
+ O.[UseDirectory],
+ O.[UseEvents],
+ O.[UseTotp],
+ O.[Use2fa],
+ O.[UseApi],
+ O.[SelfHost],
+ O.[UsersGetPremium],
+ O.[Seats],
+ O.[MaxCollections],
+ O.[MaxStorageGb],
+ OU.[Key],
+ OU.[Status],
+ OU.[Type]
+FROM
+ [dbo].[OrganizationUser] OU
+INNER JOIN
+ [dbo].[Organization] O ON O.[Id] = OU.[OrganizationId]
+GO
diff --git a/util/Setup/Setup.csproj b/util/Setup/Setup.csproj
index 6d75649437..5c8fef3157 100644
--- a/util/Setup/Setup.csproj
+++ b/util/Setup/Setup.csproj
@@ -12,13 +12,6 @@
-
-
-
-
-
-
-