1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

Getting rid of CipherDataModel in favor of more specific models. Optimizations to model transformations.

This commit is contained in:
Kyle Spearrin 2016-06-08 22:00:31 -04:00
parent 89e524e1e4
commit ed0c6ad795
9 changed files with 124 additions and 71 deletions

View File

@ -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; }
}
}

View File

@ -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<FolderDataModel>(cipher.Data);
Name = data.Name;
}
public string Name { get; set; }
}
}

View File

@ -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<ValidationResult> 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;
}
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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; }
}
}

View File

@ -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<CipherDataModel>(cipher.Data);
var data = new FolderDataModel(cipher);
Id = cipher.Id.ToString();
Name = data.Name;

View File

@ -19,7 +19,7 @@ namespace Bit.Api.Models
throw new ArgumentException(nameof(cipher.Type));
}
var data = JsonConvert.DeserializeObject<CipherDataModel>(cipher.Data);
var data = new SiteDataModel(cipher);
Id = cipher.Id.ToString();
FolderId = cipher.FolderId?.ToString();

View File

@ -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<SiteDataModel>(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; }
}
}