mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
Added cipher service with bulk import to account controller
This commit is contained in:
54
src/Core/Services/CipherService.cs
Normal file
54
src/Core/Services/CipherService.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Domains;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class CipherService : ICipherService
|
||||
{
|
||||
private readonly IFolderRepository _folderRepository;
|
||||
private readonly ICipherRepository _cipherRepository;
|
||||
|
||||
public CipherService(
|
||||
IFolderRepository folderRepository,
|
||||
ICipherRepository cipherRepository)
|
||||
{
|
||||
_folderRepository = folderRepository;
|
||||
_cipherRepository = cipherRepository;
|
||||
}
|
||||
|
||||
public async Task ImportCiphersAsync(
|
||||
List<Folder> folders,
|
||||
List<Site> sites,
|
||||
IEnumerable<KeyValuePair<int, int>> siteRelationships)
|
||||
{
|
||||
// create all the folders
|
||||
var folderTasks = new List<Task>();
|
||||
foreach(var folder in folders)
|
||||
{
|
||||
folderTasks.Add(_folderRepository.CreateAsync(folder));
|
||||
}
|
||||
await Task.WhenAll(folderTasks);
|
||||
|
||||
// associate the newly created folders to the sites
|
||||
foreach(var relationship in siteRelationships)
|
||||
{
|
||||
var site = sites.ElementAtOrDefault(relationship.Key);
|
||||
var folder = folders.ElementAtOrDefault(relationship.Value);
|
||||
|
||||
if(site == null || folder == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
site.FolderId = folder.Id;
|
||||
}
|
||||
|
||||
// create all the sites
|
||||
await _cipherRepository.CreateAsync(sites);
|
||||
}
|
||||
}
|
||||
}
|
11
src/Core/Services/ICipherService.cs
Normal file
11
src/Core/Services/ICipherService.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Domains;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public interface ICipherService
|
||||
{
|
||||
Task ImportCiphersAsync(List<Folder> folders, List<Site> sites, IEnumerable<KeyValuePair<int, int>> siteRelationships);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user