1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 15:42:48 -05:00

[Soft Delete] - API updates for soft delete + retrieval

This commit is contained in:
Chad Scharf
2020-04-01 13:00:25 -04:00
parent fef512bad1
commit d014a597dd
28 changed files with 1279 additions and 122 deletions

View File

@ -26,6 +26,8 @@
Cipher_ClientCopiedHiddenField = 1112,
Cipher_ClientCopiedCardCode = 1113,
Cipher_ClientAutofilled = 1114,
Cipher_SoftDeleted = 1115,
Cipher_Restored = 1116,
Collection_Created = 1300,
Collection_Updated = 1301,

View File

@ -205,6 +205,12 @@ namespace Bit.Core.Models.Api
public IEnumerable<string> Ids { get; set; }
}
public class CipherBulkRestoreRequestModel
{
[Required]
public IEnumerable<string> Ids { get; set; }
}
public class CipherBulkMoveRequestModel
{
[Required]

View File

@ -60,6 +60,7 @@ namespace Bit.Core.Models.Api
OrganizationId = cipher.OrganizationId?.ToString();
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
OrganizationUseTotp = orgUseTotp;
DeletedDate = cipher.DeletedDate;
}
public string Id { get; set; }
@ -77,6 +78,7 @@ namespace Bit.Core.Models.Api
public IEnumerable<AttachmentResponseModel> Attachments { get; set; }
public bool OrganizationUseTotp { get; set; }
public DateTime RevisionDate { get; set; }
public DateTime? DeletedDate { get; set; }
}
public class CipherResponseModel : CipherMiniResponseModel

View File

@ -20,6 +20,7 @@ namespace Bit.Core.Models.Table
public string Attachments { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public DateTime? DeletedDate { get; internal set; }
public void SetNewId()
{

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, bool deleted = false);
Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id);
Task<bool> GetCanEditByIdAsync(Guid userId, Guid cipherId);
Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true, bool deleted = false);
Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId, bool deleted = false);
Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true);
Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId);
Task CreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds);
Task CreateAsync(CipherDetails cipher);
Task CreateAsync(CipherDetails cipher, IEnumerable<Guid> collectionIds);
@ -23,8 +23,7 @@ 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(Cipher obj, bool permanent = true);
Task DeleteAsync(IEnumerable<Guid> ids, Guid userId, bool permanent = true);
Task DeleteAsync(IEnumerable<Guid> ids, Guid userId);
Task MoveAsync(IEnumerable<Guid> ids, Guid? folderId, Guid userId);
Task DeleteByUserIdAsync(Guid userId);
Task DeleteByOrganizationIdAsync(Guid organizationId);
@ -33,5 +32,9 @@ 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

@ -36,13 +36,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id, bool deleted = false)
public async Task<CipherOrganizationDetails> GetOrganizationDetailsByIdAsync(Guid id)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherOrganizationDetails_ReadById]",
new { Id = id, Deleted = deleted },
new { Id = id },
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, bool deleted = false)
public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true)
{
string sprocName = null;
if (withOrganizations)
@ -78,7 +78,7 @@ namespace Bit.Core.Repositories.SqlServer
{
var results = await connection.QueryAsync<CipherDetails>(
sprocName,
new { UserId = userId, Deleted = deleted },
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results
@ -88,13 +88,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId, bool deleted = false)
public async Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Cipher>(
$"[{Schema}].[Cipher_ReadByOrganizationId]",
new { OrganizationId = organizationId, Deleted = deleted },
new { OrganizationId = organizationId },
commandType: CommandType.StoredProcedure);
return results.ToList();
@ -156,7 +156,7 @@ namespace Bit.Core.Repositories.SqlServer
public async Task UpsertAsync(CipherDetails cipher)
{
if (cipher.Id.Equals(default(Guid)))
if (cipher.Id.Equals(default))
{
await CreateAsync(cipher);
}
@ -215,24 +215,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task DeleteAsync(Cipher obj, bool permanent = true)
{
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 = true)
public async Task DeleteAsync(IEnumerable<Guid> ids, Guid userId)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_Delete]",
new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId, Permanent = permanent },
new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId },
commandType: CommandType.StoredProcedure);
}
}
@ -448,7 +437,8 @@ namespace Bit.Core.Repositories.SqlServer
[Type] = TC.[Type],
[Data] = TC.[Data],
[Attachments] = TC.[Attachments],
[RevisionDate] = TC.[RevisionDate]
[RevisionDate] = TC.[RevisionDate],
[DeletedDate] = TC.[DeletedDate]
FROM
[dbo].[Cipher] C
INNER JOIN
@ -587,6 +577,50 @@ 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))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_SoftDelete]",
new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId },
commandType: CommandType.StoredProcedure);
}
}
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))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Cipher_Restore]",
new { Ids = ids.ToGuidIdArrayTVP(), UserId = userId },
commandType: CommandType.StoredProcedure);
}
}
private DataTable BuildCiphersTable(SqlBulkCopy bulkCopy, IEnumerable<Cipher> ciphers)
{
var c = ciphers.FirstOrDefault();
@ -617,6 +651,8 @@ namespace Bit.Core.Repositories.SqlServer
ciphersTable.Columns.Add(creationDateColumn);
var revisionDateColumn = new DataColumn(nameof(c.RevisionDate), c.RevisionDate.GetType());
ciphersTable.Columns.Add(revisionDateColumn);
var deletedDateColumn = new DataColumn(nameof(c.DeletedDate), c.DeletedDate.GetType());
ciphersTable.Columns.Add(deletedDateColumn);
foreach (DataColumn col in ciphersTable.Columns)
{
@ -641,6 +677,7 @@ namespace Bit.Core.Repositories.SqlServer
row[attachmentsColumn] = cipher.Attachments;
row[creationDateColumn] = cipher.CreationDate;
row[revisionDateColumn] = cipher.RevisionDate;
row[deletedDateColumn] = cipher.DeletedDate;
ciphersTable.Rows.Add(row);
}

View File

@ -33,5 +33,9 @@ namespace Bit.Core.Services
IEnumerable<KeyValuePair<int, int>> folderRelationships);
Task ImportCiphersAsync(List<Collection> collections, List<CipherDetails> ciphers,
IEnumerable<KeyValuePair<int, int>> collectionRelationships, Guid importingUserId);
Task SoftDeleteAsync(Cipher cipher, Guid deletingUserId, bool orgAdmin = false);
Task SoftDeleteManyAsync(IEnumerable<Guid> cipherIds, Guid deletingUserId);
Task RestoreAsync(Cipher cipher, Guid restoringUserId, bool orgAdmin = false);
Task RestoreManyAsync(IEnumerable<Guid> cipherIds, Guid restoringUserId);
}
}

View File

@ -282,7 +282,7 @@ namespace Bit.Core.Services
await _cipherRepository.DeleteAsync(cipher);
await _attachmentStorageService.DeleteAttachmentsForCipherAsync(cipher.Id);
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Deleted);
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_Deleted);
// push
await _pushService.PushSyncCipherDeleteAsync(cipher);
@ -664,6 +664,72 @@ namespace Bit.Core.Services
await _pushService.PushSyncVaultAsync(importingUserId);
}
public async Task SoftDeleteAsync(Cipher cipher, Guid deletingUserId, bool orgAdmin = false)
{
if (!orgAdmin && !(await UserCanEditAsync(cipher, deletingUserId)))
{
throw new BadRequestException("You do not have permissions to soft delete this.");
}
await _cipherRepository.SoftDeleteAsync(cipher);
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_SoftDeleted);
// push
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
}
public async Task SoftDeleteManyAsync(IEnumerable<Guid> cipherIds, Guid deletingUserId)
{
var cipherIdsSet = new HashSet<Guid>(cipherIds);
var ciphers = await _cipherRepository.GetManyByUserIdAsync(deletingUserId);
var deletingCiphers = ciphers.Where(c => cipherIdsSet.Contains(c.Id) && c.Edit);
await _cipherRepository.SoftDeleteAsync(cipherIds, deletingUserId);
var events = deletingCiphers.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_SoftDeleted, null));
foreach (var eventsBatch in events.Batch(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
// push
await _pushService.PushSyncCiphersAsync(deletingUserId);
}
public async Task RestoreAsync(Cipher cipher, Guid restoringUserId, bool orgAdmin = false)
{
if (!orgAdmin && !(await UserCanEditAsync(cipher, restoringUserId)))
{
throw new BadRequestException("You do not have permissions to delete this.");
}
await _cipherRepository.RestoreAsync(cipher);
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_Restored);
// push
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
}
public async Task RestoreManyAsync(IEnumerable<Guid> cipherIds, Guid restoringUserId)
{
var cipherIdsSet = new HashSet<Guid>(cipherIds);
var ciphers = await _cipherRepository.GetManyByUserIdAsync(restoringUserId);
var restoringCiphers = ciphers.Where(c => cipherIdsSet.Contains(c.Id) && c.Edit);
await _cipherRepository.RestoreAsync(cipherIds, restoringUserId);
var events = restoringCiphers.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Restored, null));
foreach (var eventsBatch in events.Batch(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
// push
await _pushService.PushSyncCiphersAsync(restoringUserId);
}
private async Task<bool> UserCanEditAsync(Cipher cipher, Guid userId)
{
if (!cipher.OrganizationId.HasValue && cipher.UserId.HasValue && cipher.UserId.Value == userId)