1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00

fix logic in some EF cipher queries (#2366)

* fix logic in some cipher queries

* Update src/Infrastructure.EntityFramework/Repositories/Queries/UserCipherDetailsQuery.cs

Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>

* Update src/Infrastructure.EntityFramework/Repositories/Queries/UserCipherDetailsQuery.cs

Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
This commit is contained in:
Kyle Spearrin 2022-10-27 10:03:35 -04:00 committed by GitHub
parent c5fb49758a
commit d60a0f52fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -261,7 +261,7 @@ public class CipherRepository : Repository<Core.Entities.Cipher, Cipher, Guid>,
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = new CipherOrganizationDetailsReadByIdQuery(organizationId);
var query = new CipherOrganizationDetailsReadByOrgizationIdQuery(organizationId);
var data = await query.Run(dbContext).ToListAsync();
return data;
}
@ -477,6 +477,16 @@ public class CipherRepository : Repository<Core.Entities.Cipher, Cipher, Guid>,
private async Task<DateTime> ToggleCipherStates(IEnumerable<Guid> ids, Guid userId, CipherStateAction action)
{
static bool FilterDeletedDate(CipherStateAction action, CipherDetails ucd)
{
return action switch
{
CipherStateAction.Restore => ucd.DeletedDate != null,
CipherStateAction.SoftDelete => ucd.DeletedDate == null,
_ => true,
};
}
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
@ -485,7 +495,7 @@ public class CipherRepository : Repository<Core.Entities.Cipher, Cipher, Guid>,
var query = from ucd in await (userCipherDetailsQuery.Run(dbContext)).ToListAsync()
join c in cipherEntitiesToCheck
on ucd.Id equals c.Id
where ucd.Edit && ucd.DeletedDate == null
where ucd.Edit && FilterDeletedDate(action, ucd)
select c;
var utcNow = DateTime.UtcNow;

View File

@ -1,6 +1,6 @@
using Bit.Core.Enums;
using System.Text.Json;
using Bit.Core.Enums;
using Core.Models.Data;
using Newtonsoft.Json.Linq;
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
@ -59,13 +59,24 @@ public class UserCipherDetailsQuery : IQuery<CipherDetails>
RevisionDate = c.RevisionDate,
DeletedDate = c.DeletedDate,
Favorite = _userId.HasValue && c.Favorites != null && c.Favorites.Contains($"\"{_userId}\":true"),
FolderId = _userId.HasValue && !string.IsNullOrWhiteSpace(c.Folders) ?
Guid.Parse(JObject.Parse(c.Folders)[_userId.Value.ToString()].Value<string>()) :
null,
FolderId = GetFolderId(_userId, c),
Edit = true,
ViewPassword = true,
OrganizationUseTotp = false,
});
return union;
}
private static Guid? GetFolderId(Guid? userId, Models.Cipher cipher)
{
if (userId.HasValue && !string.IsNullOrWhiteSpace(cipher.Folders))
{
var folders = JsonSerializer.Deserialize<Dictionary<Guid, Guid>>(cipher.Folders);
if (folders.TryGetValue(userId.Value, out var folder))
{
return folder;
}
}
return null;
}
}