1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 23:52:50 -05:00

Added cipher service with bulk import to account controller

This commit is contained in:
Kyle Spearrin
2015-12-26 23:09:53 -05:00
parent 437b971003
commit 8d7178bc74
14 changed files with 246 additions and 29 deletions

View File

@ -11,6 +11,7 @@ using Bit.Core.Domains;
using Bit.Core.Enums;
using Bit.Core;
using System.Security.Claims;
using System.Linq;
namespace Bit.Api.Controllers
{
@ -19,16 +20,18 @@ namespace Bit.Api.Controllers
public class AccountsController : Controller
{
private readonly IUserService _userService;
private readonly ICipherService _cipherService;
private readonly UserManager<User> _userManager;
private readonly CurrentContext _currentContext;
public AccountsController(
IDataProtectionProvider dataProtectionProvider,
IUserService userService,
ICipherService cipherService,
UserManager<User> userManager,
CurrentContext currentContext)
{
_userService = userService;
_cipherService = cipherService;
_userManager = userManager;
_currentContext = currentContext;
}
@ -206,5 +209,14 @@ namespace Bit.Api.Controllers
var response = new TwoFactorResponseModel(user);
return response;
}
[HttpPost("import")]
public async Task PostImport([FromBody]ImportRequestModel model)
{
await _cipherService.ImportCiphersAsync(
model.Folders.Select(f => f.ToFolder(User.GetUserId())).ToList(),
model.Sites.Select(s => s.ToSite(User.GetUserId())).ToList(),
model.SiteRelationships);
}
}
}

View File

@ -144,5 +144,4 @@ namespace Bit.Api.Controllers
}
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
namespace Bit.Api.Models
{
public class ImportRequestModel
{
public FolderRequestModel[] Folders { get; set; }
public SiteRequestModel[] Sites { get; set; }
public KeyValuePair<int, int>[] SiteRelationships { get; set; }
}
}

View File

@ -71,10 +71,6 @@ namespace Bit.Api.Models
{
yield return new ValidationResult("Uri is required for a site cypher.", new[] { "Uri" });
}
if(string.IsNullOrWhiteSpace(Username))
{
yield return new ValidationResult("Username is required for a site cypher.", new[] { "Username" });
}
if(string.IsNullOrWhiteSpace(Password))
{
yield return new ValidationResult("Password is required for a site cypher.", new[] { "Password" });

View File

@ -14,7 +14,6 @@ namespace Bit.Api.Models
[Required]
[EncryptedString]
public string Uri { get; set; }
[Required]
[EncryptedString]
public string Username { get; set; }
[Required]
@ -31,7 +30,7 @@ namespace Bit.Api.Models
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId,
Name = Name,
Uri = Uri,
Username = Username,
Username = string.IsNullOrWhiteSpace(Username) ? null : Username,
Password = Password,
Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes
};
@ -42,7 +41,7 @@ namespace Bit.Api.Models
existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId;
existingSite.Name = Name;
existingSite.Uri = Uri;
existingSite.Username = Username;
existingSite.Username = string.IsNullOrWhiteSpace(Username) ? null : Username;
existingSite.Password = Password;
existingSite.Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes;

View File

@ -53,7 +53,7 @@ namespace Bit.Api
services.AddSingleton(s => globalSettings);
// Repositories
var documentDBClient = DocumentClientHelpers.InitClient(globalSettings.DocumentDB);
var documentDBClient = DocumentDBHelpers.InitClient(globalSettings.DocumentDB);
services.AddSingleton<IUserRepository>(s => new Repos.UserRepository(documentDBClient, globalSettings.DocumentDB.DatabaseId));
services.AddSingleton<ISiteRepository>(s => new Repos.SiteRepository(documentDBClient, globalSettings.DocumentDB.DatabaseId));
services.AddSingleton<IFolderRepository>(s => new Repos.FolderRepository(documentDBClient, globalSettings.DocumentDB.DatabaseId));
@ -116,6 +116,7 @@ namespace Bit.Api
// Services
services.AddSingleton<IMailService, MailService>();
services.AddSingleton<ICipherService, CipherService>();
services.AddScoped<IUserService, UserService>();
// Cors