Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PS-2363] fix bumping AccountRevisionDate when creating and updating ciphers #2634

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
justindbaur marked this conversation as resolved.
Show resolved Hide resolved

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