Skip to content

Commit

Permalink
fix bumping AccountRevisionDate when creating and updating ciphers (#…
Browse files Browse the repository at this point in the history
…2634)

When the user is not part of an organization,
`UserBumpAccountRevisionDateByCipherIdQuery` doesn't work. In that case
we have to use `UserBumpAccountRevisionDateAsync` instead.

This was already done by most parts of the code but a few more were
missing.

Fixes #2615
  • Loading branch information
M1cha authored Feb 2, 2023
1 parent 28a3d4a commit e019f01
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CipherRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper
var dbContext = GetDatabaseContext(scope);
if (cipher.OrganizationId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId);
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.Value);
}
else if (cipher.UserId.HasValue)
{
Expand Down Expand Up @@ -59,7 +59,7 @@ public override async Task DeleteAsync(Core.Entities.Cipher cipher)
await OrganizationUpdateStorage(cipherInfo.OrganizationId.Value);
}

await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipherInfo.OrganizationId);
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipherInfo.OrganizationId.Value);
}
else if (cipherInfo?.UserId != null)
{
Expand Down Expand Up @@ -107,7 +107,16 @@ private async Task<CipherDetails> CreateAsyncReturnCipher(CipherDetails cipher)
null;
var entity = Mapper.Map<Cipher>((Core.Entities.Cipher)cipher);
await dbContext.AddAsync(entity);
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.GetValueOrDefault());

if (cipher.OrganizationId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.Value);
}
else if (cipher.UserId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateAsync(cipher.UserId.Value);
}

await dbContext.SaveChangesAsync();
}
return cipher;
Expand Down Expand Up @@ -458,7 +467,16 @@ public async Task ReplaceAsync(CipherDetails cipher)
}
var mappedEntity = Mapper.Map<Cipher>((Core.Entities.Cipher)cipher);
dbContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.GetValueOrDefault());

if (cipher.OrganizationId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.Value);
}
else if (cipher.UserId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateAsync(cipher.UserId.Value);
}

await dbContext.SaveChangesAsync();
}
}
Expand Down Expand Up @@ -566,7 +584,15 @@ public async Task<bool> ReplaceAsync(Core.Entities.Cipher cipher, IEnumerable<Gu
}
}

await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.GetValueOrDefault());
if (cipher.OrganizationId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.Value);
}
else if (cipher.UserId.HasValue)
{
await dbContext.UserBumpAccountRevisionDateAsync(cipher.UserId.Value);
}

await dbContext.SaveChangesAsync();
return true;
}
Expand Down Expand Up @@ -677,7 +703,7 @@ public async Task UpdateAttachmentAsync(CipherAttachment attachment)
if (attachment.OrganizationId.HasValue)
{
await OrganizationUpdateStorage(cipher.OrganizationId.Value);
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId);
await dbContext.UserBumpAccountRevisionDateByCipherIdAsync(cipher.Id, cipher.OrganizationId.Value);
}
else if (attachment.UserId.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ join ou in context.OrganizationUsers on u.Id equals ou.UserId
UpdateUserRevisionDate(users);
}

public static async Task UserBumpAccountRevisionDateByCipherIdAsync(this DatabaseContext context, Guid cipherId, Guid? organizationId)
public static async Task UserBumpAccountRevisionDateByCipherIdAsync(this DatabaseContext context, Guid cipherId, Guid organizationId)
{
var query = new UserBumpAccountRevisionDateByCipherIdQuery(cipherId, organizationId);
var users = await query.Run(context).ToListAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Enums;
using User = Bit.Infrastructure.EntityFramework.Models.User;

namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;

public class UserBumpAccountRevisionDateByCipherIdQuery : IQuery<User>
{
private readonly Guid _cipherId;
private readonly Guid? _organizationId;
private readonly Guid _organizationId;

public UserBumpAccountRevisionDateByCipherIdQuery(Cipher cipher)
{
_cipherId = cipher.Id;
_organizationId = cipher.OrganizationId;
}

public UserBumpAccountRevisionDateByCipherIdQuery(Guid cipherId, Guid? organizationId)
public UserBumpAccountRevisionDateByCipherIdQuery(Guid cipherId, Guid organizationId)
{
_cipherId = cipherId;
_organizationId = organizationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async void CreateAsync_BumpsOrgUserAccountRevisionDates(Cipher cipher, Li
var postEfCipher = await sut.CreateAsync(cipher);
sut.ClearChangeTracking();

var query = new UserBumpAccountRevisionDateByCipherIdQuery(cipher);
var query = new UserBumpAccountRevisionDateByCipherIdQuery(cipher, cipher.OrganizationId.Value);
var modifiedUsers = await sut.Run(query).ToListAsync();
Assert.True(modifiedUsers
.All(u => u.AccountRevisionDate.ToShortDateString() ==
Expand Down

0 comments on commit e019f01

Please sign in to comment.