1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-29 23:34:53 -05:00

support for sharing new attachments

This commit is contained in:
Kyle Spearrin 2018-11-15 12:52:31 -05:00
parent 7cda459127
commit 331ee3266a

View File

@ -373,7 +373,8 @@ namespace Bit.Core.Services
IEnumerable<Guid> collectionIds, Guid sharingUserId) IEnumerable<Guid> collectionIds, Guid sharingUserId)
{ {
var attachments = cipher.GetAttachments(); var attachments = cipher.GetAttachments();
var hasAttachments = (attachments?.Count ?? 0) > 0; var hasAttachments = attachments?.Any() ?? false;
var hasOldAttachments = attachments?.Any(a => a.Key == null) ?? false;
var updatedCipher = false; var updatedCipher = false;
var migratedAttachments = false; var migratedAttachments = false;
@ -418,16 +419,22 @@ namespace Bit.Core.Services
updatedCipher = true; updatedCipher = true;
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Shared); await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Shared);
if(hasAttachments) if(hasOldAttachments)
{ {
// migrate attachments // migrate old attachments
foreach(var attachment in attachments) foreach(var attachment in attachments.Where(a => a.Key == null))
{ {
await _attachmentStorageService.StartShareAttachmentAsync(cipher.Id, organizationId, await _attachmentStorageService.StartShareAttachmentAsync(cipher.Id, organizationId,
attachment.Key); attachment.Key);
migratedAttachments = true; migratedAttachments = true;
} }
// commit attachment migration
await _attachmentStorageService.CleanupAsync(cipher.Id);
} }
// push
await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
} }
catch catch
{ {
@ -437,7 +444,7 @@ namespace Bit.Core.Services
await _cipherRepository.ReplaceAsync(originalCipher); await _cipherRepository.ReplaceAsync(originalCipher);
} }
if(!hasAttachments || !migratedAttachments) if(!hasOldAttachments || !migratedAttachments)
{ {
throw; throw;
} }
@ -448,7 +455,7 @@ namespace Bit.Core.Services
await _organizationRepository.UpdateStorageAsync(organizationId); await _organizationRepository.UpdateStorageAsync(organizationId);
} }
foreach(var attachment in attachments) foreach(var attachment in attachments.Where(a => a.Key == null))
{ {
await _attachmentStorageService.RollbackShareAttachmentAsync(cipher.Id, organizationId, await _attachmentStorageService.RollbackShareAttachmentAsync(cipher.Id, organizationId,
attachment.Key); attachment.Key);
@ -457,12 +464,6 @@ namespace Bit.Core.Services
await _attachmentStorageService.CleanupAsync(cipher.Id); await _attachmentStorageService.CleanupAsync(cipher.Id);
throw; throw;
} }
// commit attachment migration
await _attachmentStorageService.CleanupAsync(cipher.Id);
// push
await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
} }
public async Task ShareManyAsync(IEnumerable<Cipher> ciphers, Guid organizationId, public async Task ShareManyAsync(IEnumerable<Cipher> ciphers, Guid organizationId,
@ -486,11 +487,6 @@ namespace Bit.Core.Services
throw new BadRequestException("One or more ciphers do not belong to you."); throw new BadRequestException("One or more ciphers do not belong to you.");
} }
if(!string.IsNullOrWhiteSpace(cipher.Attachments))
{
throw new BadRequestException("One or more ciphers have attachments.");
}
cipher.UserId = null; cipher.UserId = null;
cipher.OrganizationId = organizationId; cipher.OrganizationId = organizationId;
cipher.RevisionDate = DateTime.UtcNow; cipher.RevisionDate = DateTime.UtcNow;