1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

recompute full storage each time

This commit is contained in:
Kyle Spearrin 2017-07-10 22:08:52 -04:00
parent de8b2de8e6
commit 8684b9c8e5
10 changed files with 70 additions and 53 deletions

View File

@ -8,6 +8,6 @@ namespace Bit.Core.Repositories
public interface IOrganizationRepository : IRepository<Organization, Guid> public interface IOrganizationRepository : IRepository<Organization, Guid>
{ {
Task<ICollection<Organization>> GetManyByUserIdAsync(Guid userId); Task<ICollection<Organization>> GetManyByUserIdAsync(Guid userId);
Task UpdateStorageAsync(Guid id, long storageIncrease); Task UpdateStorageAsync(Guid id);
} }
} }

View File

@ -9,6 +9,6 @@ namespace Bit.Core.Repositories
Task<User> GetByEmailAsync(string email); Task<User> GetByEmailAsync(string email);
Task<string> GetPublicKeyAsync(Guid id); Task<string> GetPublicKeyAsync(Guid id);
Task<DateTime> GetAccountRevisionDateAsync(Guid id); Task<DateTime> GetAccountRevisionDateAsync(Guid id);
Task UpdateStorageAsync(Guid id, long storageIncrease); Task UpdateStorageAsync(Guid id);
} }
} }

View File

@ -32,13 +32,13 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task UpdateStorageAsync(Guid id, long storageIncrease) public async Task UpdateStorageAsync(Guid id)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
await connection.ExecuteAsync( await connection.ExecuteAsync(
"[dbo].[Organization_UpdateStorage]", "[dbo].[Organization_UpdateStorage]",
new { Id = id, StorageIncrease = storageIncrease }, new { Id = id },
commandType: CommandType.StoredProcedure, commandType: CommandType.StoredProcedure,
commandTimeout: 180); commandTimeout: 180);
} }

View File

@ -79,13 +79,13 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task UpdateStorageAsync(Guid id, long storageIncrease) public async Task UpdateStorageAsync(Guid id)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
await connection.ExecuteAsync( await connection.ExecuteAsync(
$"[{Schema}].[{Table}_UpdateStorage]", $"[{Schema}].[{Table}_UpdateStorage]",
new { Id = id, StorageIncrease = storageIncrease }, new { Id = id },
commandType: CommandType.StoredProcedure, commandType: CommandType.StoredProcedure,
commandTimeout: 180); commandTimeout: 180);
} }

View File

@ -302,7 +302,6 @@ namespace Bit.Core.Services
{ {
var attachments = cipher.GetAttachments(); var attachments = cipher.GetAttachments();
var hasAttachments = (attachments?.Count ?? 0) > 0; var hasAttachments = (attachments?.Count ?? 0) > 0;
var storageAdjustment = attachments?.Sum(a => a.Value.Size) ?? 0;
var updatedCipher = false; var updatedCipher = false;
var migratedAttachments = false; var migratedAttachments = false;
@ -329,8 +328,8 @@ namespace Bit.Core.Services
throw new BadRequestException("This organization cannot use attachments."); throw new BadRequestException("This organization cannot use attachments.");
} }
var storageBytesRemaining = org.StorageBytesRemaining(); var storageAdjustment = attachments?.Sum(a => a.Value.Size) ?? 0;
if(storageBytesRemaining < storageAdjustment) if(org.StorageBytesRemaining() < storageAdjustment)
{ {
throw new BadRequestException("Not enough storage available for this organization."); throw new BadRequestException("Not enough storage available for this organization.");
} }
@ -367,8 +366,8 @@ namespace Bit.Core.Services
if(updatedCipher) if(updatedCipher)
{ {
await _userRepository.UpdateStorageAsync(sharingUserId, storageAdjustment); await _userRepository.UpdateStorageAsync(sharingUserId);
await _organizationRepository.UpdateStorageAsync(organizationId, -1 * storageAdjustment); await _organizationRepository.UpdateStorageAsync(organizationId);
} }
foreach(var attachment in attachments) foreach(var attachment in attachments)

View File

@ -8,21 +8,16 @@ BEGIN
DECLARE @AttachmentIdKey VARCHAR(50) = CONCAT('"', @AttachmentId, '"') DECLARE @AttachmentIdKey VARCHAR(50) = CONCAT('"', @AttachmentId, '"')
DECLARE @AttachmentIdPath VARCHAR(50) = CONCAT('$.', @AttachmentIdKey) DECLARE @AttachmentIdPath VARCHAR(50) = CONCAT('$.', @AttachmentIdKey)
DECLARE @Attachments NVARCHAR(MAX)
DECLARE @UserId UNIQUEIDENTIFIER DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @OrganizationId UNIQUEIDENTIFIER DECLARE @OrganizationId UNIQUEIDENTIFIER
SELECT SELECT
@UserId = [UserId], @UserId = [UserId],
@OrganizationId = [OrganizationId], @OrganizationId = [OrganizationId]
@Attachments = [Attachments]
FROM FROM
[dbo].[Cipher] [dbo].[Cipher]
WHERE [Id] = @Id WHERE [Id] = @Id
DECLARE @AttachmentData NVARCHAR(MAX) = JSON_QUERY(@Attachments, @AttachmentIdPath)
DECLARE @Size BIGINT = (CAST(JSON_VALUE(@AttachmentData, '$.Size') AS BIGINT) * -1)
UPDATE UPDATE
[dbo].[Cipher] [dbo].[Cipher]
SET SET
@ -32,12 +27,12 @@ BEGIN
IF @OrganizationId IS NOT NULL IF @OrganizationId IS NOT NULL
BEGIN BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId, @Size EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
END END
ELSE IF @UserId IS NOT NULL ELSE IF @UserId IS NOT NULL
BEGIN BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId, @Size EXEC [dbo].[User_UpdateStorage] @UserId
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END END
END END

View File

@ -10,7 +10,6 @@ BEGIN
DECLARE @AttachmentIdKey VARCHAR(50) = CONCAT('"', @AttachmentId, '"') DECLARE @AttachmentIdKey VARCHAR(50) = CONCAT('"', @AttachmentId, '"')
DECLARE @AttachmentIdPath VARCHAR(50) = CONCAT('$.', @AttachmentIdKey) DECLARE @AttachmentIdPath VARCHAR(50) = CONCAT('$.', @AttachmentIdKey)
DECLARE @Size BIGINT = CAST(JSON_VALUE(@AttachmentData, '$.Size') AS BIGINT)
UPDATE UPDATE
[dbo].[Cipher] [dbo].[Cipher]
@ -27,12 +26,12 @@ BEGIN
IF @OrganizationId IS NOT NULL IF @OrganizationId IS NOT NULL
BEGIN BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId, @Size EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
END END
ELSE IF @UserId IS NOT NULL ELSE IF @UserId IS NOT NULL
BEGIN BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId, @Size EXEC [dbo].[User_UpdateStorage] @UserId
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END END
END END

View File

@ -14,26 +14,6 @@ AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
DECLARE @CipherAttachments NVARCHAR(MAX)
SELECT
@CipherAttachments = [Attachments]
FROM
[dbo].[Cipher]
WHERE [Id] = @Id
DECLARE @Size BIGINT
DECLARE @SizeDec BIGINT
IF @CipherAttachments IS NOT NULL
BEGIN
SELECT
@Size = SUM(CAST(JSON_VALUE(value,'$.Size') AS BIGINT))
FROM
OPENJSON(@CipherAttachments)
SET @SizeDec = @Size * -1
END
UPDATE UPDATE
[dbo].[Cipher] [dbo].[Cipher]
SET SET
@ -87,10 +67,10 @@ BEGIN
WHERE WHERE
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) [Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
IF ISNULL(@Size, 0) > 0 IF @Attachments IS NOT NULL
BEGIN BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId, @Size EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
EXEC [dbo].[User_UpdateStorage] @UserId, @SizeDec EXEC [dbo].[User_UpdateStorage] @UserId
END END
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId

View File

@ -1,15 +1,37 @@
CREATE PROCEDURE [dbo].[Organization_UpdateStorage] CREATE PROCEDURE [dbo].[Organization_UpdateStorage]
@Id UNIQUEIDENTIFIER, @Id UNIQUEIDENTIFIER
@StorageIncrease BIGINT
AS AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
DECLARE @Storage BIGINT
;WITH [CTE] AS (
SELECT
[Id],
(
SELECT
SUM(CAST(JSON_VALUE(value,'$.Size') AS BIGINT))
FROM
OPENJSON([Attachments])
) [Size]
FROM
[dbo].[Cipher]
)
SELECT
@Storage = SUM([CTE].[Size])
FROM
[dbo].[Cipher] C
LEFT JOIN
[CTE] ON C.[Id] = [CTE].[Id]
WHERE
C.[OrganizationId] = @Id
AND C.[Attachments] IS NOT NULL
UPDATE UPDATE
[dbo].[Organization] [dbo].[Organization]
SET SET
[Storage] = ISNULL([Storage], 0) + @StorageIncrease, [Storage] = @Storage,
[RevisionDate] = GETUTCDATE() [RevisionDate] = GETUTCDATE()
WHERE WHERE
[Id] = @Id [Id] = @Id

View File

@ -1,15 +1,37 @@
CREATE PROCEDURE [dbo].[User_UpdateStorage] CREATE PROCEDURE [dbo].[User_UpdateStorage]
@Id UNIQUEIDENTIFIER, @Id UNIQUEIDENTIFIER
@StorageIncrease BIGINT
AS AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
DECLARE @Storage BIGINT
;WITH [CTE] AS (
SELECT
[Id],
(
SELECT
SUM(CAST(JSON_VALUE(value,'$.Size') AS BIGINT))
FROM
OPENJSON([Attachments])
) [Size]
FROM
[dbo].[Cipher]
)
SELECT
@Storage = SUM([CTE].[Size])
FROM
[dbo].[Cipher] C
LEFT JOIN
[CTE] ON C.[Id] = [CTE].[Id]
WHERE
C.[UserId] = @Id
AND C.[Attachments] IS NOT NULL
UPDATE UPDATE
[dbo].[User] [dbo].[User]
SET SET
[Storage] = ISNULL([Storage], 0) + @StorageIncrease, [Storage] = @Storage,
[RevisionDate] = GETUTCDATE() [RevisionDate] = GETUTCDATE()
WHERE WHERE
[Id] = @Id [Id] = @Id