1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-03 00:52:49 -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

@ -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)