mirror of
https://github.com/bitwarden/server.git
synced 2025-05-20 19:14:32 -05:00
Getting rid of CipherDataModel in favor of more specific models. Optimizations to model transformations.
This commit is contained in:
parent
89e524e1e4
commit
ed0c6ad795
@ -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; }
|
|
||||||
}
|
|
||||||
}
|
|
35
src/Api/Models/FolderDataModel.cs
Normal file
35
src/Api/Models/FolderDataModel.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using Bit.Api.Utilities;
|
using Bit.Api.Utilities;
|
||||||
using Bit.Core.Domains;
|
using Bit.Core.Domains;
|
||||||
@ -8,7 +7,7 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Bit.Api.Models
|
namespace Bit.Api.Models
|
||||||
{
|
{
|
||||||
public class CipherRequestModel : IValidatableObject
|
public class CipherRequestModel
|
||||||
{
|
{
|
||||||
public CipherType Type { get; set; }
|
public CipherType Type { get; set; }
|
||||||
|
|
||||||
@ -36,29 +35,27 @@ namespace Bit.Api.Models
|
|||||||
|
|
||||||
public virtual Cipher ToCipher(string userId = null)
|
public virtual Cipher ToCipher(string userId = null)
|
||||||
{
|
{
|
||||||
return new Cipher
|
var cipher = new Cipher
|
||||||
{
|
{
|
||||||
Id = new Guid(Id),
|
Id = new Guid(Id),
|
||||||
UserId = new Guid(userId),
|
UserId = new Guid(userId),
|
||||||
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId),
|
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId),
|
||||||
Type = Type,
|
Type = Type
|
||||||
Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
switch(Type)
|
||||||
|
{
|
||||||
|
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) + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
return cipher;
|
||||||
{
|
|
||||||
if(Type == CipherType.Site)
|
|
||||||
{
|
|
||||||
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" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,17 +15,15 @@ namespace Bit.Api.Models
|
|||||||
|
|
||||||
public Cipher ToCipher(string userId = null)
|
public Cipher ToCipher(string userId = null)
|
||||||
{
|
{
|
||||||
return new Cipher
|
return ToCipher(new Cipher
|
||||||
{
|
{
|
||||||
UserId = new Guid(userId),
|
UserId = new Guid(userId)
|
||||||
Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
|
});
|
||||||
Type = Core.Enums.CipherType.Folder
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cipher ToCipher(Cipher existingFolder)
|
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;
|
existingFolder.Type = Core.Enums.CipherType.Folder;
|
||||||
|
|
||||||
return existingFolder;
|
return existingFolder;
|
||||||
|
@ -31,19 +31,16 @@ namespace Bit.Api.Models
|
|||||||
|
|
||||||
public Cipher ToCipher(string userId = null)
|
public Cipher ToCipher(string userId = null)
|
||||||
{
|
{
|
||||||
return new Cipher
|
return ToCipher(new Cipher
|
||||||
{
|
{
|
||||||
UserId = new Guid(userId),
|
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
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cipher ToCipher(Cipher existingSite)
|
public Cipher ToCipher(Cipher existingSite)
|
||||||
{
|
{
|
||||||
existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
|
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;
|
existingSite.Type = Core.Enums.CipherType.Site;
|
||||||
|
|
||||||
return existingSite;
|
return existingSite;
|
||||||
|
@ -18,12 +18,24 @@ namespace Bit.Api.Models
|
|||||||
Type = cipher.Type;
|
Type = cipher.Type;
|
||||||
Data = cipher.Data;
|
Data = cipher.Data;
|
||||||
RevisionDate = cipher.RevisionDate;
|
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 Id { get; set; }
|
||||||
public string FolderId { get; set; }
|
public string FolderId { get; set; }
|
||||||
public Core.Enums.CipherType Type { get; set; }
|
public Core.Enums.CipherType Type { get; set; }
|
||||||
public string Data { get; set; }
|
public dynamic Data { get; set; }
|
||||||
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
public DateTime RevisionDate { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using Bit.Core.Domains;
|
using Bit.Core.Domains;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Bit.Api.Models
|
namespace Bit.Api.Models
|
||||||
{
|
{
|
||||||
@ -19,7 +18,7 @@ namespace Bit.Api.Models
|
|||||||
throw new ArgumentException(nameof(cipher.Type));
|
throw new ArgumentException(nameof(cipher.Type));
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = JsonConvert.DeserializeObject<CipherDataModel>(cipher.Data);
|
var data = new FolderDataModel(cipher);
|
||||||
|
|
||||||
Id = cipher.Id.ToString();
|
Id = cipher.Id.ToString();
|
||||||
Name = data.Name;
|
Name = data.Name;
|
||||||
|
@ -19,7 +19,7 @@ namespace Bit.Api.Models
|
|||||||
throw new ArgumentException(nameof(cipher.Type));
|
throw new ArgumentException(nameof(cipher.Type));
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = JsonConvert.DeserializeObject<CipherDataModel>(cipher.Data);
|
var data = new SiteDataModel(cipher);
|
||||||
|
|
||||||
Id = cipher.Id.ToString();
|
Id = cipher.Id.ToString();
|
||||||
FolderId = cipher.FolderId?.ToString();
|
FolderId = cipher.FolderId?.ToString();
|
||||||
|
51
src/Api/Models/SiteDataModel.cs
Normal file
51
src/Api/Models/SiteDataModel.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user