1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

Org admin cleanup

- Added sproc to check if org admin for free org create
- Removed old proeprties not in user from org and subvault
- Added more cascade deletes
This commit is contained in:
Kyle Spearrin 2017-04-07 14:52:31 -04:00
parent 52ccef85c6
commit 7497d5ca6f
29 changed files with 47 additions and 98 deletions

View File

@ -44,14 +44,12 @@ namespace Bit.Core.Models.Api
{
[Required]
public string SubvaultId { get; set; }
public bool Admin { get; set; }
public bool ReadOnly { get; set; }
public SubvaultUser ToSubvaultUser()
{
var subvault = new SubvaultUser
{
Admin = Admin,
ReadOnly = ReadOnly,
SubvaultId = new Guid(SubvaultId)
};

View File

@ -1,9 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@ -20,7 +16,6 @@ namespace Bit.Core.Models.Api
{
OrganizationUserId = new Guid(UserId),
SubvaultId = new Guid(s.SubvaultId),
Admin = s.Admin,
ReadOnly = s.ReadOnly
});
}
@ -28,7 +23,6 @@ namespace Bit.Core.Models.Api
public class Subvault
{
public string SubvaultId { get; set; }
public bool Admin { get; set; }
public bool ReadOnly { get; set; }
}
}
@ -36,7 +30,6 @@ namespace Bit.Core.Models.Api
public class SubvaultUserUserRequestModel
{
public string UserId { get; set; }
public bool Admin { get; set; }
public bool ReadOnly { get; set; }
}
}

View File

@ -23,7 +23,6 @@ namespace Bit.Core.Models.Api
BillingEmail = organization.BillingEmail;
Plan = organization.Plan;
PlanType = organization.PlanType;
PlanTrial = organization.PlanTrial;
MaxUsers = organization.MaxUsers;
}
@ -33,7 +32,6 @@ namespace Bit.Core.Models.Api
public string BillingEmail { get; set; }
public string Plan { get; set; }
public Enums.PlanType PlanType { get; set; }
public bool PlanTrial { get; set; }
public short MaxUsers { get; set; }
}

View File

@ -18,13 +18,11 @@ namespace Bit.Core.Models.Api
Name = details.Name;
SubvaultId = details.SubvaultId.ToString();
ReadOnly = details.ReadOnly;
Admin = details.Admin;
}
public string Id { get; set; }
public string Name { get; set; }
public string SubvaultId { get; set; }
public bool ReadOnly { get; set; }
public bool Admin { get; set; }
}
}

View File

@ -23,7 +23,6 @@ namespace Bit.Core.Models.Api
Type = subvaultUser.Type;
Status = subvaultUser.Status;
ReadOnly = subvaultUser.ReadOnly;
Admin = subvaultUser.Admin;
}
public string Id { get; set; }
@ -34,6 +33,5 @@ namespace Bit.Core.Models.Api
public OrganizationUserType Type { get; set; }
public OrganizationUserStatusType Status { get; set; }
public bool ReadOnly { get; set; }
public bool Admin { get; set; }
}
}

View File

@ -6,6 +6,5 @@ namespace Bit.Core.Models.Data
{
public Guid SubvaultId { get; set; }
public bool ReadOnly { get; set; }
public bool Admin { get; set; }
}
}

View File

@ -9,6 +9,5 @@ namespace Bit.Core.Models.Data
public string Name { get; set; }
public Guid SubvaultId { get; set; }
public bool ReadOnly { get; set; }
public bool Admin { get; set; }
}
}

View File

@ -12,6 +12,5 @@ namespace Bit.Core.Models.Data
public Enums.OrganizationUserStatusType Status { get; set; }
public Enums.OrganizationUserType Type { get; set; }
public bool ReadOnly { get; set; }
public bool Admin { get; set; }
}
}

View File

@ -13,12 +13,6 @@ namespace Bit.Core.Models.Table
public string BillingEmail { get; set; }
public string Plan { get; set; }
public PlanType PlanType { get; set; }
public decimal PlanBasePrice { get; set; }
public decimal PlanUserPrice { get; set; }
public DateTime? PlanRenewalDate { get; set; }
public bool PlanTrial { get; set; }
public short BaseUsers { get; set; }
public short AdditionalUsers { get; set; }
public short MaxUsers { get; set; }
public string StripeCustomerId { get; set; }
public string StripeSubscriptionId { get; set; }

View File

@ -8,7 +8,6 @@ namespace Bit.Core.Models.Table
public Guid Id { get; set; }
public Guid SubvaultId { get; set; }
public Guid OrganizationUserId { get; set; }
public bool Admin { get; set; }
public bool ReadOnly { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -9,6 +9,7 @@ namespace Bit.Core.Repositories
{
public interface IOrganizationUserRepository : IRepository<OrganizationUser, Guid>
{
Task<int> GetCountByFreeOrganizationAdminUserAsync(Guid userId);
Task<ICollection<OrganizationUser>> GetManyByUserAsync(Guid userId);
Task<ICollection<OrganizationUser>> GetManyByOrganizationAsync(Guid organizationId, OrganizationUserType? type);
Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, string email);

View File

@ -21,6 +21,19 @@ namespace Bit.Core.Repositories.SqlServer
: base(connectionString)
{ }
public async Task<int> GetCountByFreeOrganizationAdminUserAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteScalarAsync<int>(
"[dbo].[OrganizationUser_ReadCountByFreeOrganizationAdminUser]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results;
}
}
public async Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, string email)
{
using(var connection = new SqlConnection(ConnectionString))

View File

@ -89,9 +89,9 @@ namespace Bit.Core.Services
if(plan.Type == Enums.PlanType.Free)
{
var ownerExistingOrgs = await _organizationUserRepository.GetManyByUserAsync(signup.Owner.Id);
if(ownerExistingOrgs.Any(ou => ou.Type == Enums.OrganizationUserType.Owner ||
ou.Type == Enums.OrganizationUserType.Admin))
var ownerExistingOrgCount =
await _organizationUserRepository.GetCountByFreeOrganizationAdminUserAsync(signup.Owner.Id);
if(ownerExistingOrgCount > 0)
{
throw new BadRequestException("You can only be an admin of one free organization.");
}
@ -136,13 +136,7 @@ namespace Bit.Core.Services
BusinessName = signup.BusinessName,
UserId = signup.Owner.Id,
PlanType = plan.Type,
BaseUsers = plan.BaseUsers,
AdditionalUsers = (short)(plan.CanBuyAdditionalUsers ? signup.AdditionalUsers : 0),
MaxUsers = (short)(plan.BaseUsers + (plan.CanBuyAdditionalUsers ? signup.AdditionalUsers : 0)),
PlanTrial = plan.Trial.HasValue,
PlanBasePrice = plan.CanMonthly && signup.Monthly ? plan.BaseMonthlyPrice : plan.BaseAnnualPrice,
PlanUserPrice = plan.CanMonthly && signup.Monthly ? plan.UserMonthlyPrice : plan.UserAnnualPrice,
PlanRenewalDate = subscription?.CurrentPeriodEnd,
Plan = plan.ToString(),
StripeCustomerId = customer?.Id,
StripeSubscriptionId = subscription?.Id,
@ -254,8 +248,8 @@ namespace Bit.Core.Services
throw new BadRequestException("Already accepted.");
}
var existingOrgs = await _organizationUserRepository.GetManyByUserAsync(user.Id);
if(existingOrgs.Any(ou => ou.Type == Enums.OrganizationUserType.Owner || ou.Type == Enums.OrganizationUserType.Admin))
var ownerExistingOrgCount = await _organizationUserRepository.GetCountByFreeOrganizationAdminUserAsync(user.Id);
if(ownerExistingOrgCount > 0)
{
throw new BadRequestException("You can only be an admin of one free organization.");
}

View File

@ -99,8 +99,11 @@
<Build Include="dbo\Views\SubvaultView.sql" />
<Build Include="dbo\Functions\UserCanEditCipher.sql" />
<Build Include="dbo\Functions\CipherDetails.sql" />
<Build Include="dbo\Stored Procedures\SubvaultCipher_ReadByUserIdCipherId.sql" />
<Build Include="dbo\Stored Procedures\Subvault_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\SubvaultUser_ReadByOrganizationUserId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByFreeOrganizationAdminUser.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Create.sql" />
<Build Include="dbo\Stored Procedures\Cipher_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Favorite_Create.sql" />
@ -178,7 +181,5 @@
<Build Include="dbo\Stored Procedures\OrganizationUserUserDetails_ReadById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUserUserDetails_ReadByOrganizationId.sql" />
<Build Include="dbo\User Defined Types\GuidIdArray.sql" />
<Build Include="dbo\Stored Procedures\SubvaultCipher_ReadByCipherId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadByUserId.sql" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,17 @@
CREATE PROCEDURE [dbo].[OrganizationUser_ReadCountByFreeOrganizationAdminUser]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
COUNT(1)
FROM
[dbo].[OrganizationUser] OU
INNER JOIN
[dbo].[Organization] O ON O.Id = OU.[OrganizationId]
WHERE
OU.[UserId] = @UserId
AND OU.[Type] < 2 -- Owner or Admin
AND O.[PlanType] = 0 -- Free
END

View File

@ -1,17 +1,10 @@
CREATE PROCEDURE [dbo].[Organization_Create]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@Name NVARCHAR(50),
@BusinessName NVARCHAR(50),
@BillingEmail NVARCHAR(50),
@Plan NVARCHAR(20),
@PlanType TINYINT,
@PlanBasePrice MONEY,
@PlanUserPrice MONEY,
@PlanRenewalDate DATETIME2(7),
@PlanTrial BIT,
@BaseUsers SMALLINT,
@AdditionalUsers SMALLINT,
@MaxUsers SMALLINT,
@StripeCustomerId VARCHAR(50),
@StripeSubscriptionId VARCHAR(50),
@ -24,18 +17,11 @@ BEGIN
INSERT INTO [dbo].[Organization]
(
[Id],
[UserId],
[Name],
[BusinessName],
[BillingEmail],
[Plan],
[PlanType],
[PlanBasePrice],
[PlanUserPrice],
[PlanRenewalDate],
[PlanTrial],
[BaseUsers],
[AdditionalUsers],
[MaxUsers],
[StripeCustomerId],
[StripeSubscriptionId],
@ -45,18 +31,11 @@ BEGIN
VALUES
(
@Id,
@UserId,
@Name,
@BusinessName,
@BillingEmail,
@Plan,
@PlanType,
@PlanBasePrice,
@PlanUserPrice,
@PlanRenewalDate,
@PlanTrial,
@BaseUsers,
@AdditionalUsers,
@MaxUsers,
@StripeCustomerId,
@StripeSubscriptionId,

View File

@ -1,17 +1,10 @@
CREATE PROCEDURE [dbo].[Organization_Update]
@Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER,
@Name NVARCHAR(50),
@BusinessName NVARCHAR(50),
@BillingEmail NVARCHAR(50),
@Plan NVARCHAR(20),
@PlanType TINYINT,
@PlanBasePrice MONEY,
@PlanUserPrice MONEY,
@PlanRenewalDate DATETIME2(7),
@PlanTrial BIT,
@BaseUsers SMALLINT,
@AdditionalUsers SMALLINT,
@MaxUsers SMALLINT,
@StripeCustomerId VARCHAR(50),
@StripeSubscriptionId VARCHAR(50),
@ -25,18 +18,11 @@ BEGIN
UPDATE
[dbo].[Organization]
SET
[UserId] = @UserId,
[Name] = @Name,
[BusinessName] = @BusinessName,
[BillingEmail] = @BillingEmail,
[Plan] = @Plan,
[PlanType] = @PlanType,
[PlanBasePrice] = @PlanBasePrice,
[PlanUserPrice] = @PlanUserPrice,
[PlanRenewalDate] = @PlanRenewalDate,
[PlanTrial] = @PlanTrial,
[BaseUsers] = @BaseUsers,
[AdditionalUsers] = @AdditionalUsers,
[MaxUsers] = @MaxUsers,
[StripeCustomerId] = @StripeCustomerId,
[StripeSubscriptionId] = @StripeSubscriptionId,

View File

@ -2,7 +2,6 @@
@Id UNIQUEIDENTIFIER,
@SubvaultId UNIQUEIDENTIFIER,
@OrganizationUserId UNIQUEIDENTIFIER,
@Admin BIT,
@ReadOnly BIT,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@ -15,7 +14,6 @@ BEGIN
[Id],
[SubvaultId],
[OrganizationUserId],
[Admin],
[ReadOnly],
[CreationDate],
[RevisionDate]
@ -25,7 +23,6 @@ BEGIN
@Id,
@SubvaultId,
@OrganizationUserId,
@Admin,
@ReadOnly,
@CreationDate,
@RevisionDate

View File

@ -8,7 +8,6 @@ BEGIN
SELECT
SU.[SubvaultId],
CASE WHEN OU.[Type] = 2 THEN SU.[Admin] ELSE 1 END AS [Admin], -- 2 = Regular User
SU.[ReadOnly]
FROM
[dbo].[SubvaultUser] SU

View File

@ -2,7 +2,6 @@
@Id UNIQUEIDENTIFIER,
@SubvaultId UNIQUEIDENTIFIER,
@OrganizationUserId UNIQUEIDENTIFIER,
@Admin BIT,
@ReadOnly BIT,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@ -15,7 +14,6 @@ BEGIN
SET
[SubvaultId] = @SubvaultId,
[OrganizationUserId] = @OrganizationUserId,
[Admin] = @Admin,
[ReadOnly] = @ReadOnly,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate

View File

@ -5,6 +5,6 @@
[CreationDate] DATETIME NOT NULL,
[RevisionDate] DATETIME NOT NULL,
CONSTRAINT [PK_Group] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Group_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id])
CONSTRAINT [FK_Group_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE
);

View File

@ -1,23 +1,15 @@
CREATE TABLE [dbo].[Organization] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[UserId] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[BusinessName] NVARCHAR (50) NULL,
[BillingEmail] NVARCHAR (50) NOT NULL,
[Plan] NVARCHAR (20) NOT NULL,
[PlanType] TINYINT NOT NULL,
[PlanBasePrice] MONEY NOT NULL,
[PlanUserPrice] MONEY NOT NULL,
[PlanRenewalDate] DATETIME2 (7) NULL,
[PlanTrial] BIT NOT NULL,
[BaseUsers] SMALLINT NULL,
[AdditionalUsers] SMALLINT NULL,
[MaxUsers] SMALLINT NULL,
[StripeCustomerId] VARCHAR (50) NULL,
[StripeSubscriptionId] VARCHAR (50) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_Organization] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Organization_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
CONSTRAINT [PK_Organization] PRIMARY KEY CLUSTERED ([Id] ASC)
);

View File

@ -9,7 +9,7 @@
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_OrganizationUser] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_OrganizationUser_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
CONSTRAINT [FK_OrganizationUser_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_OrganizationUser_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
);

View File

@ -5,6 +5,6 @@
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_Subvault] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Subvault_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id])
CONSTRAINT [FK_Subvault_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE
);

View File

@ -3,6 +3,6 @@
[GroupId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [PK_SubvaultGroup] PRIMARY KEY CLUSTERED ([SubvaultId] ASC, [GroupId] ASC),
CONSTRAINT [FK_SubvaultGroup_Group] FOREIGN KEY ([GroupId]) REFERENCES [dbo].[Group] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_SubvaultGroup_Subvault] FOREIGN KEY ([SubvaultId]) REFERENCES [dbo].[Subvault] ([Id]) ON DELETE CASCADE
CONSTRAINT [FK_SubvaultGroup_Subvault] FOREIGN KEY ([SubvaultId]) REFERENCES [dbo].[Subvault] ([Id])
);

View File

@ -2,12 +2,11 @@
[Id] UNIQUEIDENTIFIER NOT NULL,
[SubvaultId] UNIQUEIDENTIFIER NOT NULL,
[OrganizationUserId] UNIQUEIDENTIFIER NOT NULL,
[Admin] BIT NOT NULL,
[ReadOnly] BIT NOT NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_SubvaultUser] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_SubvaultUser_OrganizationUser] FOREIGN KEY ([OrganizationUserId]) REFERENCES [dbo].[OrganizationUser] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_SubvaultUser_OrganizationUser] FOREIGN KEY ([OrganizationUserId]) REFERENCES [dbo].[OrganizationUser] ([Id]),
CONSTRAINT [FK_SubvaultUser_Subvault] FOREIGN KEY ([SubvaultId]) REFERENCES [dbo].[Subvault] ([Id]) ON DELETE CASCADE
);

View File

@ -5,8 +5,7 @@ SELECT
SU.[OrganizationUserId],
S.[Name],
S.[Id] SubvaultId,
SU.[ReadOnly],
SU.[Admin]
SU.[ReadOnly]
FROM
[dbo].[SubvaultUser] SU
INNER JOIN

View File

@ -8,8 +8,7 @@ SELECT
ISNULL(U.[Email], OU.[Email]) Email,
OU.[Status],
OU.[Type],
SU.[ReadOnly],
SU.[Admin]
SU.[ReadOnly]
FROM
[dbo].[SubvaultUser] SU
INNER JOIN