Files
server/src/Core/Services/Implementations/CipherService.cs

220 lines
7.7 KiB
C#
Raw Normal View History

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;
using Bit.Core.Repositories;
using Core.Models.Data;
using Bit.Core.Exceptions;
namespace Bit.Core.Services
{
public class CipherService : ICipherService
{
private readonly ICipherRepository _cipherRepository;
private readonly IFolderRepository _folderRepository;
2017-02-25 23:38:24 -05:00
private readonly IUserRepository _userRepository;
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly ISubvaultUserRepository _subvaultUserRepository;
2017-04-12 12:42:00 -04:00
private readonly ISubvaultCipherRepository _subvaultCipherRepository;
private readonly IPushService _pushService;
public CipherService(
ICipherRepository cipherRepository,
IFolderRepository folderRepository,
2017-02-25 23:38:24 -05:00
IUserRepository userRepository,
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
ISubvaultUserRepository subvaultUserRepository,
2017-04-12 12:42:00 -04:00
ISubvaultCipherRepository subvaultCipherRepository,
IPushService pushService)
{
_cipherRepository = cipherRepository;
_folderRepository = folderRepository;
2017-02-25 23:38:24 -05:00
_userRepository = userRepository;
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
_subvaultUserRepository = subvaultUserRepository;
2017-04-12 12:42:00 -04:00
_subvaultCipherRepository = subvaultCipherRepository;
_pushService = pushService;
}
public async Task SaveAsync(CipherDetails cipher, Guid savingUserId)
{
if(!(await UserCanEditAsync(cipher, savingUserId)))
{
throw new BadRequestException("You do not have permissions to edit this.");
}
cipher.UserId = savingUserId;
if(cipher.Id == default(Guid))
{
await _cipherRepository.CreateAsync(cipher);
// push
//await _pushService.PushSyncCipherCreateAsync(cipher);
}
else
{
cipher.RevisionDate = DateTime.UtcNow;
await _cipherRepository.ReplaceAsync(cipher);
// push
//await _pushService.PushSyncCipherUpdateAsync(cipher);
}
}
2017-04-19 16:00:47 -04:00
public async Task DeleteAsync(Cipher cipher, Guid deletingUserId, bool orgAdmin = false)
{
2017-04-19 16:00:47 -04:00
if(!orgAdmin && !(await UserCanEditAsync(cipher, deletingUserId)))
{
throw new BadRequestException("You do not have permissions to delete this.");
}
await _cipherRepository.DeleteAsync(cipher);
// push
//await _pushService.PushSyncCipherDeleteAsync(cipher);
}
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)
{
if(cipher.Id == default(Guid))
{
throw new BadRequestException(nameof(cipher.Id));
}
2017-04-04 22:07:30 -04:00
if(cipher.OrganizationId.HasValue)
{
2017-04-04 22:07:30 -04:00
throw new BadRequestException("Already belongs to an organization.");
}
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;
cipher.RevisionDate = DateTime.UtcNow;
2017-04-17 23:12:48 -04:00
await _cipherRepository.ReplaceAsync(cipher, subvaultIds);
// 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);
}
public async Task ImportCiphersAsync(
List<Folder> folders,
List<CipherDetails> ciphers,
IEnumerable<KeyValuePair<int, int>> folderRelationships)
{
foreach(var cipher in ciphers)
{
cipher.SetNewId();
if(cipher.UserId.HasValue && cipher.Favorite)
{
cipher.Favorites = $"{{\"{cipher.UserId.ToString().ToUpperInvariant()}\":\"true\"}}";
}
}
// Init. ids for folders
foreach(var folder in folders)
{
folder.SetNewId();
}
// Create the folder associations based on the newly created folder ids
foreach(var relationship in folderRelationships)
{
var cipher = ciphers.ElementAtOrDefault(relationship.Key);
var folder = folders.ElementAtOrDefault(relationship.Value);
if(cipher == null || folder == null)
{
continue;
}
cipher.Folders = $"{{\"{cipher.UserId.ToString().ToUpperInvariant()}\":" +
$"\"{folder.Id.ToString().ToUpperInvariant()}\"}}";
}
// Create it all
await _cipherRepository.CreateAsync(ciphers, folders);
// push
var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;
if(userId.HasValue)
{
await _pushService.PushSyncCiphersAsync(userId.Value);
}
}
2017-03-25 16:25:10 -04:00
private async Task<bool> UserCanEditAsync(Cipher cipher, Guid userId)
{
if(!cipher.OrganizationId.HasValue && cipher.UserId.HasValue && cipher.UserId.Value == userId)
{
return true;
}
return await _subvaultUserRepository.GetCanEditByUserIdCipherIdAsync(userId, cipher.Id);
}
}
}