1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 16:42:50 -05:00

refactored cipherfolder and fav to JSON columns

This commit is contained in:
Kyle Spearrin
2017-04-15 22:26:45 -04:00
parent 8e193dfc62
commit f21652b46b
21 changed files with 216 additions and 272 deletions

View File

@ -10,6 +10,8 @@ namespace Bit.Core.Models.Table
public Guid? OrganizationId { get; set; }
public Enums.CipherType Type { get; set; }
public string Data { get; set; }
public string Favorites { get; set; }
public string Folders { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -1,10 +0,0 @@
using System;
namespace Bit.Core.Models.Table
{
public class Favorite
{
public Guid CipherId { get; set; }
public Guid UserId { get; set; }
}
}

View File

@ -1,11 +0,0 @@
using System;
namespace Bit.Core.Models.Table
{
public class FolderCipher
{
public Guid CipherId { get; set; }
public Guid FolderId { get; set; }
public Guid UserId { get; set; }
}
}

View File

@ -19,7 +19,6 @@ namespace Bit.Core.Repositories
Task ReplaceAsync(Cipher obj, IEnumerable<Guid> subvaultIds);
Task UpdatePartialAsync(Guid id, Guid userId, Guid? folderId, bool favorite);
Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers);
Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Favorite> favorites, IEnumerable<Folder> folders,
IEnumerable<FolderCipher> folderCiphers);
Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders);
}
}

View File

@ -237,8 +237,7 @@ namespace Bit.Core.Repositories.SqlServer
return Task.FromResult(0);
}
public Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Favorite> favorites, IEnumerable<Folder> folders,
IEnumerable<FolderCipher> folderCiphers)
public Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders)
{
if(!ciphers.Any())
{
@ -272,28 +271,6 @@ namespace Bit.Core.Repositories.SqlServer
bulkCopy.WriteToServer(dataTable);
}
if(folderCiphers.Any())
{
using(var bulkCopy = new SqlBulkCopy(connection,
SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.FireTriggers, transaction))
{
bulkCopy.DestinationTableName = "[dbo].[FolderCipher]";
var dataTable = BuildFolderCiphersTable(folderCiphers);
bulkCopy.WriteToServer(dataTable);
}
}
if(favorites.Any())
{
using(var bulkCopy = new SqlBulkCopy(connection,
SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.FireTriggers, transaction))
{
bulkCopy.DestinationTableName = "[dbo].[Favorite]";
var dataTable = BuildFavoritesTable(favorites);
bulkCopy.WriteToServer(dataTable);
}
}
transaction.Commit();
}
catch
@ -327,6 +304,10 @@ namespace Bit.Core.Repositories.SqlServer
ciphersTable.Columns.Add(typeColumn);
var dataColumn = new DataColumn(nameof(c.Data), typeof(string));
ciphersTable.Columns.Add(dataColumn);
var favoritesColumn = new DataColumn(nameof(c.Favorites), typeof(string));
ciphersTable.Columns.Add(favoritesColumn);
var foldersColumn = new DataColumn(nameof(c.Folders), typeof(string));
ciphersTable.Columns.Add(foldersColumn);
var creationDateColumn = new DataColumn(nameof(c.CreationDate), c.CreationDate.GetType());
ciphersTable.Columns.Add(creationDateColumn);
var revisionDateColumn = new DataColumn(nameof(c.RevisionDate), c.RevisionDate.GetType());
@ -345,6 +326,8 @@ namespace Bit.Core.Repositories.SqlServer
row[organizationId] = cipher.OrganizationId.HasValue ? (object)cipher.OrganizationId.Value : DBNull.Value;
row[typeColumn] = (short)cipher.Type;
row[dataColumn] = cipher.Data;
row[favoritesColumn] = cipher.Favorites;
row[foldersColumn] = cipher.Folders;
row[creationDateColumn] = cipher.CreationDate;
row[revisionDateColumn] = cipher.RevisionDate;
@ -354,76 +337,6 @@ namespace Bit.Core.Repositories.SqlServer
return ciphersTable;
}
private DataTable BuildFavoritesTable(IEnumerable<Favorite> favorites)
{
var f = favorites.FirstOrDefault();
if(f == null)
{
throw new ApplicationException("Must have some favorites to bulk import.");
}
var favoritesTable = new DataTable("FavoriteDataTable");
var userIdColumn = new DataColumn(nameof(f.UserId), f.UserId.GetType());
favoritesTable.Columns.Add(userIdColumn);
var cipherIdColumn = new DataColumn(nameof(f.CipherId), f.CipherId.GetType());
favoritesTable.Columns.Add(cipherIdColumn);
var keys = new DataColumn[2];
keys[0] = userIdColumn;
keys[1] = cipherIdColumn;
favoritesTable.PrimaryKey = keys;
foreach(var favorite in favorites)
{
var row = favoritesTable.NewRow();
row[cipherIdColumn] = favorite.CipherId;
row[userIdColumn] = favorite.UserId;
favoritesTable.Rows.Add(row);
}
return favoritesTable;
}
private DataTable BuildFolderCiphersTable(IEnumerable<FolderCipher> folderCiphers)
{
var f = folderCiphers.FirstOrDefault();
if(f == null)
{
throw new ApplicationException("Must have some folderCiphers to bulk import.");
}
var folderCiphersTable = new DataTable("FolderCipherDataTable");
var folderIdColumn = new DataColumn(nameof(f.FolderId), f.FolderId.GetType());
folderCiphersTable.Columns.Add(folderIdColumn);
var cipherIdColumn = new DataColumn(nameof(f.CipherId), f.CipherId.GetType());
folderCiphersTable.Columns.Add(cipherIdColumn);
var userIdColumn = new DataColumn(nameof(f.UserId), f.UserId.GetType());
folderCiphersTable.Columns.Add(userIdColumn);
var keys = new DataColumn[3];
keys[0] = folderIdColumn;
keys[1] = cipherIdColumn;
keys[2] = userIdColumn;
folderCiphersTable.PrimaryKey = keys;
foreach(var folderCipher in folderCiphers)
{
var row = folderCiphersTable.NewRow();
row[folderIdColumn] = folderCipher.FolderId;
row[cipherIdColumn] = folderCipher.CipherId;
row[userIdColumn] = folderCipher.UserId;
folderCiphersTable.Rows.Add(row);
}
return folderCiphersTable;
}
private DataTable BuildFoldersTable(IEnumerable<Folder> folders)
{
var f = folders.FirstOrDefault();

View File

@ -186,19 +186,13 @@ namespace Bit.Core.Services
List<CipherDetails> ciphers,
IEnumerable<KeyValuePair<int, int>> folderRelationships)
{
// Init. ids and build out favorites.
var favorites = new List<Favorite>();
foreach(var cipher in ciphers)
{
cipher.SetNewId();
if(cipher.UserId.HasValue && cipher.Favorite)
{
favorites.Add(new Favorite
{
UserId = cipher.UserId.Value,
CipherId = cipher.Id
});
cipher.Favorites = $"[{{\"u\":\"{cipher.UserId.ToString().ToUpperInvariant()}\"}}]";
}
}
@ -209,7 +203,6 @@ namespace Bit.Core.Services
}
// Create the folder associations based on the newly created folder ids
var folderCiphers = new List<FolderCipher>();
foreach(var relationship in folderRelationships)
{
var cipher = ciphers.ElementAtOrDefault(relationship.Key);
@ -220,16 +213,12 @@ namespace Bit.Core.Services
continue;
}
folderCiphers.Add(new FolderCipher
{
FolderId = folder.Id,
CipherId = cipher.Id,
UserId = folder.UserId
});
cipher.Folders = $"[{{\"u\":\"{cipher.UserId.ToString().ToUpperInvariant()}\"," +
$"\"f\":\"{folder.Id.ToString().ToUpperInvariant()}\"}}]";
}
// Create it all
await _cipherRepository.CreateAsync(ciphers, favorites, folders, folderCiphers);
await _cipherRepository.CreateAsync(ciphers, folders);
// push
var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;