From b8b5bc93b89b072efc4c1bc5c7c1871088f46541 Mon Sep 17 00:00:00 2001 From: Michael Zimmermann Date: Thu, 26 Jan 2023 12:47:26 +0100 Subject: [PATCH] fix bumping `AccountRevisionDate` when creating and updating ciphers 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 --- .../Repositories/CipherRepository.cs | 38 ++++++++++++++++--- .../Repositories/DatabaseContextExtensions.cs | 2 +- ...rBumpAccountRevisionDateByCipherIdQuery.cs | 13 ++----- .../Repositories/CipherRepositoryTests.cs | 2 +- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs index 096c90518405..49b407018c0e 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs @@ -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) { @@ -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) { @@ -107,7 +107,16 @@ private async Task CreateAsyncReturnCipher(CipherDetails cipher) null; var entity = Mapper.Map((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; @@ -458,7 +467,16 @@ public async Task ReplaceAsync(CipherDetails cipher) } var mappedEntity = Mapper.Map((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(); } } @@ -566,7 +584,15 @@ public async Task ReplaceAsync(Core.Entities.Cipher cipher, IEnumerable { 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; diff --git a/test/Infrastructure.EFIntegration.Test/Repositories/CipherRepositoryTests.cs b/test/Infrastructure.EFIntegration.Test/Repositories/CipherRepositoryTests.cs index 7868c7c0347c..19cef95ec093 100644 --- a/test/Infrastructure.EFIntegration.Test/Repositories/CipherRepositoryTests.cs +++ b/test/Infrastructure.EFIntegration.Test/Repositories/CipherRepositoryTests.cs @@ -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() ==