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

Deleted date on Cipher table, related sprocs and repositories updated

This commit is contained in:
Chad Scharf
2020-03-26 19:32:37 -04:00
parent a74df16848
commit bc46eccf70
13 changed files with 601 additions and 44 deletions

View File

@ -10,10 +10,10 @@ namespace Bit.Core.Repositories
public interface ICipherRepository : IRepository<Cipher, Guid>
{
Task<CipherDetails> GetByIdAsync(Guid id, Guid userId);
Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id);
Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id, bool deleted = false);
Task<bool> GetCanEditByIdAsync(Guid userId, Guid cipherId);
Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true);
Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId);
Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true, bool deleted = false);
Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId, bool deleted = false);
Task CreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds);
Task CreateAsync(CipherDetails cipher);
Task CreateAsync(CipherDetails cipher, IEnumerable<Guid> collectionIds);
@ -23,7 +23,8 @@ namespace Bit.Core.Repositories
Task UpdatePartialAsync(Guid id, Guid userId, Guid? folderId, bool favorite);
Task UpdateAttachmentAsync(CipherAttachment attachment);
Task DeleteAttachmentAsync(Guid cipherId, string attachmentId);
Task DeleteAsync(IEnumerable<Guid> ids, Guid userId);
Task DeleteAsync(Cipher obj, bool permanent = false);
Task DeleteAsync(IEnumerable<Guid> ids, Guid userId, bool permanent = false);
Task MoveAsync(IEnumerable<Guid> ids, Guid? folderId, Guid userId);
Task DeleteByUserIdAsync(Guid userId);
Task DeleteByOrganizationIdAsync(Guid organizationId);

View File

@ -36,13 +36,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id)
public async Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id, bool deleted = false)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherOrganizationDetails_ReadById]",
new { Id = id },
new { Id = id, Deleted = deleted },
commandType: CommandType.StoredProcedure);
return results.FirstOrDefault();
@ -62,7 +62,7 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true)
public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true, bool deleted = false)
{
string sprocName = null;
if(withOrganizations)
@ -78,7 +78,7 @@ namespace Bit.Core.Repositories.SqlServer
{
var results = await connection.QueryAsync<CipherDetails>(
sprocName,
new { UserId = userId },
new { UserId = userId, Deleted = deleted },
commandType: CommandType.StoredProcedure);
return results
@ -88,13 +88,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId)
public async Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId, bool deleted = false)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Cipher>(
$"[{Schema}].[Cipher_ReadByOrganizationId]",
new { OrganizationId = organizationId },
new { OrganizationId = organizationId, Deleted = deleted },
commandType: CommandType.StoredProcedure);
return results.ToList();
@ -215,13 +215,24 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task DeleteAsync(IEnumerable<Guid> ids, Guid userId)
public async Task DeleteAsync(Cipher obj, bool permanent = false)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_DeleteById]",
new { obj.Id, Permanent = permanent },
commandType: CommandType.StoredProcedure);
}
}
public async Task DeleteAsync(IEnumerable<Guid> ids, Guid userId, bool permanent = false)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_Delete]",
new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId },
new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId, Permanent = permanent },
commandType: CommandType.StoredProcedure);
}
}

View File

@ -24,6 +24,7 @@ SELECT
OR C.[Folders] IS NULL
THEN NULL
ELSE TRY_CONVERT(UNIQUEIDENTIFIER, JSON_VALUE(C.[Folders], CONCAT('$."', @UserId, '"')))
END [FolderId]
END [FolderId],
C.[DeletedDate]
FROM
[dbo].[Cipher] C

View File

@ -1,5 +1,6 @@
CREATE PROCEDURE [dbo].[CipherDetails_ReadByUserId]
@UserId UNIQUEIDENTIFIER
@UserId UNIQUEIDENTIFIER,
@Deleted BIT = 0
AS
BEGIN
SET NOCOUNT ON
@ -8,4 +9,7 @@ BEGIN
*
FROM
[dbo].[UserCipherDetails](@UserId)
WHERE
(@Deleted = 1 AND [DeletedDate] IS NOT NULL)
OR (@Deleted = 0 AND [DeletedDate] IS NULL)
END

View File

@ -1,5 +1,6 @@
CREATE PROCEDURE [dbo].[CipherDetails_ReadWithoutOrganizationsByUserId]
@UserId UNIQUEIDENTIFIER
@UserId UNIQUEIDENTIFIER,
@Deleted BIT = 0
AS
BEGIN
SET NOCOUNT ON
@ -12,4 +13,9 @@ BEGIN
[dbo].[CipherDetails](@UserId)
WHERE
[UserId] = @UserId
AND
(
(@Deleted = 1 AND [DeletedDate] IS NOT NULL)
OR (@Deleted = 0 AND [DeletedDate] IS NULL)
)
END

View File

@ -1,5 +1,6 @@
CREATE PROCEDURE [dbo].[CipherOrganizationDetails_ReadById]
@Id UNIQUEIDENTIFIER
@Id UNIQUEIDENTIFIER,
@Deleted BIT = 0
AS
BEGIN
SET NOCOUNT ON
@ -16,4 +17,9 @@ BEGIN
[dbo].[Organization] O ON O.[Id] = C.[OrganizationId]
WHERE
C.[Id] = @Id
AND
(
(@Deleted = 1 AND [DeletedDate] IS NOT NULL)
OR (@Deleted = 0 AND [DeletedDate] IS NULL)
)
END

View File

@ -1,6 +1,7 @@
CREATE PROCEDURE [dbo].[Cipher_Delete]
@Ids AS [dbo].[GuidIdArray] READONLY,
@UserId AS UNIQUEIDENTIFIER
@UserId AS UNIQUEIDENTIFIER,
@Permanent AS BIT = 0
AS
BEGIN
SET NOCOUNT ON
@ -26,11 +27,23 @@ BEGIN
AND [Id] IN (SELECT * FROM @Ids)
-- Delete ciphers
DELETE
FROM
[dbo].[Cipher]
WHERE
[Id] IN (SELECT [Id] FROM #Temp)
IF @Permanent = 1
BEGIN
DELETE
FROM
[dbo].[Cipher]
WHERE
[Id] IN (SELECT [Id] FROM #Temp)
END
ELSE
BEGIN
UPDATE
[dbo].[Cipher]
SET
[DeletedDate] = SYSUTCDATETIME()
WHERE
[Id] IN (SELECT [Id] FROM #Temp)
END
-- Cleanup orgs
DECLARE @OrgId UNIQUEIDENTIFIER
@ -46,7 +59,11 @@ BEGIN
OPEN [OrgCursor]
FETCH NEXT FROM [OrgCursor] INTO @OrgId
WHILE @@FETCH_STATUS = 0 BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrgId
-- Storage cleanup for groups only matters if we're permanently deleting
IF @Permanent = 1
BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrgId
END
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrgId
FETCH NEXT FROM [OrgCursor] INTO @OrgId
END
@ -54,18 +71,22 @@ BEGIN
DEALLOCATE [OrgCursor]
-- Cleanup user
DECLARE @UserCiphersWithStorageCount INT
SELECT
@UserCiphersWithStorageCount = COUNT(1)
FROM
#Temp
WHERE
[UserId] IS NOT NULL
AND [Attachments] = 1
IF @UserCiphersWithStorageCount > 0
IF @Permanent = 1
BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId
-- Storage cleanup for users only matters if we're permanently deleting
DECLARE @UserCiphersWithStorageCount INT
SELECT
@UserCiphersWithStorageCount = COUNT(1)
FROM
#Temp
WHERE
[UserId] IS NOT NULL
AND [Attachments] = 1
IF @UserCiphersWithStorageCount > 0
BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId
END
END
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId

View File

@ -1,5 +1,6 @@
CREATE PROCEDURE [dbo].[Cipher_DeleteById]
@Id UNIQUEIDENTIFIER
@Id UNIQUEIDENTIFIER,
@Permanent AS BIT = 0
WITH RECOMPILE
AS
BEGIN
@ -17,16 +18,28 @@ BEGIN
[dbo].[Cipher]
WHERE
[Id] = @Id
DELETE
FROM
[dbo].[Cipher]
WHERE
[Id] = @Id
IF @Permanent = 1
BEGIN
DELETE
FROM
[dbo].[Cipher]
WHERE
[Id] = @Id
END
ELSE
BEGIN
UPDATE
[dbo].[Cipher]
SET
[DeletedDate] = SYSUTCDATETIME()
WHERE
[Id] = @Id
END
IF @OrganizationId IS NOT NULL
BEGIN
IF @Attachments = 1
IF @Attachments = 1 AND @Permanent = 1
BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
END
@ -34,7 +47,7 @@ BEGIN
END
ELSE IF @UserId IS NOT NULL
BEGIN
IF @Attachments = 1
IF @Attachments = 1 AND @Permanent = 1
BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId
END

View File

@ -1,5 +1,6 @@
CREATE PROCEDURE [dbo].[Cipher_ReadByOrganizationId]
@OrganizationId UNIQUEIDENTIFIER
@OrganizationId UNIQUEIDENTIFIER,
@Deleted BIT = 0
AS
BEGIN
SET NOCOUNT ON
@ -11,4 +12,9 @@ BEGIN
WHERE
[UserId] IS NULL
AND [OrganizationId] = @OrganizationId
AND
(
(@Deleted = 1 AND [DeletedDate] IS NOT NULL)
OR (@Deleted = 0 AND [DeletedDate] IS NULL)
)
END

View File

@ -9,6 +9,7 @@
[Attachments] NVARCHAR (MAX) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
[DeletedDate] DATETIME2 (7) NULL,
CONSTRAINT [PK_Cipher] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Cipher_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
CONSTRAINT [FK_Cipher_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])