From ed0c6ad7957b68e3da141f679e5da29aed72604c Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 8 Jun 2016 22:00:31 -0400 Subject: [PATCH] Getting rid of CipherDataModel in favor of more specific models. Optimizations to model transformations. --- src/Api/Models/CipherDataModel.cs | 36 ------------- src/Api/Models/FolderDataModel.cs | 35 +++++++++++++ src/Api/Models/Request/CipherRequestModel.cs | 31 +++++------ src/Api/Models/Request/FolderRequestModel.cs | 10 ++-- src/Api/Models/Request/SiteRequestModel.cs | 11 ++-- .../Models/Response/CipherResponseModel.cs | 16 +++++- .../Models/Response/FolderResponseModel.cs | 3 +- src/Api/Models/Response/SiteResponseModel.cs | 2 +- src/Api/Models/SiteDataModel.cs | 51 +++++++++++++++++++ 9 files changed, 124 insertions(+), 71 deletions(-) delete mode 100644 src/Api/Models/CipherDataModel.cs create mode 100644 src/Api/Models/FolderDataModel.cs create mode 100644 src/Api/Models/SiteDataModel.cs diff --git a/src/Api/Models/CipherDataModel.cs b/src/Api/Models/CipherDataModel.cs deleted file mode 100644 index b8ed7a4ad..000000000 --- a/src/Api/Models/CipherDataModel.cs +++ /dev/null @@ -1,36 +0,0 @@ -namespace Bit.Api.Models -{ - public class CipherDataModel - { - public CipherDataModel() { } - - public CipherDataModel(CipherRequestModel cipher) - { - Name = cipher.Name; - Uri = cipher.Uri; - Username = cipher.Username; - Password = cipher.Password; - Notes = cipher.Notes; - } - - public CipherDataModel(SiteRequestModel site) - { - Name = site.Name; - Uri = site.Uri; - Username = site.Username; - Password = site.Password; - Notes = site.Notes; - } - - public CipherDataModel(FolderRequestModel folder) - { - Name = folder.Name; - } - - public string Name { get; set; } - public string Uri { get; set; } - public string Username { get; set; } - public string Password { get; set; } - public string Notes { get; set; } - } -} diff --git a/src/Api/Models/FolderDataModel.cs b/src/Api/Models/FolderDataModel.cs new file mode 100644 index 000000000..429bf22eb --- /dev/null +++ b/src/Api/Models/FolderDataModel.cs @@ -0,0 +1,35 @@ +using System; +using Bit.Core.Domains; +using Newtonsoft.Json; + +namespace Bit.Api.Models +{ + public class FolderDataModel + { + public FolderDataModel() { } + + public FolderDataModel(FolderRequestModel folder) + { + Name = folder.Name; + } + + public FolderDataModel(CipherRequestModel cipher) + { + Name = cipher.Name; + } + + public FolderDataModel(Cipher cipher) + { + if(cipher.Type != Core.Enums.CipherType.Folder) + { + throw new ArgumentException("Cipher is not correct type."); + } + + var data = JsonConvert.DeserializeObject(cipher.Data); + + Name = data.Name; + } + + public string Name { get; set; } + } +} diff --git a/src/Api/Models/Request/CipherRequestModel.cs b/src/Api/Models/Request/CipherRequestModel.cs index c060a82da..219b10c1c 100644 --- a/src/Api/Models/Request/CipherRequestModel.cs +++ b/src/Api/Models/Request/CipherRequestModel.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Bit.Api.Utilities; using Bit.Core.Domains; @@ -8,7 +7,7 @@ using Newtonsoft.Json; namespace Bit.Api.Models { - public class CipherRequestModel : IValidatableObject + public class CipherRequestModel { public CipherType Type { get; set; } @@ -36,29 +35,27 @@ namespace Bit.Api.Models public virtual Cipher ToCipher(string userId = null) { - return new Cipher + var cipher = new Cipher { Id = new Guid(Id), UserId = new Guid(userId), FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId), - Type = Type, - Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }) + Type = Type }; - } - public IEnumerable Validate(ValidationContext validationContext) - { - if(Type == CipherType.Site) + switch(Type) { - if(string.IsNullOrWhiteSpace(Uri)) - { - yield return new ValidationResult("Uri is required for a site cypher.", new[] { "Uri" }); - } - if(string.IsNullOrWhiteSpace(Password)) - { - yield return new ValidationResult("Password is required for a site cypher.", new[] { "Password" }); - } + 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 }); + break; + default: + throw new ArgumentException("Unsupported " + nameof(Type) + "."); } + + return cipher; } } } diff --git a/src/Api/Models/Request/FolderRequestModel.cs b/src/Api/Models/Request/FolderRequestModel.cs index f543e3122..08db98e02 100644 --- a/src/Api/Models/Request/FolderRequestModel.cs +++ b/src/Api/Models/Request/FolderRequestModel.cs @@ -15,17 +15,15 @@ namespace Bit.Api.Models public Cipher ToCipher(string userId = null) { - return new Cipher + return ToCipher(new Cipher { - UserId = new Guid(userId), - Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), - Type = Core.Enums.CipherType.Folder - }; + UserId = new Guid(userId) + }); } public Cipher ToCipher(Cipher existingFolder) { - existingFolder.Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + existingFolder.Data = JsonConvert.SerializeObject(new FolderDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); existingFolder.Type = Core.Enums.CipherType.Folder; return existingFolder; diff --git a/src/Api/Models/Request/SiteRequestModel.cs b/src/Api/Models/Request/SiteRequestModel.cs index bce718245..a9c6ebdf0 100644 --- a/src/Api/Models/Request/SiteRequestModel.cs +++ b/src/Api/Models/Request/SiteRequestModel.cs @@ -31,19 +31,16 @@ namespace Bit.Api.Models public Cipher ToCipher(string userId = null) { - return new Cipher + return ToCipher(new Cipher { - UserId = new Guid(userId), - FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId), - Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), - Type = Core.Enums.CipherType.Site - }; + UserId = new Guid(userId) + }); } public Cipher ToCipher(Cipher existingSite) { existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId); - existingSite.Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + existingSite.Data = JsonConvert.SerializeObject(new SiteDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); existingSite.Type = Core.Enums.CipherType.Site; return existingSite; diff --git a/src/Api/Models/Response/CipherResponseModel.cs b/src/Api/Models/Response/CipherResponseModel.cs index 369651286..8b713695b 100644 --- a/src/Api/Models/Response/CipherResponseModel.cs +++ b/src/Api/Models/Response/CipherResponseModel.cs @@ -18,12 +18,24 @@ namespace Bit.Api.Models Type = cipher.Type; Data = cipher.Data; RevisionDate = cipher.RevisionDate; + + switch(cipher.Type) + { + case Core.Enums.CipherType.Folder: + Data = new FolderDataModel(cipher); + break; + case Core.Enums.CipherType.Site: + Data = new SiteDataModel(cipher); + break; + default: + throw new ArgumentException("Unsupported " + nameof(Type) + "."); + } } public string Id { get; set; } public string FolderId { get; set; } public Core.Enums.CipherType Type { get; set; } - public string Data { get; set; } - public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow; + public dynamic Data { get; set; } + public DateTime RevisionDate { get; set; } } } diff --git a/src/Api/Models/Response/FolderResponseModel.cs b/src/Api/Models/Response/FolderResponseModel.cs index 6a0434d5e..dbcc9ade9 100644 --- a/src/Api/Models/Response/FolderResponseModel.cs +++ b/src/Api/Models/Response/FolderResponseModel.cs @@ -1,6 +1,5 @@ using System; using Bit.Core.Domains; -using Newtonsoft.Json; namespace Bit.Api.Models { @@ -19,7 +18,7 @@ namespace Bit.Api.Models throw new ArgumentException(nameof(cipher.Type)); } - var data = JsonConvert.DeserializeObject(cipher.Data); + var data = new FolderDataModel(cipher); Id = cipher.Id.ToString(); Name = data.Name; diff --git a/src/Api/Models/Response/SiteResponseModel.cs b/src/Api/Models/Response/SiteResponseModel.cs index 034ea8c7b..c186b55bc 100644 --- a/src/Api/Models/Response/SiteResponseModel.cs +++ b/src/Api/Models/Response/SiteResponseModel.cs @@ -19,7 +19,7 @@ namespace Bit.Api.Models throw new ArgumentException(nameof(cipher.Type)); } - var data = JsonConvert.DeserializeObject(cipher.Data); + var data = new SiteDataModel(cipher); Id = cipher.Id.ToString(); FolderId = cipher.FolderId?.ToString(); diff --git a/src/Api/Models/SiteDataModel.cs b/src/Api/Models/SiteDataModel.cs new file mode 100644 index 000000000..0c23f504c --- /dev/null +++ b/src/Api/Models/SiteDataModel.cs @@ -0,0 +1,51 @@ +using System; +using Bit.Core.Domains; +using Newtonsoft.Json; + +namespace Bit.Api.Models +{ + public class SiteDataModel + { + public SiteDataModel() { } + + public SiteDataModel(SiteRequestModel site) + { + Name = site.Name; + Uri = site.Uri; + Username = site.Username; + Password = site.Password; + Notes = site.Notes; + } + + public SiteDataModel(CipherRequestModel cipher) + { + Name = cipher.Name; + Uri = cipher.Uri; + Username = cipher.Username; + Password = cipher.Password; + Notes = cipher.Notes; + } + + public SiteDataModel(Cipher cipher) + { + if(cipher.Type != Core.Enums.CipherType.Site) + { + throw new ArgumentException("Cipher is not correct type."); + } + + var data = JsonConvert.DeserializeObject(cipher.Data); + + Name = data.Name; + Uri = data.Uri; + Username = data.Username; + Password = data.Password; + Notes = data.Notes; + } + + public string Name { get; set; } + public string Uri { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string Notes { get; set; } + } +}