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

refactor for cipher details, folders, favorites

This commit is contained in:
Kyle Spearrin
2017-03-18 11:58:02 -04:00
parent 2b71420818
commit 588f6c7c2c
15 changed files with 139 additions and 138 deletions

View File

@ -1,6 +1,7 @@
using System;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
using Core.Models.Data;
namespace Bit.Core.Models.Api
{
@ -28,7 +29,23 @@ namespace Bit.Core.Models.Api
public LoginDataModel(Cipher cipher)
{
if(cipher.Type != Core.Enums.CipherType.Login)
if(cipher.Type != Enums.CipherType.Login)
{
throw new ArgumentException("Cipher is not correct type.");
}
var data = JsonConvert.DeserializeObject<LoginDataModel>(cipher.Data);
Name = data.Name;
Uri = data.Uri;
Username = data.Username;
Password = data.Password;
Notes = data.Notes;
}
public LoginDataModel(CipherDetails cipher)
{
if(cipher.Type != Enums.CipherType.Login)
{
throw new ArgumentException("Cipher is not correct type.");
}

View File

@ -39,15 +39,12 @@ namespace Bit.Core.Models.Api
{
Id = new Guid(Id),
UserId = userId,
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId),
//FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId),
Type = Type
};
switch(Type)
{
case CipherType.Folder:
cipher.Data = JsonConvert.SerializeObject(new FolderDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
case CipherType.Login:
cipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;

View File

@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
using Core.Models.Data;
namespace Bit.Core.Models.Api
{
@ -28,21 +29,22 @@ namespace Bit.Core.Models.Api
[StringLength(10000)]
public string Notes { get; set; }
public Cipher ToCipher(Guid userId)
public CipherDetails ToCipher(Guid userId)
{
return ToCipher(new Cipher
return ToCipher(new CipherDetails
{
UserId = userId
});
}
public Cipher ToCipher(Cipher existingLogin)
public CipherDetails ToCipher(CipherDetails existingLogin)
{
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;
existingLogin.Type = Enums.CipherType.Login;
return existingLogin;
}

View File

@ -20,7 +20,7 @@ namespace Bit.Core.Models.Api
throw new ArgumentNullException(nameof(deletedIds));
}
Revised = revisedCiphers.Select(c => new CipherResponseModel(c, userId));
Revised = revisedCiphers.Select(c => new CipherResponseModel(c));
Deleted = deletedIds.Select(id => id.ToString());
}

View File

@ -1,11 +1,12 @@
using System;
using Bit.Core.Models.Table;
using Core.Models.Data;
namespace Bit.Core.Models.Api
{
public class CipherResponseModel : ResponseModel
{
public CipherResponseModel(Cipher cipher, Guid userId, string obj = "cipher")
public CipherResponseModel(Cipher cipher, string obj = "cipher")
: base(obj)
{
if(cipher == null)
@ -14,17 +15,33 @@ namespace Bit.Core.Models.Api
}
Id = cipher.Id.ToString();
FolderId = cipher.FolderId?.ToString();
Type = cipher.Type;
Favorite = cipher.Favorite;
RevisionDate = cipher.RevisionDate;
switch(cipher.Type)
{
case Core.Enums.CipherType.Folder:
Data = new FolderDataModel(cipher);
case Enums.CipherType.Login:
Data = new LoginDataModel(cipher);
break;
case Core.Enums.CipherType.Login:
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
}
public CipherResponseModel(CipherDetails cipher, string obj = "cipher")
: base(obj)
{
if(cipher == null)
{
throw new ArgumentNullException(nameof(cipher));
}
Id = cipher.Id.ToString();
Type = cipher.Type;
RevisionDate = cipher.RevisionDate;
switch(cipher.Type)
{
case Enums.CipherType.Login:
Data = new LoginDataModel(cipher);
break;
default:
@ -34,7 +51,7 @@ namespace Bit.Core.Models.Api
public string Id { get; set; }
public string FolderId { get; set; }
public Core.Enums.CipherType Type { get; set; }
public Enums.CipherType Type { get; set; }
public bool Favorite { get; set; }
public dynamic Data { get; set; }
public DateTime RevisionDate { get; set; }

View File

@ -1,11 +1,12 @@
using System;
using Core.Models.Data;
using Bit.Core.Models.Table;
namespace Bit.Core.Models.Api
{
public class LoginResponseModel : ResponseModel
{
public LoginResponseModel(Cipher cipher, Guid userId, string obj = "login")
public LoginResponseModel(CipherDetails cipher, string obj = "login")
: base(obj)
{
if(cipher == null)
@ -13,7 +14,7 @@ namespace Bit.Core.Models.Api
throw new ArgumentNullException(nameof(cipher));
}
if(cipher.Type != Core.Enums.CipherType.Login)
if(cipher.Type != Enums.CipherType.Login)
{
throw new ArgumentException(nameof(cipher.Type));
}

View File

@ -1,18 +1,11 @@
using Bit.Core.Enums;
using Bit.Core.Models.Table;
using System;
namespace Core.Models.Data
{
public class CipherDetails
public class CipherDetails : Cipher
{
public Guid Id { get; set; }
public Guid? UserId { get; set; }
public Guid? OrganizationId { get; set; }
public Guid? FolderId { get; set; }
public CipherType Type { get; set; }
public bool Favorite { get; set; }
public string Data { get; set; }
public DateTime CreationDate { get; set; }
public DateTime RevisionDate { get; set; }
}
}