1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

refactored data storage to use cipher table. added history table and insert triggers.

This commit is contained in:
Kyle Spearrin
2016-05-21 17:16:22 -04:00
parent 8137847485
commit 3fdb0fcf67
56 changed files with 422 additions and 646 deletions

View File

@ -0,0 +1,37 @@
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

@ -3,8 +3,8 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
using System.Linq;
using Bit.Core.Enums;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
@ -34,42 +34,18 @@ namespace Bit.Api.Models
[StringLength(5000)]
public string Notes { get; set; }
public virtual Site ToSite(string userId = null)
public virtual Cipher ToCipher(string userId = null)
{
return new Site
return new Cipher
{
Id = Id,
UserId = userId,
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId,
Name = Name,
Uri = Uri,
Username = Username,
Password = Password,
Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes
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 })
};
}
public Folder ToFolder(string userId = null)
{
return new Folder
{
Id = Id,
UserId = userId,
Name = Name
};
}
public static IEnumerable<dynamic> ToDynamicCiphers(CipherRequestModel[] models, string userId)
{
var sites = models.Where(m => m.Type == CipherType.Site).Select(m => m.ToSite(userId)).ToList();
var folders = models.Where(m => m.Type == CipherType.Folder).Select(m => m.ToFolder(userId)).ToList();
var ciphers = new List<dynamic>();
ciphers.AddRange(sites);
ciphers.AddRange(folders);
return ciphers;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(Type == CipherType.Site)

View File

@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
@ -12,18 +13,20 @@ namespace Bit.Api.Models
[StringLength(300)]
public string Name { get; set; }
public Folder ToFolder(string userId = null)
public Cipher ToCipher(string userId = null)
{
return new Folder
return new Cipher
{
UserId = userId,
Name = Name
UserId = new Guid(userId),
Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
Type = Core.Enums.CipherType.Folder
};
}
public Folder ToFolder(Folder existingFolder)
public Cipher ToCipher(Cipher existingFolder)
{
existingFolder.Name = Name;
existingFolder.Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
existingFolder.Type = Core.Enums.CipherType.Folder;
return existingFolder;
}

View File

@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
@ -28,28 +29,22 @@ namespace Bit.Api.Models
[StringLength(5000)]
public string Notes { get; set; }
public Site ToSite(string userId = null)
public Cipher ToCipher(string userId = null)
{
return new Site
return new Cipher
{
UserId = userId,
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId,
Name = Name,
Uri = Uri,
Username = string.IsNullOrWhiteSpace(Username) ? null : Username,
Password = Password,
Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes
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 Site ToSite(Site existingSite)
public Cipher ToCipher(Cipher existingSite)
{
existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId;
existingSite.Name = Name;
existingSite.Uri = Uri;
existingSite.Username = string.IsNullOrWhiteSpace(Username) ? null : Username;
existingSite.Password = Password;
existingSite.Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes;
existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
existingSite.Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
existingSite.Type = Core.Enums.CipherType.Site;
return existingSite;
}

View File

@ -1,21 +1,29 @@
using System;
using Bit.Core.Domains;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
public class FolderResponseModel : ResponseModel
{
public FolderResponseModel(Folder folder)
public FolderResponseModel(Cipher cipher)
: base("folder")
{
if(folder == null)
if(cipher == null)
{
throw new ArgumentNullException(nameof(folder));
throw new ArgumentNullException(nameof(cipher));
}
Id = folder.Id;
Name = folder.Name;
RevisionDate = folder.RevisionDate;
if(cipher.Type != Core.Enums.CipherType.Folder)
{
throw new ArgumentException(nameof(cipher.Type));
}
var data = JsonConvert.DeserializeObject<CipherDataModel>(cipher.Data);
Id = cipher.Id.ToString();
Name = data.Name;
RevisionDate = cipher.RevisionDate;
}
public string Id { get; set; }

View File

@ -13,7 +13,7 @@ namespace Bit.Api.Models
throw new ArgumentNullException(nameof(user));
}
Id = user.Id;
Id = user.Id.ToString();
Name = user.Name;
Email = user.Email;
MasterPasswordHint = string.IsNullOrWhiteSpace(user.MasterPasswordHint) ? null : user.MasterPasswordHint;

View File

@ -1,26 +1,34 @@
using System;
using Bit.Core.Domains;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
public class SiteResponseModel : ResponseModel
{
public SiteResponseModel(Site site)
public SiteResponseModel(Cipher cipher)
: base("site")
{
if(site == null)
if(cipher == null)
{
throw new ArgumentNullException(nameof(site));
throw new ArgumentNullException(nameof(cipher));
}
Id = site.Id;
FolderId = string.IsNullOrWhiteSpace(site.FolderId) ? null : site.FolderId;
Name = site.Name;
Uri = site.Uri;
Username = site.Username;
Password = site.Password;
Notes = site.Notes;
RevisionDate = site.RevisionDate;
if(cipher.Type != Core.Enums.CipherType.Site)
{
throw new ArgumentException(nameof(cipher.Type));
}
var data = JsonConvert.DeserializeObject<CipherDataModel>(cipher.Data);
Id = cipher.Id.ToString();
FolderId = cipher.FolderId?.ToString();
Name = data.Name;
Uri = data.Uri;
Username = data.Username;
Password = data.Password;
Notes = data.Notes;
RevisionDate = cipher.RevisionDate;
}
public string Id { get; set; }