1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 07:36:14 -05:00

[Soft-Delete] Simplify the data-tier, removed extra sprocs and reuse update

This commit is contained in:
Chad Scharf
2020-04-01 16:39:27 -04:00
parent f6044f0d00
commit d07f27f274
14 changed files with 65 additions and 200 deletions

View File

@ -32,9 +32,7 @@ namespace Bit.Core.Repositories
Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders);
Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Collection> collections,
IEnumerable<CollectionCipher> collectionCiphers);
Task SoftDeleteAsync(Cipher obj);
Task SoftDeleteAsync(IEnumerable<Guid> ids, Guid userId);
Task RestoreAsync(Cipher obj);
Task RestoreAsync(IEnumerable<Guid> ids, Guid userId);
}
}

View File

@ -577,17 +577,6 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task SoftDeleteAsync(Cipher obj)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_SoftDeleteById]",
new { obj.Id },
commandType: CommandType.StoredProcedure);
}
}
public async Task SoftDeleteAsync(IEnumerable<Guid> ids, Guid userId)
{
using (var connection = new SqlConnection(ConnectionString))
@ -599,17 +588,6 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task RestoreAsync(Cipher obj)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_RestoreById]",
new { obj.Id },
commandType: CommandType.StoredProcedure);
}
}
public async Task RestoreAsync(IEnumerable<Guid> ids, Guid userId)
{
using (var connection = new SqlConnection(ConnectionString))

View File

@ -671,7 +671,16 @@ namespace Bit.Core.Services
throw new BadRequestException("You do not have permissions to soft delete this.");
}
await _cipherRepository.SoftDeleteAsync(cipher);
if (cipher.DeletedDate.HasValue)
{
// Already soft-deleted, we can safely ignore this
return;
}
cipher.DeletedDate = DateTime.UtcNow;
cipher.RevisionDate = DateTime.UtcNow;
await _cipherRepository.UpsertAsync(cipher);
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_SoftDeleted);
// push
@ -704,7 +713,16 @@ namespace Bit.Core.Services
throw new BadRequestException("You do not have permissions to delete this.");
}
await _cipherRepository.RestoreAsync(cipher);
if (!cipher.DeletedDate.HasValue)
{
// Already restored, we can safely ignore this
return;
}
cipher.DeletedDate = null;
cipher.RevisionDate = DateTime.UtcNow;
await _cipherRepository.UpsertAsync(cipher);
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_Restored);
// push

View File

@ -262,8 +262,6 @@
<Build Include="dbo\Stored Procedures\Policy_ReadByOrganizationIdType.sql" />
<Build Include="dbo\Stored Procedures\Policy_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Restore.sql" />
<Build Include="dbo\Stored Procedures\Cipher_RestoreById.sql" />
<Build Include="dbo\Stored Procedures\Cipher_SoftDelete.sql" />
<Build Include="dbo\Stored Procedures\Cipher_SoftDeleteById.sql" />
</ItemGroup>
</Project>

View File

@ -13,7 +13,7 @@
@Favorite BIT,
@Edit BIT, -- not used
@OrganizationUseTotp BIT, -- not used
@DeletedDate DATETIME2(7) -- not used
@DeletedDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
@ -31,7 +31,8 @@ BEGIN
[Favorites],
[Folders],
[CreationDate],
[RevisionDate]
[RevisionDate],
[DeletedDate]
)
VALUES
(
@ -43,7 +44,8 @@ BEGIN
CASE WHEN @Favorite = 1 THEN CONCAT('{', @UserIdKey, ':true}') ELSE NULL END,
CASE WHEN @FolderId IS NOT NULL THEN CONCAT('{', @UserIdKey, ':"', @FolderId, '"', '}') ELSE NULL END,
@CreationDate,
@RevisionDate
@RevisionDate,
@DeletedDate
)
IF @OrganizationId IS NOT NULL

View File

@ -13,7 +13,7 @@
@Favorite BIT,
@Edit BIT, -- not used
@OrganizationUseTotp BIT, -- not used
@DeletedDate DATETIME2(7), -- not used
@DeletedDate DATETIME2(7),
@CollectionIds AS [dbo].[GuidIdArray] READONLY
AS
BEGIN

View File

@ -13,7 +13,7 @@
@Favorite BIT,
@Edit BIT, -- not used
@OrganizationUseTotp BIT, -- not used
@DeletedDate DATETIME2(2) -- not used
@DeletedDate DATETIME2(2)
AS
BEGIN
SET NOCOUNT ON
@ -47,7 +47,8 @@ BEGIN
JSON_MODIFY([Favorites], @UserIdPath, NULL)
END,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[DeletedDate] = @DeletedDate
WHERE
[Id] = @Id

View File

@ -9,7 +9,7 @@
@Attachments NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7),
@DeletedDate DATETIME2(7) -- not used
@DeletedDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
@ -25,7 +25,8 @@ BEGIN
[Folders],
[Attachments],
[CreationDate],
[RevisionDate]
[RevisionDate],
[DeletedDate]
)
VALUES
(
@ -38,7 +39,8 @@ BEGIN
@Folders,
@Attachments,
@CreationDate,
@RevisionDate
@RevisionDate,
@DeletedDate
)
IF @OrganizationId IS NOT NULL

View File

@ -9,7 +9,7 @@
@Attachments NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7),
@DeletedDate DATETIME2(7), -- not used
@DeletedDate DATETIME2(7),
@CollectionIds AS [dbo].[GuidIdArray] READONLY
AS
BEGIN

View File

@ -1,34 +0,0 @@
CREATE PROCEDURE [dbo].[Cipher_RestoreById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @OrganizationId UNIQUEIDENTIFIER
SELECT TOP 1
@UserId = [UserId],
@OrganizationId = [OrganizationId]
FROM
[dbo].[Cipher]
WHERE
[Id] = @Id
UPDATE
[dbo].[Cipher]
SET
[DeletedDate] = NULL,
[RevisionDate] = GETUTCDATE()
WHERE
[Id] = @Id
IF @OrganizationId IS NOT NULL
BEGIN
EXEC [dbo].[User_BumpAccountRevisionDateByCipherId] @Id, @OrganizationId
END
ELSE IF @UserId IS NOT NULL
BEGIN
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END
END

View File

@ -1,35 +0,0 @@
CREATE PROCEDURE [dbo].[Cipher_SoftDeleteById]
@Id UNIQUEIDENTIFIER
WITH RECOMPILE
AS
BEGIN
SET NOCOUNT ON
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @OrganizationId UNIQUEIDENTIFIER
SELECT TOP 1
@UserId = [UserId],
@OrganizationId = [OrganizationId]
FROM
[dbo].[Cipher]
WHERE
[Id] = @Id
UPDATE
[dbo].[Cipher]
SET
[DeletedDate] = SYSUTCDATETIME(),
[RevisionDate] = GETUTCDATE()
WHERE
[Id] = @Id
IF @OrganizationId IS NOT NULL
BEGIN
EXEC [dbo].[User_BumpAccountRevisionDateByCipherId] @Id, @OrganizationId
END
ELSE IF @UserId IS NOT NULL
BEGIN
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END
END

View File

@ -9,7 +9,7 @@
@Attachments NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7),
@DeletedDate DATETIME2(7) -- not used
@DeletedDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
@ -25,7 +25,8 @@ BEGIN
[Folders] = @Folders,
[Attachments] = @Attachments,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[DeletedDate] = @DeletedDate
WHERE
[Id] = @Id

View File

@ -9,7 +9,7 @@
@Attachments NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7),
@DeletedDate DATETIME2(7), -- not used
@DeletedDate DATETIME2(7),
@CollectionIds AS [dbo].[GuidIdArray] READONLY
AS
BEGIN
@ -34,9 +34,9 @@ BEGIN
[OrganizationId] = @OrganizationId,
[Data] = @Data,
[Attachments] = @Attachments,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[DeletedDate] = @DeletedDate
-- No need to update CreationDate, Favorites, Folders, or Type since that data will not change
-- Do not update DeletedDate because that is a separate atomic action
WHERE
[Id] = @Id