2015-12-26 23:09:53 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2017-06-30 11:15:58 -04:00
|
|
|
|
using System.IO;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
using System.Linq;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using System.Text.Json;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2019-07-25 15:39:25 -04:00
|
|
|
|
using Bit.Core.Enums;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
using Bit.Core.Exceptions;
|
2021-08-10 14:38:58 -04:00
|
|
|
|
using Bit.Core.Models.Business;
|
2017-06-30 11:15:58 -04:00
|
|
|
|
using Bit.Core.Models.Data;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2019-07-25 15:50:13 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2017-03-18 11:58:02 -04:00
|
|
|
|
using Core.Models.Data;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class CipherService : ICipherService
|
|
|
|
|
|
{
|
2021-08-04 09:00:30 +10:00
|
|
|
|
public const long MAX_FILE_SIZE = Constants.FileSize501mb;
|
2021-03-30 18:41:14 -05:00
|
|
|
|
public const string MAX_FILE_SIZE_READABLE = "500 MB";
|
2015-12-26 23:09:53 -05:00
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
2017-03-18 11:58:02 -04:00
|
|
|
|
private readonly IFolderRepository _folderRepository;
|
2018-07-17 13:34:12 -04:00
|
|
|
|
private readonly ICollectionRepository _collectionRepository;
|
2017-02-25 23:38:24 -05:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
2017-04-27 09:19:30 -04:00
|
|
|
|
private readonly ICollectionCipherRepository _collectionCipherRepository;
|
2017-05-26 09:44:54 -04:00
|
|
|
|
private readonly IPushNotificationService _pushService;
|
2017-06-30 11:15:58 -04:00
|
|
|
|
private readonly IAttachmentStorageService _attachmentStorageService;
|
2017-12-01 14:06:16 -05:00
|
|
|
|
private readonly IEventService _eventService;
|
2018-08-28 17:40:08 -04:00
|
|
|
|
private readonly IUserService _userService;
|
2020-12-11 10:45:26 -06:00
|
|
|
|
private readonly IPolicyRepository _policyRepository;
|
2018-08-29 09:45:57 -04:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2021-03-30 18:41:14 -05:00
|
|
|
|
private const long _fileSizeLeeway = 1024L * 1024L; // 1MB
|
2021-08-10 14:38:58 -04:00
|
|
|
|
private readonly IReferenceEventService _referenceEventService;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
public CipherService(
|
2016-06-29 01:15:37 -04:00
|
|
|
|
ICipherRepository cipherRepository,
|
2017-03-18 11:58:02 -04:00
|
|
|
|
IFolderRepository folderRepository,
|
2018-07-17 13:34:12 -04:00
|
|
|
|
ICollectionRepository collectionRepository,
|
2017-02-25 23:38:24 -05:00
|
|
|
|
IUserRepository userRepository,
|
2017-03-21 00:04:39 -04:00
|
|
|
|
IOrganizationRepository organizationRepository,
|
2017-04-27 09:19:30 -04:00
|
|
|
|
ICollectionCipherRepository collectionCipherRepository,
|
2017-06-30 11:15:58 -04:00
|
|
|
|
IPushNotificationService pushService,
|
2017-12-01 14:06:16 -05:00
|
|
|
|
IAttachmentStorageService attachmentStorageService,
|
2018-08-28 17:40:08 -04:00
|
|
|
|
IEventService eventService,
|
2018-08-29 09:45:57 -04:00
|
|
|
|
IUserService userService,
|
2020-12-11 10:45:26 -06:00
|
|
|
|
IPolicyRepository policyRepository,
|
2021-08-10 14:38:58 -04:00
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
IReferenceEventService referenceEventService)
|
2015-12-26 23:09:53 -05:00
|
|
|
|
{
|
|
|
|
|
|
_cipherRepository = cipherRepository;
|
2017-03-18 11:58:02 -04:00
|
|
|
|
_folderRepository = folderRepository;
|
2018-07-17 13:34:12 -04:00
|
|
|
|
_collectionRepository = collectionRepository;
|
2017-02-25 23:38:24 -05:00
|
|
|
|
_userRepository = userRepository;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
_organizationRepository = organizationRepository;
|
2017-04-27 09:19:30 -04:00
|
|
|
|
_collectionCipherRepository = collectionCipherRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
_pushService = pushService;
|
2017-06-30 11:15:58 -04:00
|
|
|
|
_attachmentStorageService = attachmentStorageService;
|
2017-12-01 14:06:16 -05:00
|
|
|
|
_eventService = eventService;
|
2018-08-28 17:40:08 -04:00
|
|
|
|
_userService = userService;
|
2020-12-11 10:45:26 -06:00
|
|
|
|
_policyRepository = policyRepository;
|
2018-08-29 09:45:57 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
2021-08-10 14:38:58 -04:00
|
|
|
|
_referenceEventService = referenceEventService;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
public async Task SaveAsync(Cipher cipher, Guid savingUserId, DateTime? lastKnownRevisionDate,
|
|
|
|
|
|
IEnumerable<Guid> collectionIds = null, bool skipPermissionCheck = false, bool limitCollectionScope = true)
|
2017-04-27 14:50:22 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!skipPermissionCheck && !(await UserCanEditAsync(cipher, savingUserId)))
|
2017-04-27 14:50:22 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("You do not have permissions to edit this.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.Id == default(Guid))
|
2017-04-27 14:50:22 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue && collectionIds != null)
|
2018-10-19 12:07:31 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (limitCollectionScope)
|
2018-10-22 14:09:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Set user ID to limit scope of collection ids in the create sproc
|
|
|
|
|
|
cipher.UserId = savingUserId;
|
|
|
|
|
|
}
|
2018-10-19 12:07:31 -04:00
|
|
|
|
await _cipherRepository.CreateAsync(cipher, collectionIds);
|
2021-08-10 14:38:58 -04:00
|
|
|
|
|
|
|
|
|
|
await _referenceEventService.RaiseEventAsync(
|
|
|
|
|
|
new ReferenceEvent(ReferenceEventType.CipherCreated, await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value)));
|
2018-10-19 12:07:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.CreateAsync(cipher);
|
|
|
|
|
|
}
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Created);
|
2017-04-27 14:50:22 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await _pushService.PushSyncCipherCreateAsync(cipher, null);
|
2017-04-27 14:50:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (collectionIds != null)
|
2018-10-19 12:07:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Cannot create cipher with collection ids at the same time.");
|
|
|
|
|
|
}
|
2020-11-23 08:48:05 -06:00
|
|
|
|
ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
|
2017-04-27 14:50:22 -04:00
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
await _cipherRepository.ReplaceAsync(cipher);
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);
|
2017-04-27 14:50:22 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
2017-04-27 14:50:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
public async Task SaveDetailsAsync(CipherDetails cipher, Guid savingUserId, DateTime? lastKnownRevisionDate,
|
2018-10-19 12:07:31 -04:00
|
|
|
|
IEnumerable<Guid> collectionIds = null, bool skipPermissionCheck = false)
|
2016-06-29 01:15:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!skipPermissionCheck && !(await UserCanEditAsync(cipher, savingUserId)))
|
2017-03-24 09:27:15 -04:00
|
|
|
|
{
|
2017-04-20 16:19:23 -04:00
|
|
|
|
throw new BadRequestException("You do not have permissions to edit this.");
|
2017-03-24 09:27:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cipher.UserId = savingUserId;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.Id == default(Guid))
|
2016-06-29 01:15:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue && collectionIds != null)
|
2018-10-19 12:07:31 -04:00
|
|
|
|
{
|
2021-04-05 15:20:13 -05:00
|
|
|
|
var existingCollectionIds = (await _collectionRepository.GetManyByOrganizationIdAsync(cipher.OrganizationId.Value)).Select(c => c.Id);
|
|
|
|
|
|
if (collectionIds.Except(existingCollectionIds).Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Specified CollectionId does not exist on the specified Organization.");
|
|
|
|
|
|
}
|
2018-10-19 12:07:31 -04:00
|
|
|
|
await _cipherRepository.CreateAsync(cipher, collectionIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-12-11 10:45:26 -06:00
|
|
|
|
// Make sure the user can save new ciphers to their personal vault
|
2021-09-28 06:54:28 +10:00
|
|
|
|
var personalOwnershipPolicyCount = await _policyRepository.GetCountByTypeApplicableToUserIdAsync(savingUserId,
|
|
|
|
|
|
PolicyType.PersonalOwnership);
|
|
|
|
|
|
if (personalOwnershipPolicyCount > 0)
|
2020-12-11 10:45:26 -06:00
|
|
|
|
{
|
2021-09-28 06:54:28 +10:00
|
|
|
|
throw new BadRequestException("Due to an Enterprise Policy, you are restricted from saving items to your personal vault.");
|
2020-12-11 10:45:26 -06:00
|
|
|
|
}
|
2018-10-19 12:07:31 -04:00
|
|
|
|
await _cipherRepository.CreateAsync(cipher);
|
|
|
|
|
|
}
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Created);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
2017-07-07 14:08:30 -04:00
|
|
|
|
{
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value);
|
|
|
|
|
|
cipher.OrganizationUseTotp = org.UseTotp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-29 01:15:37 -04:00
|
|
|
|
// push
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await _pushService.PushSyncCipherCreateAsync(cipher, null);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (collectionIds != null)
|
2018-10-19 12:07:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Cannot create cipher with collection ids at the same time.");
|
|
|
|
|
|
}
|
2020-11-23 08:48:05 -06:00
|
|
|
|
ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
|
2016-06-30 21:31:12 -04:00
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
await _cipherRepository.ReplaceAsync(cipher);
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
public async Task UploadFileForExistingAttachmentAsync(Stream stream, Cipher cipher, CipherAttachment.MetaData attachment)
|
2017-06-30 11:15:58 -04:00
|
|
|
|
{
|
2021-03-30 18:41:14 -05:00
|
|
|
|
if (attachment == null)
|
2017-06-30 11:15:58 -04:00
|
|
|
|
{
|
2021-03-30 18:41:14 -05:00
|
|
|
|
throw new BadRequestException("Cipher attachment does not exist");
|
2017-06-30 11:15:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
await _attachmentStorageService.UploadNewAttachmentAsync(stream, cipher, attachment);
|
|
|
|
|
|
|
|
|
|
|
|
if (!await ValidateCipherAttachmentFile(cipher, attachment))
|
2017-06-30 11:15:58 -04:00
|
|
|
|
{
|
2021-03-30 18:41:14 -05:00
|
|
|
|
throw new BadRequestException("File received does not match expected file length.");
|
2017-06-30 11:15:58 -04:00
|
|
|
|
}
|
2021-03-30 18:41:14 -05:00
|
|
|
|
}
|
2017-06-30 11:15:58 -04:00
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
public async Task<(string attachmentId, string uploadUrl)> CreateAttachmentForDelayedUploadAsync(Cipher cipher,
|
2021-12-14 15:05:07 +00:00
|
|
|
|
string key, string fileName, long fileSize, bool adminRequest, Guid savingUserId)
|
2021-03-30 18:41:14 -05:00
|
|
|
|
{
|
2021-12-14 15:05:07 +00:00
|
|
|
|
await ValidateCipherEditForAttachmentAsync(cipher, savingUserId, adminRequest, fileSize);
|
2017-07-05 16:06:53 -04:00
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
|
|
|
|
|
|
var data = new CipherAttachment.MetaData
|
2017-06-30 14:41:57 -04:00
|
|
|
|
{
|
2021-03-30 18:41:14 -05:00
|
|
|
|
AttachmentId = attachmentId,
|
2021-12-14 15:05:07 +00:00
|
|
|
|
FileName = fileName,
|
|
|
|
|
|
Key = key,
|
|
|
|
|
|
Size = fileSize,
|
2021-03-30 18:41:14 -05:00
|
|
|
|
Validated = false,
|
2021-04-05 15:20:13 -05:00
|
|
|
|
};
|
2017-07-05 16:06:53 -04:00
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
var uploadUrl = await _attachmentStorageService.GetAttachmentUploadUrlAsync(cipher, data);
|
2017-06-30 14:41:57 -04:00
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
await _cipherRepository.UpdateAttachmentAsync(new CipherAttachment
|
2017-06-30 14:41:57 -04:00
|
|
|
|
{
|
2021-03-30 18:41:14 -05:00
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
AttachmentId = attachmentId,
|
2022-01-21 09:36:25 -05:00
|
|
|
|
AttachmentData = JsonSerializer.Serialize(data)
|
2021-03-30 18:41:14 -05:00
|
|
|
|
});
|
|
|
|
|
|
cipher.AddAttachment(attachmentId, data);
|
|
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
|
|
|
|
|
|
|
|
|
|
|
return (attachmentId, uploadUrl);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task CreateAttachmentAsync(Cipher cipher, Stream stream, string fileName, string key,
|
|
|
|
|
|
long requestLength, Guid savingUserId, bool orgAdmin = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ValidateCipherEditForAttachmentAsync(cipher, savingUserId, orgAdmin, requestLength);
|
2017-06-30 11:15:58 -04:00
|
|
|
|
|
|
|
|
|
|
var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
|
2021-02-22 15:35:16 -06:00
|
|
|
|
var data = new CipherAttachment.MetaData
|
|
|
|
|
|
{
|
|
|
|
|
|
AttachmentId = attachmentId,
|
|
|
|
|
|
FileName = fileName,
|
|
|
|
|
|
Key = key,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await _attachmentStorageService.UploadNewAttachmentAsync(stream, cipher, data);
|
2021-03-09 10:49:49 -06:00
|
|
|
|
// Must read stream length after it has been saved, otherwise it's 0
|
|
|
|
|
|
data.Size = stream.Length;
|
2017-06-30 11:15:58 -04:00
|
|
|
|
|
2017-06-30 14:41:57 -04:00
|
|
|
|
try
|
2017-06-30 11:15:58 -04:00
|
|
|
|
{
|
2017-06-30 14:41:57 -04:00
|
|
|
|
var attachment = new CipherAttachment
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
AttachmentId = attachmentId,
|
2022-01-21 09:36:25 -05:00
|
|
|
|
AttachmentData = JsonSerializer.Serialize(data)
|
2017-06-30 14:41:57 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await _cipherRepository.UpdateAttachmentAsync(attachment);
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_AttachmentCreated);
|
2017-07-07 11:07:22 -04:00
|
|
|
|
cipher.AddAttachment(attachmentId, data);
|
2021-03-30 18:41:14 -05:00
|
|
|
|
|
2021-04-05 15:20:13 -05:00
|
|
|
|
if (!await ValidateCipherAttachmentFile(cipher, data))
|
|
|
|
|
|
{
|
2021-03-30 18:41:14 -05:00
|
|
|
|
throw new Exception("Content-Length does not match uploaded file size");
|
|
|
|
|
|
}
|
2017-06-30 14:41:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
2017-06-30 11:15:58 -04:00
|
|
|
|
{
|
2017-06-30 14:41:57 -04:00
|
|
|
|
// Clean up since this is not transactional
|
2021-02-22 15:35:16 -06:00
|
|
|
|
await _attachmentStorageService.DeleteAttachmentAsync(cipher.Id, data);
|
2017-06-30 14:41:57 -04:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2017-06-30 11:15:58 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
2017-06-30 11:15:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-14 17:19:04 -05:00
|
|
|
|
public async Task CreateAttachmentShareAsync(Cipher cipher, Stream stream, long requestLength,
|
2017-07-10 14:30:12 -04:00
|
|
|
|
string attachmentId, Guid organizationId)
|
|
|
|
|
|
{
|
2017-07-10 16:21:18 -04:00
|
|
|
|
try
|
2017-07-10 14:30:12 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (requestLength < 1)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("No data to attach.");
|
|
|
|
|
|
}
|
2017-07-10 14:30:12 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.Id == default(Guid))
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException(nameof(cipher.Id));
|
|
|
|
|
|
}
|
2017-07-10 14:30:12 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cipher belongs to an organization already.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(organizationId);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (org == null || !org.MaxStorageGb.HasValue)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("This organization cannot use attachments.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var storageBytesRemaining = org.StorageBytesRemaining();
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (storageBytesRemaining < requestLength)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Not enough storage available for this organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-22 15:35:16 -06:00
|
|
|
|
var attachments = cipher.GetAttachments();
|
|
|
|
|
|
if (!attachments.ContainsKey(attachmentId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException($"Cipher does not own specified attachment");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 23:11:57 -04:00
|
|
|
|
await _attachmentStorageService.UploadShareAttachmentAsync(stream, cipher.Id, organizationId,
|
2021-02-22 15:35:16 -06:00
|
|
|
|
attachments[attachmentId]);
|
|
|
|
|
|
|
|
|
|
|
|
// Previous call may alter metadata
|
|
|
|
|
|
var updatedAttachment = new CipherAttachment
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
AttachmentId = attachmentId,
|
2022-01-21 09:36:25 -05:00
|
|
|
|
AttachmentData = JsonSerializer.Serialize(attachments[attachmentId])
|
2021-02-22 15:35:16 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await _cipherRepository.UpdateAttachmentAsync(updatedAttachment);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
2017-07-10 16:21:18 -04:00
|
|
|
|
catch
|
|
|
|
|
|
{
|
2017-07-10 20:48:06 -04:00
|
|
|
|
await _attachmentStorageService.CleanupAsync(cipher.Id);
|
2017-07-10 16:21:18 -04:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
public async Task<bool> ValidateCipherAttachmentFile(Cipher cipher, CipherAttachment.MetaData attachmentData)
|
|
|
|
|
|
{
|
|
|
|
|
|
var (valid, realSize) = await _attachmentStorageService.ValidateFileAsync(cipher, attachmentData, _fileSizeLeeway);
|
|
|
|
|
|
|
|
|
|
|
|
if (!valid || realSize > MAX_FILE_SIZE)
|
|
|
|
|
|
{
|
|
|
|
|
|
// File reported differs in size from that promised. Must be a rogue client. Delete Send
|
|
|
|
|
|
await DeleteAttachmentAsync(cipher, attachmentData);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Update Send data if necessary
|
|
|
|
|
|
if (realSize != attachmentData.Size)
|
|
|
|
|
|
{
|
|
|
|
|
|
attachmentData.Size = realSize.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
attachmentData.Validated = true;
|
|
|
|
|
|
|
|
|
|
|
|
var updatedAttachment = new CipherAttachment
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
AttachmentId = attachmentData.AttachmentId,
|
2022-01-21 09:36:25 -05:00
|
|
|
|
AttachmentData = JsonSerializer.Serialize(attachmentData)
|
2021-03-30 18:41:14 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await _cipherRepository.UpdateAttachmentAsync(updatedAttachment);
|
|
|
|
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-14 15:05:07 +00:00
|
|
|
|
public async Task<AttachmentResponseData> GetAttachmentDownloadDataAsync(Cipher cipher, string attachmentId)
|
2021-03-30 18:41:14 -05:00
|
|
|
|
{
|
|
|
|
|
|
var attachments = cipher?.GetAttachments() ?? new Dictionary<string, CipherAttachment.MetaData>();
|
|
|
|
|
|
|
2021-04-26 14:36:06 -05:00
|
|
|
|
if (!attachments.ContainsKey(attachmentId))
|
2021-03-30 18:41:14 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var data = attachments[attachmentId];
|
2021-12-14 15:05:07 +00:00
|
|
|
|
var response = new AttachmentResponseData
|
2021-03-30 18:41:14 -05:00
|
|
|
|
{
|
2021-12-14 15:05:07 +00:00
|
|
|
|
Cipher = cipher,
|
|
|
|
|
|
Data = data,
|
|
|
|
|
|
Id = attachmentId,
|
|
|
|
|
|
Url = await _attachmentStorageService.GetAttachmentDownloadUrlAsync(cipher, data),
|
2021-03-30 18:41:14 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-19 16:00:47 -04:00
|
|
|
|
public async Task DeleteAsync(Cipher cipher, Guid deletingUserId, bool orgAdmin = false)
|
2016-06-29 01:15:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!orgAdmin && !(await UserCanEditAsync(cipher, deletingUserId)))
|
2017-03-24 09:27:15 -04:00
|
|
|
|
{
|
2017-04-20 16:19:23 -04:00
|
|
|
|
throw new BadRequestException("You do not have permissions to delete this.");
|
2017-03-24 09:27:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-29 01:15:37 -04:00
|
|
|
|
await _cipherRepository.DeleteAsync(cipher);
|
2017-07-10 20:48:06 -04:00
|
|
|
|
await _attachmentStorageService.DeleteAttachmentsForCipherAsync(cipher.Id);
|
2020-04-01 13:00:25 -04:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_Deleted);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2017-04-21 14:22:32 -04:00
|
|
|
|
await _pushService.PushSyncCipherDeleteAsync(cipher);
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 11:38:53 -05:00
|
|
|
|
public async Task DeleteManyAsync(IEnumerable<Guid> cipherIds, Guid deletingUserId, Guid? organizationId = null, bool orgAdmin = false)
|
2017-06-09 00:30:59 -04:00
|
|
|
|
{
|
2019-01-10 22:24:08 -05:00
|
|
|
|
var cipherIdsSet = new HashSet<Guid>(cipherIds);
|
2020-07-22 11:38:53 -05:00
|
|
|
|
var deletingCiphers = new List<Cipher>();
|
2019-01-10 22:24:08 -05:00
|
|
|
|
|
2020-07-22 11:38:53 -05:00
|
|
|
|
if (orgAdmin && organizationId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ciphers = await _cipherRepository.GetManyByOrganizationIdAsync(organizationId.Value);
|
|
|
|
|
|
deletingCiphers = ciphers.Where(c => cipherIdsSet.Contains(c.Id)).ToList();
|
|
|
|
|
|
await _cipherRepository.DeleteByIdsOrganizationIdAsync(deletingCiphers.Select(c => c.Id), organizationId.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var ciphers = await _cipherRepository.GetManyByUserIdAsync(deletingUserId);
|
|
|
|
|
|
deletingCiphers = ciphers.Where(c => cipherIdsSet.Contains(c.Id) && c.Edit).Select(x => (Cipher)x).ToList();
|
|
|
|
|
|
await _cipherRepository.DeleteAsync(deletingCiphers.Select(c => c.Id), deletingUserId);
|
|
|
|
|
|
}
|
2019-01-10 22:24:08 -05:00
|
|
|
|
|
2019-07-25 15:50:13 -04:00
|
|
|
|
var events = deletingCiphers.Select(c =>
|
2019-07-25 15:39:25 -04:00
|
|
|
|
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Deleted, null));
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var eventsBatch in events.Batch(100))
|
2019-07-25 15:50:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
|
|
|
|
|
}
|
2019-01-10 22:24:08 -05:00
|
|
|
|
|
2017-06-09 00:30:59 -04:00
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCiphersAsync(deletingUserId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 23:11:57 -04:00
|
|
|
|
public async Task DeleteAttachmentAsync(Cipher cipher, string attachmentId, Guid deletingUserId,
|
|
|
|
|
|
bool orgAdmin = false)
|
2017-07-07 11:07:22 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!orgAdmin && !(await UserCanEditAsync(cipher, deletingUserId)))
|
2017-07-07 11:07:22 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("You do not have permissions to delete this.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!cipher.ContainsAttachment(attachmentId))
|
2017-07-07 11:07:22 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
await DeleteAttachmentAsync(cipher, cipher.GetAttachments()[attachmentId]);
|
2017-07-07 11:07:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-25 09:12:50 -04:00
|
|
|
|
public async Task PurgeAsync(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(organizationId);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (org == null)
|
2018-09-25 09:12:50 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
await _cipherRepository.DeleteByOrganizationIdAsync(organizationId);
|
|
|
|
|
|
await _eventService.LogOrganizationEventAsync(org, Enums.EventType.Organization_PurgedVault);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-09 09:48:44 -04:00
|
|
|
|
public async Task MoveManyAsync(IEnumerable<Guid> cipherIds, Guid? destinationFolderId, Guid movingUserId)
|
2017-06-09 00:30:59 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (destinationFolderId.HasValue)
|
2017-06-09 09:48:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
var folder = await _folderRepository.GetByIdAsync(destinationFolderId.Value);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (folder == null || folder.UserId != movingUserId)
|
2017-06-09 09:48:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Invalid folder.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-09 00:30:59 -04:00
|
|
|
|
await _cipherRepository.MoveAsync(cipherIds, destinationFolderId, movingUserId);
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCiphersAsync(movingUserId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-18 11:58:02 -04:00
|
|
|
|
public async Task SaveFolderAsync(Folder folder)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (folder.Id == default(Guid))
|
2017-03-18 11:58:02 -04:00
|
|
|
|
{
|
|
|
|
|
|
await _folderRepository.CreateAsync(folder);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-04-21 14:22:32 -04:00
|
|
|
|
await _pushService.PushSyncFolderCreateAsync(folder);
|
2017-03-18 11:58:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
folder.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
await _folderRepository.UpsertAsync(folder);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-04-21 14:22:32 -04:00
|
|
|
|
await _pushService.PushSyncFolderUpdateAsync(folder);
|
2017-03-18 11:58:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFolderAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _folderRepository.DeleteAsync(folder);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-04-21 14:22:32 -04:00
|
|
|
|
await _pushService.PushSyncFolderDeleteAsync(folder);
|
2017-03-18 11:58:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 14:30:12 -04:00
|
|
|
|
public async Task ShareAsync(Cipher originalCipher, Cipher cipher, Guid organizationId,
|
2020-11-23 08:48:05 -06:00
|
|
|
|
IEnumerable<Guid> collectionIds, Guid sharingUserId, DateTime? lastKnownRevisionDate)
|
2017-03-21 00:04:39 -04:00
|
|
|
|
{
|
2017-07-10 14:30:12 -04:00
|
|
|
|
var attachments = cipher.GetAttachments();
|
2018-11-15 12:52:31 -05:00
|
|
|
|
var hasAttachments = attachments?.Any() ?? false;
|
|
|
|
|
|
var hasOldAttachments = attachments?.Any(a => a.Key == null) ?? false;
|
2017-07-10 16:21:18 -04:00
|
|
|
|
var updatedCipher = false;
|
|
|
|
|
|
var migratedAttachments = false;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
var originalAttachments = CoreHelpers.CloneObject(attachments);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.Id == default(Guid))
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException(nameof(cipher.Id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Already belongs to an organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!cipher.UserId.HasValue || cipher.UserId.Value != sharingUserId)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(organizationId);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (hasAttachments && !org.MaxStorageGb.HasValue)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("This organization cannot use attachments.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 22:08:52 -04:00
|
|
|
|
var storageAdjustment = attachments?.Sum(a => a.Value.Size) ?? 0;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (org.StorageBytesRemaining() < storageAdjustment)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Not enough storage available for this organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
|
|
|
|
|
|
|
2017-07-10 14:30:12 -04:00
|
|
|
|
// Sproc will not save this UserId on the cipher. It is used limit scope of the collectionIds.
|
|
|
|
|
|
cipher.UserId = sharingUserId;
|
|
|
|
|
|
cipher.OrganizationId = organizationId;
|
|
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!await _cipherRepository.ReplaceAsync(cipher, collectionIds))
|
2017-08-30 18:18:39 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Unable to save.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 16:21:18 -04:00
|
|
|
|
updatedCipher = true;
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Shared);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (hasOldAttachments)
|
2017-07-10 14:30:12 -04:00
|
|
|
|
{
|
2018-11-15 12:52:31 -05:00
|
|
|
|
// migrate old attachments
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var attachment in attachments.Where(a => a.Key == null))
|
2017-07-10 14:30:12 -04:00
|
|
|
|
{
|
2018-08-21 23:11:57 -04:00
|
|
|
|
await _attachmentStorageService.StartShareAttachmentAsync(cipher.Id, organizationId,
|
2021-02-22 15:35:16 -06:00
|
|
|
|
attachment.Value);
|
2017-07-10 16:21:18 -04:00
|
|
|
|
migratedAttachments = true;
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
2018-11-15 12:52:31 -05:00
|
|
|
|
|
|
|
|
|
|
// commit attachment migration
|
|
|
|
|
|
await _attachmentStorageService.CleanupAsync(cipher.Id);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
2018-11-15 12:52:31 -05:00
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// roll everything back
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (updatedCipher)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.ReplaceAsync(originalCipher);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!hasOldAttachments || !migratedAttachments)
|
2017-07-10 14:30:12 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (updatedCipher)
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
2017-07-10 22:08:52 -04:00
|
|
|
|
await _userRepository.UpdateStorageAsync(sharingUserId);
|
|
|
|
|
|
await _organizationRepository.UpdateStorageAsync(organizationId);
|
2017-07-10 16:21:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var attachment in attachments.Where(a => a.Key == null))
|
2017-07-10 14:30:12 -04:00
|
|
|
|
{
|
2018-08-21 23:11:57 -04:00
|
|
|
|
await _attachmentStorageService.RollbackShareAttachmentAsync(cipher.Id, organizationId,
|
2021-02-22 15:35:16 -06:00
|
|
|
|
attachment.Value, originalAttachments[attachment.Key].ContainerName);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 20:48:06 -04:00
|
|
|
|
await _attachmentStorageService.CleanupAsync(cipher.Id);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2017-04-12 12:42:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
public async Task ShareManyAsync(IEnumerable<(Cipher cipher, DateTime? lastKnownRevisionDate)> cipherInfos,
|
|
|
|
|
|
Guid organizationId, IEnumerable<Guid> collectionIds, Guid sharingUserId)
|
2018-06-13 14:03:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
var cipherIds = new List<Guid>();
|
2020-11-23 08:48:05 -06:00
|
|
|
|
foreach (var (cipher, lastKnownRevisionDate) in cipherInfos)
|
2018-06-13 14:03:44 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.Id == default(Guid))
|
2018-06-13 14:03:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("All ciphers must already exist.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
2018-06-13 14:03:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("One or more ciphers already belong to an organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!cipher.UserId.HasValue || cipher.UserId.Value != sharingUserId)
|
2018-06-13 14:03:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("One or more ciphers do not belong to you.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
|
|
|
|
|
|
|
2018-06-13 14:03:44 -04:00
|
|
|
|
cipher.UserId = null;
|
|
|
|
|
|
cipher.OrganizationId = organizationId;
|
|
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
cipherIds.Add(cipher.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
await _cipherRepository.UpdateCiphersAsync(sharingUserId, cipherInfos.Select(c => c.cipher));
|
2018-06-13 14:03:44 -04:00
|
|
|
|
await _collectionCipherRepository.UpdateCollectionsForCiphersAsync(cipherIds, sharingUserId,
|
|
|
|
|
|
organizationId, collectionIds);
|
|
|
|
|
|
|
2020-11-23 08:48:05 -06:00
|
|
|
|
var events = cipherInfos.Select(c =>
|
|
|
|
|
|
new Tuple<Cipher, EventType, DateTime?>(c.cipher, EventType.Cipher_Shared, null));
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var eventsBatch in events.Batch(100))
|
2019-07-25 15:50:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
|
|
|
|
|
}
|
2018-06-13 14:03:44 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCiphersAsync(sharingUserId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 23:11:57 -04:00
|
|
|
|
public async Task SaveCollectionsAsync(Cipher cipher, IEnumerable<Guid> collectionIds, Guid savingUserId,
|
|
|
|
|
|
bool orgAdmin)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.Id == default(Guid))
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException(nameof(cipher.Id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!cipher.OrganizationId.HasValue)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cipher must belong to an organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 23:11:57 -04:00
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
// The sprocs will validate that all collections belong to this org/user and that they have
|
|
|
|
|
|
// proper write permissions.
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (orgAdmin)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
2018-08-21 23:11:57 -04:00
|
|
|
|
await _collectionCipherRepository.UpdateCollectionsForAdminAsync(cipher.Id,
|
|
|
|
|
|
cipher.OrganizationId.Value, collectionIds);
|
2017-04-17 23:12:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!(await UserCanEditAsync(cipher, savingUserId)))
|
2018-08-23 23:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("You do not have permissions to edit this.");
|
|
|
|
|
|
}
|
2017-04-27 09:19:30 -04:00
|
|
|
|
await _collectionCipherRepository.UpdateCollectionsAsync(cipher.Id, savingUserId, collectionIds);
|
2017-04-12 12:42:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-01 14:06:16 -05:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_UpdatedCollections);
|
|
|
|
|
|
|
2017-04-12 12:42:00 -04:00
|
|
|
|
// push
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
|
2017-03-21 00:04:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-26 23:09:53 -05:00
|
|
|
|
public async Task ImportCiphersAsync(
|
2017-03-18 11:58:02 -04:00
|
|
|
|
List<Folder> folders,
|
|
|
|
|
|
List<CipherDetails> ciphers,
|
2016-05-21 17:16:22 -04:00
|
|
|
|
IEnumerable<KeyValuePair<int, int>> folderRelationships)
|
2015-12-26 23:09:53 -05:00
|
|
|
|
{
|
2021-09-08 07:20:05 +10:00
|
|
|
|
var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;
|
|
|
|
|
|
|
2021-09-28 06:54:28 +10:00
|
|
|
|
// Make sure the user can save new ciphers to their personal vault
|
|
|
|
|
|
var personalOwnershipPolicyCount = await _policyRepository.GetCountByTypeApplicableToUserIdAsync(userId.Value,
|
|
|
|
|
|
PolicyType.PersonalOwnership);
|
|
|
|
|
|
if (personalOwnershipPolicyCount > 0)
|
2021-09-08 07:20:05 +10:00
|
|
|
|
{
|
2021-09-28 06:54:28 +10:00
|
|
|
|
throw new BadRequestException("You cannot import items into your personal vault because you are " +
|
|
|
|
|
|
"a member of an organization which forbids it.");
|
2021-09-08 07:20:05 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var cipher in ciphers)
|
2017-04-12 16:48:38 -04:00
|
|
|
|
{
|
|
|
|
|
|
cipher.SetNewId();
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.UserId.HasValue && cipher.Favorite)
|
2017-04-12 16:48:38 -04:00
|
|
|
|
{
|
2017-04-17 11:44:09 -04:00
|
|
|
|
cipher.Favorites = $"{{\"{cipher.UserId.ToString().ToUpperInvariant()}\":\"true\"}}";
|
2017-04-12 16:48:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Init. ids for folders
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var folder in folders)
|
2015-12-26 23:09:53 -05:00
|
|
|
|
{
|
2017-04-12 16:48:38 -04:00
|
|
|
|
folder.SetNewId();
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-12 16:48:38 -04:00
|
|
|
|
// Create the folder associations based on the newly created folder ids
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var relationship in folderRelationships)
|
2015-12-26 23:09:53 -05:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
var cipher = ciphers.ElementAtOrDefault(relationship.Key);
|
2015-12-26 23:09:53 -05:00
|
|
|
|
var folder = folders.ElementAtOrDefault(relationship.Value);
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher == null || folder == null)
|
2015-12-26 23:09:53 -05:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-17 11:44:09 -04:00
|
|
|
|
cipher.Folders = $"{{\"{cipher.UserId.ToString().ToUpperInvariant()}\":" +
|
|
|
|
|
|
$"\"{folder.Id.ToString().ToUpperInvariant()}\"}}";
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-12 16:48:38 -04:00
|
|
|
|
// Create it all
|
2017-04-15 22:26:45 -04:00
|
|
|
|
await _cipherRepository.CreateAsync(ciphers, folders);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (userId.HasValue)
|
2016-06-29 01:15:37 -04:00
|
|
|
|
{
|
2017-04-21 14:22:32 -04:00
|
|
|
|
await _pushService.PushSyncVaultAsync(userId.Value);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
}
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
2017-03-24 09:27:15 -04:00
|
|
|
|
|
2017-09-05 17:49:34 -04:00
|
|
|
|
public async Task ImportCiphersAsync(
|
|
|
|
|
|
List<Collection> collections,
|
|
|
|
|
|
List<CipherDetails> ciphers,
|
|
|
|
|
|
IEnumerable<KeyValuePair<int, int>> collectionRelationships,
|
|
|
|
|
|
Guid importingUserId)
|
|
|
|
|
|
{
|
2021-08-10 14:38:58 -04:00
|
|
|
|
var org = collections.Count > 0 ?
|
|
|
|
|
|
await _organizationRepository.GetByIdAsync(collections[0].OrganizationId) :
|
|
|
|
|
|
await _organizationRepository.GetByIdAsync(ciphers.FirstOrDefault(c => c.OrganizationId.HasValue).OrganizationId.Value);
|
|
|
|
|
|
|
|
|
|
|
|
if (collections.Count > 0 && org != null && org.MaxCollections.HasValue)
|
2018-07-17 13:34:12 -04:00
|
|
|
|
{
|
2021-08-10 14:38:58 -04:00
|
|
|
|
var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id);
|
|
|
|
|
|
if (org.MaxCollections.Value < (collectionCount + collections.Count))
|
2018-07-17 13:34:12 -04:00
|
|
|
|
{
|
2021-08-10 14:38:58 -04:00
|
|
|
|
throw new BadRequestException("This organization can only have a maximum of " +
|
|
|
|
|
|
$"{org.MaxCollections.Value} collections.");
|
2018-07-17 13:34:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-05 17:49:34 -04:00
|
|
|
|
// Init. ids for ciphers
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var cipher in ciphers)
|
2017-09-05 17:49:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
cipher.SetNewId();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Init. ids for collections
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var collection in collections)
|
2017-09-05 17:49:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
collection.SetNewId();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create associations based on the newly assigned ids
|
|
|
|
|
|
var collectionCiphers = new List<CollectionCipher>();
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var relationship in collectionRelationships)
|
2017-09-05 17:49:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
var cipher = ciphers.ElementAtOrDefault(relationship.Key);
|
|
|
|
|
|
var collection = collections.ElementAtOrDefault(relationship.Value);
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher == null || collection == null)
|
2017-09-05 17:49:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
collectionCiphers.Add(new CollectionCipher
|
|
|
|
|
|
{
|
|
|
|
|
|
CipherId = cipher.Id,
|
|
|
|
|
|
CollectionId = collection.Id
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create it all
|
|
|
|
|
|
await _cipherRepository.CreateAsync(ciphers, collections, collectionCiphers);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncVaultAsync(importingUserId);
|
2021-08-10 14:38:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (org != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _referenceEventService.RaiseEventAsync(
|
|
|
|
|
|
new ReferenceEvent(ReferenceEventType.VaultImported, org));
|
|
|
|
|
|
}
|
2017-09-05 17:49:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-01 13:00:25 -04:00
|
|
|
|
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.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-01 16:39:27 -04:00
|
|
|
|
if (cipher.DeletedDate.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Already soft-deleted, we can safely ignore this
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-02 10:56:22 -04:00
|
|
|
|
cipher.DeletedDate = cipher.RevisionDate = DateTime.UtcNow;
|
2020-04-01 16:39:27 -04:00
|
|
|
|
|
2020-04-08 16:18:22 -04:00
|
|
|
|
if (cipher is CipherDetails details)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.UpsertAsync(details);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.UpsertAsync(cipher);
|
|
|
|
|
|
}
|
2020-04-01 13:00:25 -04:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_SoftDeleted);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 11:38:53 -05:00
|
|
|
|
public async Task SoftDeleteManyAsync(IEnumerable<Guid> cipherIds, Guid deletingUserId, Guid? organizationId, bool orgAdmin)
|
2020-04-01 13:00:25 -04:00
|
|
|
|
{
|
|
|
|
|
|
var cipherIdsSet = new HashSet<Guid>(cipherIds);
|
2020-07-22 11:38:53 -05:00
|
|
|
|
var deletingCiphers = new List<Cipher>();
|
2020-04-01 13:00:25 -04:00
|
|
|
|
|
2020-07-22 11:38:53 -05:00
|
|
|
|
if (orgAdmin && organizationId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ciphers = await _cipherRepository.GetManyByOrganizationIdAsync(organizationId.Value);
|
|
|
|
|
|
deletingCiphers = ciphers.Where(c => cipherIdsSet.Contains(c.Id)).ToList();
|
|
|
|
|
|
await _cipherRepository.SoftDeleteByIdsOrganizationIdAsync(deletingCiphers.Select(c => c.Id), organizationId.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var ciphers = await _cipherRepository.GetManyByUserIdAsync(deletingUserId);
|
|
|
|
|
|
deletingCiphers = ciphers.Where(c => cipherIdsSet.Contains(c.Id) && c.Edit).Select(x => (Cipher)x).ToList();
|
2021-03-30 18:41:14 -05:00
|
|
|
|
await _cipherRepository.SoftDeleteAsync(deletingCiphers.Select(c => c.Id), deletingUserId);
|
2020-07-22 11:38:53 -05:00
|
|
|
|
}
|
2020-04-01 13:00:25 -04:00
|
|
|
|
|
|
|
|
|
|
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.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-01 16:39:27 -04:00
|
|
|
|
if (!cipher.DeletedDate.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Already restored, we can safely ignore this
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cipher.DeletedDate = null;
|
|
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
|
2020-04-08 16:18:22 -04:00
|
|
|
|
if (cipher is CipherDetails details)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.UpsertAsync(details);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.UpsertAsync(cipher);
|
|
|
|
|
|
}
|
2020-04-01 13:00:25 -04:00
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, EventType.Cipher_Restored);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-08 08:52:42 -06:00
|
|
|
|
public async Task RestoreManyAsync(IEnumerable<CipherDetails> ciphers, Guid restoringUserId)
|
2020-04-01 13:00:25 -04:00
|
|
|
|
{
|
2021-01-08 08:52:42 -06:00
|
|
|
|
var revisionDate = await _cipherRepository.RestoreAsync(ciphers.Select(c => c.Id), restoringUserId);
|
2020-04-01 13:00:25 -04:00
|
|
|
|
|
2021-01-08 08:52:42 -06:00
|
|
|
|
var events = ciphers.Select(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
c.RevisionDate = revisionDate;
|
|
|
|
|
|
c.DeletedDate = null;
|
|
|
|
|
|
return new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Restored, null);
|
|
|
|
|
|
});
|
2020-04-01 13:00:25 -04:00
|
|
|
|
foreach (var eventsBatch in events.Batch(100))
|
|
|
|
|
|
{
|
|
|
|
|
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCiphersAsync(restoringUserId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-25 16:25:10 -04:00
|
|
|
|
private async Task<bool> UserCanEditAsync(Cipher cipher, Guid userId)
|
2017-03-24 09:27:15 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!cipher.OrganizationId.HasValue && cipher.UserId.HasValue && cipher.UserId.Value == userId)
|
2017-03-24 09:27:15 -04:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-06 23:23:01 -04:00
|
|
|
|
return await _cipherRepository.GetCanEditByIdAsync(userId, cipher.Id);
|
2017-03-24 16:15:50 -04:00
|
|
|
|
}
|
2020-11-23 08:48:05 -06:00
|
|
|
|
|
|
|
|
|
|
private void ValidateCipherLastKnownRevisionDateAsync(Cipher cipher, DateTime? lastKnownRevisionDate)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (cipher.Id == default || !lastKnownRevisionDate.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((cipher.RevisionDate - lastKnownRevisionDate.Value).Duration() > TimeSpan.FromSeconds(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
|
"The cipher you are updating is out of date. Please save your work, sync your vault, and try again."
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-03-30 18:41:14 -05:00
|
|
|
|
|
|
|
|
|
|
private async Task DeleteAttachmentAsync(Cipher cipher, CipherAttachment.MetaData attachmentData)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (attachmentData == null || string.IsNullOrWhiteSpace(attachmentData.AttachmentId))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-04-05 15:20:13 -05:00
|
|
|
|
|
2021-03-30 18:41:14 -05:00
|
|
|
|
await _cipherRepository.DeleteAttachmentAsync(cipher.Id, attachmentData.AttachmentId);
|
|
|
|
|
|
cipher.DeleteAttachment(attachmentData.AttachmentId);
|
|
|
|
|
|
await _attachmentStorageService.DeleteAttachmentAsync(cipher.Id, attachmentData);
|
|
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_AttachmentDeleted);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
await _pushService.PushSyncCipherUpdateAsync(cipher, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ValidateCipherEditForAttachmentAsync(Cipher cipher, Guid savingUserId, bool orgAdmin,
|
|
|
|
|
|
long requestLength)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!orgAdmin && !(await UserCanEditAsync(cipher, savingUserId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("You do not have permissions to edit this.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (requestLength < 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("No data to attach.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var storageBytesRemaining = await StorageBytesRemainingForCipherAsync(cipher);
|
|
|
|
|
|
|
|
|
|
|
|
if (storageBytesRemaining < requestLength)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Not enough storage available.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<long> StorageBytesRemainingForCipherAsync(Cipher cipher)
|
|
|
|
|
|
{
|
|
|
|
|
|
var storageBytesRemaining = 0L;
|
|
|
|
|
|
if (cipher.UserId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await _userRepository.GetByIdAsync(cipher.UserId.Value);
|
|
|
|
|
|
if (!(await _userService.CanAccessPremium(user)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("You must have premium status to use attachments.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (user.Premium)
|
|
|
|
|
|
{
|
|
|
|
|
|
storageBytesRemaining = user.StorageBytesRemaining();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Users that get access to file storage/premium from their organization get the default
|
|
|
|
|
|
// 1 GB max storage.
|
|
|
|
|
|
storageBytesRemaining = user.StorageBytesRemaining(
|
|
|
|
|
|
_globalSettings.SelfHosted ? (short)10240 : (short)1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cipher.OrganizationId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value);
|
|
|
|
|
|
if (!org.MaxStorageGb.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("This organization cannot use attachments.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
storageBytesRemaining = org.StorageBytesRemaining();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return storageBytesRemaining;
|
|
|
|
|
|
}
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|