1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +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)
{
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(
folderCiphers,

View File

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

View File

@ -4,20 +4,20 @@ using Newtonsoft.Json;
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;
Uri = site.Uri;
Username = site.Username;
Password = site.Password;
Notes = site.Notes;
Name = login.Name;
Uri = login.Uri;
Username = login.Username;
Password = login.Password;
Notes = login.Notes;
}
public SiteDataModel(CipherRequestModel cipher)
public LoginDataModel(CipherRequestModel cipher)
{
Name = cipher.Name;
Uri = cipher.Uri;
@ -26,14 +26,14 @@ namespace Bit.Api.Models
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.");
}
var data = JsonConvert.DeserializeObject<SiteDataModel>(cipher.Data);
var data = JsonConvert.DeserializeObject<LoginDataModel>(cipher.Data);
Name = data.Name;
Uri = data.Uri;

View File

@ -5,8 +5,20 @@ namespace Bit.Api.Models
{
public class ImportRequestModel
{
private LoginRequestModel[] _logins;
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; }
}
}

View File

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

View File

@ -6,7 +6,7 @@ using Newtonsoft.Json;
namespace Bit.Api.Models
{
public class SiteRequestModel
public class LoginRequestModel
{
[StringLength(36)]
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);
existingSite.Favorite = Favorite;
existingSite.Data = JsonConvert.SerializeObject(new SiteDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
existingSite.Type = Core.Enums.CipherType.Site;
existingLogin.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
existingLogin.Favorite = Favorite;
existingLogin.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
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:
Data = new FolderDataModel(cipher);
break;
case Core.Enums.CipherType.Site:
Data = new SiteDataModel(cipher);
case Core.Enums.CipherType.Login:
Data = new LoginDataModel(cipher);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");

View File

@ -3,22 +3,22 @@ using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class SiteResponseModel : ResponseModel
public class LoginResponseModel : ResponseModel
{
public SiteResponseModel(Cipher cipher)
: base("site")
public LoginResponseModel(Cipher cipher)
: base("login")
{
if(cipher == null)
{
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));
}
var data = new SiteDataModel(cipher);
var data = new LoginDataModel(cipher);
Id = cipher.Id.ToString();
FolderId = cipher.FolderId?.ToString();

View File

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

View File

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

View File

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