1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-17 23:50:58 -05:00

refactor cipher queries by user. tuned indexing.

This commit is contained in:
Kyle Spearrin
2018-04-24 12:48:43 -04:00
parent ac4f789782
commit 165ee97d2f
16 changed files with 321 additions and 175 deletions

View File

@ -12,10 +12,8 @@ namespace Bit.Core.Repositories
Task<CipherDetails> GetByIdAsync(Guid id, Guid userId);
Task<CipherDetails> GetDetailsByIdAsync(Guid id);
Task<bool> GetCanEditByIdAsync(Guid userId, Guid cipherId);
Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId);
Task<ICollection<CipherDetails>> GetManyByUserIdHasCollectionsAsync(Guid userId);
Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true);
Task<ICollection<Cipher>> GetManyByOrganizationIdAsync(Guid organizationId);
Task<ICollection<CipherDetails>> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId);
Task CreateAsync(CipherDetails cipher);
Task ReplaceAsync(CipherDetails cipher);
Task UpsertAsync(CipherDetails cipher);

View File

@ -62,33 +62,27 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId)
public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId, bool withOrganizations = true)
{
using(var connection = new SqlConnection(ConnectionString))
string sprocName = null;
// Always "with organizations" for now. Future TODO is to possibly move to another, simpler sproc when no
// orgs are expected.
if(true || withOrganizations)
{
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherDetails_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
// Return distinct Id results. If at least one of the grouped results allows edit, that we return it.
return results
.GroupBy(c => c.Id)
.Select(g => g.OrderByDescending(og => og.Edit).First())
.ToList();
sprocName = $"[{Schema}].[CipherDetails_ReadByUserId]";
}
else
{
sprocName = $"[{Schema}].[CipherDetails_ReadWithoutOrganizationsByUserId]";
}
}
public async Task<ICollection<CipherDetails>> GetManyByUserIdHasCollectionsAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherDetails_ReadByUserIdHasCollection]",
sprocName,
new { UserId = userId },
commandType: CommandType.StoredProcedure);
// Return distinct Id results. If at least one of the grouped results allows edit, that we return it.
return results
.GroupBy(c => c.Id)
.Select(g => g.OrderByDescending(og => og.Edit).First())
@ -109,27 +103,6 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<CipherDetails>> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherDetails_ReadByTypeUserId]",
new
{
Type = type,
UserId = userId
},
commandType: CommandType.StoredProcedure);
// Return distinct Id results. If at least one of the grouped results allows edit, that we return it.
return results
.GroupBy(c => c.Id)
.Select(g => g.OrderByDescending(og => og.Edit).First())
.ToList();
}
}
public async Task CreateAsync(CipherDetails cipher)
{
cipher.SetNewId();