2015-12-26 23:09:53 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2017-03-08 21:45:08 -05:00
|
|
|
|
using Bit.Core.Models.Table;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
2017-03-18 11:58:02 -04:00
|
|
|
|
using Core.Models.Data;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
using Bit.Core.Exceptions;
|
2015-12-26 23:09:53 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class CipherService : ICipherService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
2017-03-18 11:58:02 -04:00
|
|
|
|
private readonly IFolderRepository _folderRepository;
|
2017-02-25 23:38:24 -05:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
|
private readonly ISubvaultUserRepository _subvaultUserRepository;
|
2017-04-12 12:42:00 -04:00
|
|
|
|
private readonly ISubvaultCipherRepository _subvaultCipherRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
private readonly IPushService _pushService;
|
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,
|
2017-02-25 23:38:24 -05:00
|
|
|
|
IUserRepository userRepository,
|
2017-03-21 00:04:39 -04:00
|
|
|
|
IOrganizationRepository organizationRepository,
|
|
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
|
|
|
|
|
ISubvaultUserRepository subvaultUserRepository,
|
2017-04-12 12:42:00 -04:00
|
|
|
|
ISubvaultCipherRepository subvaultCipherRepository,
|
2016-06-29 01:15:37 -04:00
|
|
|
|
IPushService pushService)
|
2015-12-26 23:09:53 -05:00
|
|
|
|
{
|
|
|
|
|
|
_cipherRepository = cipherRepository;
|
2017-03-18 11:58:02 -04:00
|
|
|
|
_folderRepository = folderRepository;
|
2017-02-25 23:38:24 -05:00
|
|
|
|
_userRepository = userRepository;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
|
|
|
|
|
_subvaultUserRepository = subvaultUserRepository;
|
2017-04-12 12:42:00 -04:00
|
|
|
|
_subvaultCipherRepository = subvaultCipherRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
_pushService = pushService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-24 09:27:15 -04:00
|
|
|
|
public async Task SaveAsync(CipherDetails cipher, Guid savingUserId)
|
2016-06-29 01:15:37 -04:00
|
|
|
|
{
|
2017-03-24 16:15:50 -04:00
|
|
|
|
if(!(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;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
if(cipher.Id == default(Guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
await _cipherRepository.CreateAsync(cipher);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-03-21 21:21:28 -04:00
|
|
|
|
//await _pushService.PushSyncCipherCreateAsync(cipher);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-06-30 21:31:12 -04:00
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
await _cipherRepository.ReplaceAsync(cipher);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-03-21 21:21:28 -04:00
|
|
|
|
//await _pushService.PushSyncCipherUpdateAsync(cipher);
|
2016-06-29 01:15:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-04-19 16:00:47 -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);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-03-21 21:21:28 -04:00
|
|
|
|
//await _pushService.PushSyncCipherDeleteAsync(cipher);
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-18 11:58:02 -04:00
|
|
|
|
public async Task SaveFolderAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(folder.Id == default(Guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
await _folderRepository.CreateAsync(folder);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
//await _pushService.PushSyncCipherCreateAsync(cipher);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
folder.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
await _folderRepository.UpsertAsync(folder);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
//await _pushService.PushSyncCipherUpdateAsync(cipher);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFolderAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _folderRepository.DeleteAsync(folder);
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
|
|
|
|
|
//await _pushService.PushSyncCipherDeleteAsync(cipher);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-12 12:42:00 -04:00
|
|
|
|
public async Task ShareAsync(Cipher cipher, Guid organizationId, IEnumerable<Guid> subvaultIds, Guid sharingUserId)
|
2017-03-21 00:04:39 -04:00
|
|
|
|
{
|
|
|
|
|
|
if(cipher.Id == default(Guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException(nameof(cipher.Id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-04 22:07:30 -04:00
|
|
|
|
if(cipher.OrganizationId.HasValue)
|
2017-03-21 00:04:39 -04:00
|
|
|
|
{
|
2017-04-04 22:07:30 -04:00
|
|
|
|
throw new BadRequestException("Already belongs to an organization.");
|
2017-03-21 00:04:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-12 12:42:00 -04:00
|
|
|
|
if(!cipher.UserId.HasValue || cipher.UserId.Value != sharingUserId)
|
2017-03-25 16:25:10 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-17 23:12:48 -04:00
|
|
|
|
// Sproc will not save this UserId on the cipher. It is used limit scope of the subvaultIds.
|
|
|
|
|
|
cipher.UserId = sharingUserId;
|
2017-03-25 16:25:10 -04:00
|
|
|
|
cipher.OrganizationId = organizationId;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
cipher.RevisionDate = DateTime.UtcNow;
|
2017-04-17 23:12:48 -04:00
|
|
|
|
await _cipherRepository.ReplaceAsync(cipher, subvaultIds);
|
2017-03-21 00:04:39 -04:00
|
|
|
|
|
|
|
|
|
|
// push
|
2017-04-12 12:42:00 -04:00
|
|
|
|
//await _pushService.PushSyncCipherUpdateAsync(cipher);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-17 23:12:48 -04:00
|
|
|
|
public async Task SaveSubvaultsAsync(Cipher cipher, IEnumerable<Guid> subvaultIds, Guid savingUserId, bool orgAdmin)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
if(cipher.Id == default(Guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException(nameof(cipher.Id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!cipher.OrganizationId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cipher must belong to an organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-17 23:12:48 -04:00
|
|
|
|
// The sprocs will validate that all subvaults belong to this org/user and that they have proper write permissions.
|
|
|
|
|
|
if(orgAdmin)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
2017-04-17 23:12:48 -04:00
|
|
|
|
await _subvaultCipherRepository.UpdateSubvaultsForAdminAsync(cipher.Id, cipher.OrganizationId.Value,
|
|
|
|
|
|
subvaultIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await _subvaultCipherRepository.UpdateSubvaultsAsync(cipher.Id, savingUserId, subvaultIds);
|
2017-04-12 12:42:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// push
|
2017-03-21 21:13:20 -04:00
|
|
|
|
//await _pushService.PushSyncCipherUpdateAsync(cipher);
|
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
|
|
|
|
{
|
2017-04-12 16:48:38 -04:00
|
|
|
|
foreach(var cipher in ciphers)
|
|
|
|
|
|
{
|
|
|
|
|
|
cipher.SetNewId();
|
|
|
|
|
|
|
|
|
|
|
|
if(cipher.UserId.HasValue && cipher.Favorite)
|
|
|
|
|
|
{
|
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
|
2015-12-26 23:09:53 -05:00
|
|
|
|
foreach(var folder in folders)
|
|
|
|
|
|
{
|
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
|
2016-05-21 17:16:22 -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);
|
|
|
|
|
|
|
2016-05-21 17:16:22 -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
|
|
|
|
|
|
var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;
|
|
|
|
|
|
if(userId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _pushService.PushSyncCiphersAsync(userId.Value);
|
|
|
|
|
|
}
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
2017-03-24 09:27:15 -04:00
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
if(!cipher.OrganizationId.HasValue && cipher.UserId.HasValue && cipher.UserId.Value == userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-24 16:15:50 -04:00
|
|
|
|
return await _subvaultUserRepository.GetCanEditByUserIdCipherIdAsync(userId, cipher.Id);
|
|
|
|
|
|
}
|
2015-12-26 23:09:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|