1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

Refactor naming: Sites => Logins

This commit is contained in:
Kyle Spearrin 2017-01-02 21:52:13 -05:00
parent 7e56797847
commit 9cde513026
11 changed files with 86 additions and 68 deletions

View File

@ -62,7 +62,7 @@ namespace Bit.Api.Controllers
public async Task PostImport([FromBody]ImportRequestModel model) public async Task PostImport([FromBody]ImportRequestModel model)
{ {
var folderCiphers = model.Folders.Select(f => f.ToCipher(_userManager.GetUserId(User))).ToList(); var folderCiphers = model.Folders.Select(f => f.ToCipher(_userManager.GetUserId(User))).ToList();
var otherCiphers = model.Sites.Select(s => s.ToCipher(_userManager.GetUserId(User))).ToList(); var otherCiphers = model.Logins.Select(s => s.ToCipher(_userManager.GetUserId(User))).ToList();
await _cipherService.ImportCiphersAsync( await _cipherService.ImportCiphersAsync(
folderCiphers, folderCiphers,

View File

@ -13,15 +13,17 @@ using Bit.Core.Services;
namespace Bit.Api.Controllers namespace Bit.Api.Controllers
{ {
[Route("logins")]
// sites route is deprecated
[Route("sites")] [Route("sites")]
[Authorize("Application")] [Authorize("Application")]
public class SitesController : Controller public class LoginsController : Controller
{ {
private readonly ICipherRepository _cipherRepository; private readonly ICipherRepository _cipherRepository;
private readonly ICipherService _cipherService; private readonly ICipherService _cipherService;
private readonly UserManager<User> _userManager; private readonly UserManager<User> _userManager;
public SitesController( public LoginsController(
ICipherRepository cipherRepository, ICipherRepository cipherRepository,
ICipherService cipherService, ICipherService cipherService,
UserManager<User> userManager) UserManager<User> userManager)
@ -32,53 +34,54 @@ namespace Bit.Api.Controllers
} }
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<SiteResponseModel> Get(string id, string[] expand = null) public async Task<LoginResponseModel> Get(string id, string[] expand = null)
{ {
var site = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User))); var login = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User)));
if(site == null || site.Type != Core.Enums.CipherType.Site) if(login == null || login.Type != Core.Enums.CipherType.Login)
{ {
throw new NotFoundException(); throw new NotFoundException();
} }
var response = new SiteResponseModel(site); var response = new LoginResponseModel(login);
await ExpandAsync(site, response, expand, null); await ExpandAsync(login, response, expand, null);
return response; return response;
} }
[HttpGet("")] [HttpGet("")]
public async Task<ListResponseModel<SiteResponseModel>> Get(string[] expand = null) public async Task<ListResponseModel<LoginResponseModel>> Get(string[] expand = null)
{ {
ICollection<Cipher> sites = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Site, new Guid(_userManager.GetUserId(User))); ICollection<Cipher> logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
var responses = sites.Select(s => new SiteResponseModel(s)).ToList(); new Guid(_userManager.GetUserId(User)));
await ExpandManyAsync(sites, responses, expand, null); var responses = logins.Select(s => new LoginResponseModel(s)).ToList();
return new ListResponseModel<SiteResponseModel>(responses); await ExpandManyAsync(logins, responses, expand, null);
return new ListResponseModel<LoginResponseModel>(responses);
} }
[HttpPost("")] [HttpPost("")]
public async Task<SiteResponseModel> Post([FromBody]SiteRequestModel model, string[] expand = null) public async Task<LoginResponseModel> Post([FromBody]LoginRequestModel model, string[] expand = null)
{ {
var site = model.ToCipher(_userManager.GetUserId(User)); var login = model.ToCipher(_userManager.GetUserId(User));
await _cipherService.SaveAsync(site); await _cipherService.SaveAsync(login);
var response = new SiteResponseModel(site); var response = new LoginResponseModel(login);
await ExpandAsync(site, response, expand, null); await ExpandAsync(login, response, expand, null);
return response; return response;
} }
[HttpPut("{id}")] [HttpPut("{id}")]
[HttpPost("{id}")] [HttpPost("{id}")]
public async Task<SiteResponseModel> Put(string id, [FromBody]SiteRequestModel model, string[] expand = null) public async Task<LoginResponseModel> Put(string id, [FromBody]LoginRequestModel model, string[] expand = null)
{ {
var site = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User))); var login = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User)));
if(site == null || site.Type != Core.Enums.CipherType.Site) if(login == null || login.Type != Core.Enums.CipherType.Login)
{ {
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherService.SaveAsync(model.ToCipher(site)); await _cipherService.SaveAsync(model.ToCipher(login));
var response = new SiteResponseModel(site); var response = new LoginResponseModel(login);
await ExpandAsync(site, response, expand, null); await ExpandAsync(login, response, expand, null);
return response; return response;
} }
@ -86,34 +89,35 @@ namespace Bit.Api.Controllers
[HttpPost("{id}/delete")] [HttpPost("{id}/delete")]
public async Task Delete(string id) public async Task Delete(string id)
{ {
var site = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User))); var login = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User)));
if(site == null || site.Type != Core.Enums.CipherType.Site) if(login == null || login.Type != Core.Enums.CipherType.Login)
{ {
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherService.DeleteAsync(site); await _cipherService.DeleteAsync(login);
} }
private async Task ExpandAsync(Cipher site, SiteResponseModel response, string[] expand, Cipher folder) private async Task ExpandAsync(Cipher login, LoginResponseModel response, string[] expand, Cipher folder)
{ {
if(expand == null || expand.Count() == 0) if(expand == null || expand.Count() == 0)
{ {
return; return;
} }
if(expand.Any(e => e.ToLower() == "folder") && site.FolderId.HasValue) if(expand.Any(e => e.ToLower() == "folder") && login.FolderId.HasValue)
{ {
if(folder == null) if(folder == null)
{ {
folder = await _cipherRepository.GetByIdAsync(site.FolderId.Value); folder = await _cipherRepository.GetByIdAsync(login.FolderId.Value);
} }
response.Folder = new FolderResponseModel(folder); response.Folder = new FolderResponseModel(folder);
} }
} }
private async Task ExpandManyAsync(IEnumerable<Cipher> sites, ICollection<SiteResponseModel> responses, string[] expand, IEnumerable<Cipher> folders) private async Task ExpandManyAsync(IEnumerable<Cipher> logins, ICollection<LoginResponseModel> responses,
string[] expand, IEnumerable<Cipher> folders)
{ {
if(expand == null || expand.Count() == 0) if(expand == null || expand.Count() == 0)
{ {
@ -124,20 +128,21 @@ namespace Bit.Api.Controllers
{ {
if(folders == null) if(folders == null)
{ {
folders = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Folder, new Guid(_userManager.GetUserId(User))); folders = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Folder,
new Guid(_userManager.GetUserId(User)));
} }
if(folders != null && folders.Count() > 0) if(folders != null && folders.Count() > 0)
{ {
foreach(var response in responses) foreach(var response in responses)
{ {
var site = sites.SingleOrDefault(s => s.Id.ToString() == response.Id); var login = logins.SingleOrDefault(s => s.Id.ToString() == response.Id);
if(site == null) if(login == null)
{ {
continue; continue;
} }
var folder = folders.SingleOrDefault(f => f.Id == site.FolderId); var folder = folders.SingleOrDefault(f => f.Id == login.FolderId);
if(folder == null) if(folder == null)
{ {
continue; continue;

View File

@ -4,20 +4,20 @@ using Newtonsoft.Json;
namespace Bit.Api.Models namespace Bit.Api.Models
{ {
public class SiteDataModel public class LoginDataModel
{ {
public SiteDataModel() { } public LoginDataModel() { }
public SiteDataModel(SiteRequestModel site) public LoginDataModel(LoginRequestModel login)
{ {
Name = site.Name; Name = login.Name;
Uri = site.Uri; Uri = login.Uri;
Username = site.Username; Username = login.Username;
Password = site.Password; Password = login.Password;
Notes = site.Notes; Notes = login.Notes;
} }
public SiteDataModel(CipherRequestModel cipher) public LoginDataModel(CipherRequestModel cipher)
{ {
Name = cipher.Name; Name = cipher.Name;
Uri = cipher.Uri; Uri = cipher.Uri;
@ -26,14 +26,14 @@ namespace Bit.Api.Models
Notes = cipher.Notes; Notes = cipher.Notes;
} }
public SiteDataModel(Cipher cipher) public LoginDataModel(Cipher cipher)
{ {
if(cipher.Type != Core.Enums.CipherType.Site) if(cipher.Type != Core.Enums.CipherType.Login)
{ {
throw new ArgumentException("Cipher is not correct type."); throw new ArgumentException("Cipher is not correct type.");
} }
var data = JsonConvert.DeserializeObject<SiteDataModel>(cipher.Data); var data = JsonConvert.DeserializeObject<LoginDataModel>(cipher.Data);
Name = data.Name; Name = data.Name;
Uri = data.Uri; Uri = data.Uri;

View File

@ -5,8 +5,20 @@ namespace Bit.Api.Models
{ {
public class ImportRequestModel public class ImportRequestModel
{ {
private LoginRequestModel[] _logins;
public FolderRequestModel[] Folders { get; set; } public FolderRequestModel[] Folders { get; set; }
public SiteRequestModel[] Sites { get; set; } [Obsolete]
public LoginRequestModel[] Sites
{
get { return _logins; }
set { _logins = value; }
}
public LoginRequestModel[] Logins
{
get { return _logins; }
set { _logins = value; }
}
public KeyValuePair<int, int>[] FolderRelationships { get; set; } public KeyValuePair<int, int>[] FolderRelationships { get; set; }
} }
} }

View File

@ -48,8 +48,8 @@ namespace Bit.Api.Models
case CipherType.Folder: case CipherType.Folder:
cipher.Data = JsonConvert.SerializeObject(new FolderDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); cipher.Data = JsonConvert.SerializeObject(new FolderDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break; break;
case CipherType.Site: case CipherType.Login:
cipher.Data = JsonConvert.SerializeObject(new SiteDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); cipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break; break;
default: default:
throw new ArgumentException("Unsupported " + nameof(Type) + "."); throw new ArgumentException("Unsupported " + nameof(Type) + ".");

View File

@ -6,7 +6,7 @@ using Newtonsoft.Json;
namespace Bit.Api.Models namespace Bit.Api.Models
{ {
public class SiteRequestModel public class LoginRequestModel
{ {
[StringLength(36)] [StringLength(36)]
public string FolderId { get; set; } public string FolderId { get; set; }
@ -36,14 +36,15 @@ namespace Bit.Api.Models
}); });
} }
public Cipher ToCipher(Cipher existingSite) public Cipher ToCipher(Cipher existingLogin)
{ {
existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId); existingLogin.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
existingSite.Favorite = Favorite; existingLogin.Favorite = Favorite;
existingSite.Data = JsonConvert.SerializeObject(new SiteDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); existingLogin.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
existingSite.Type = Core.Enums.CipherType.Site; new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
existingLogin.Type = Core.Enums.CipherType.Login;
return existingSite; return existingLogin;
} }
} }
} }

View File

@ -24,8 +24,8 @@ namespace Bit.Api.Models
case Core.Enums.CipherType.Folder: case Core.Enums.CipherType.Folder:
Data = new FolderDataModel(cipher); Data = new FolderDataModel(cipher);
break; break;
case Core.Enums.CipherType.Site: case Core.Enums.CipherType.Login:
Data = new SiteDataModel(cipher); Data = new LoginDataModel(cipher);
break; break;
default: default:
throw new ArgumentException("Unsupported " + nameof(Type) + "."); throw new ArgumentException("Unsupported " + nameof(Type) + ".");

View File

@ -3,22 +3,22 @@ using Bit.Core.Domains;
namespace Bit.Api.Models namespace Bit.Api.Models
{ {
public class SiteResponseModel : ResponseModel public class LoginResponseModel : ResponseModel
{ {
public SiteResponseModel(Cipher cipher) public LoginResponseModel(Cipher cipher)
: base("site") : base("login")
{ {
if(cipher == null) if(cipher == null)
{ {
throw new ArgumentNullException(nameof(cipher)); throw new ArgumentNullException(nameof(cipher));
} }
if(cipher.Type != Core.Enums.CipherType.Site) if(cipher.Type != Core.Enums.CipherType.Login)
{ {
throw new ArgumentException(nameof(cipher.Type)); throw new ArgumentException(nameof(cipher.Type));
} }
var data = new SiteDataModel(cipher); var data = new LoginDataModel(cipher);
Id = cipher.Id.ToString(); Id = cipher.Id.ToString();
FolderId = cipher.FolderId?.ToString(); FolderId = cipher.FolderId?.ToString();

View File

@ -3,6 +3,6 @@
public enum CipherType : short public enum CipherType : short
{ {
Folder = 0, Folder = 0,
Site = 1 Login = 1
} }
} }

View File

@ -4,7 +4,7 @@
{ {
SyncCipherUpdate = 0, SyncCipherUpdate = 0,
SyncCipherCreate = 1, SyncCipherCreate = 1,
SyncSiteDelete = 2, SyncLoginDelete = 2,
SyncFolderDelete = 3, SyncFolderDelete = 3,
SyncCiphers = 4 SyncCiphers = 4
} }

View File

@ -58,8 +58,8 @@ namespace Bit.Core.Services
case CipherType.Folder: case CipherType.Folder:
await PushCipherAsync(cipher, PushType.SyncFolderDelete); await PushCipherAsync(cipher, PushType.SyncFolderDelete);
break; break;
case CipherType.Site: case CipherType.Login:
await PushCipherAsync(cipher, PushType.SyncSiteDelete); await PushCipherAsync(cipher, PushType.SyncLoginDelete);
break; break;
default: default:
break; break;