1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

[SG-378] Get and send collectionIds when a cipher is updated (#2066)

* Get and send collectionIds when a cipher is updated

* Make Put method parameters Guids instead of strings
This commit is contained in:
Robyn MacCallum 2022-06-21 08:25:54 -04:00 committed by GitHub
parent a94823f648
commit f2a02e040c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 16 deletions

View File

@ -170,15 +170,16 @@ namespace Bit.Api.Controllers
[HttpPut("{id}")] [HttpPut("{id}")]
[HttpPost("{id}")] [HttpPost("{id}")]
public async Task<CipherResponseModel> Put(string id, [FromBody] CipherRequestModel model) public async Task<CipherResponseModel> Put(Guid id, [FromBody] CipherRequestModel model)
{ {
var userId = _userService.GetProperUserId(User).Value; var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId); var cipher = await _cipherRepository.GetByIdAsync(id, userId);
if (cipher == null) if (cipher == null)
{ {
throw new NotFoundException(); throw new NotFoundException();
} }
var collectionIds = (await _collectionCipherRepository.GetManyByUserIdCipherIdAsync(userId, id)).Select(c => c.CollectionId).ToList();
var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ? var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ?
(Guid?)null : new Guid(model.OrganizationId); (Guid?)null : new Guid(model.OrganizationId);
if (cipher.OrganizationId != modelOrgId) if (cipher.OrganizationId != modelOrgId)
@ -187,7 +188,7 @@ namespace Bit.Api.Controllers
"then try again."); "then try again.");
} }
await _cipherService.SaveDetailsAsync(model.ToCipherDetails(cipher), userId, model.LastKnownRevisionDate); await _cipherService.SaveDetailsAsync(model.ToCipherDetails(cipher), userId, model.LastKnownRevisionDate, collectionIds);
var response = new CipherResponseModel(cipher, _globalSettings); var response = new CipherResponseModel(cipher, _globalSettings);
return response; return response;
@ -195,19 +196,20 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/admin")] [HttpPut("{id}/admin")]
[HttpPost("{id}/admin")] [HttpPost("{id}/admin")]
public async Task<CipherMiniResponseModel> PutAdmin(string id, [FromBody] CipherRequestModel model) public async Task<CipherMiniResponseModel> PutAdmin(Guid id, [FromBody] CipherRequestModel model)
{ {
var userId = _userService.GetProperUserId(User).Value; var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetOrganizationDetailsByIdAsync(new Guid(id)); var cipher = await _cipherRepository.GetOrganizationDetailsByIdAsync(id);
if (cipher == null || !cipher.OrganizationId.HasValue || if (cipher == null || !cipher.OrganizationId.HasValue ||
!await _currentContext.EditAnyCollection(cipher.OrganizationId.Value)) !await _currentContext.EditAnyCollection(cipher.OrganizationId.Value))
{ {
throw new NotFoundException(); throw new NotFoundException();
} }
var collectionIds = (await _collectionCipherRepository.GetManyByUserIdCipherIdAsync(userId, id)).Select(c => c.CollectionId).ToList();
// object cannot be a descendant of CipherDetails, so let's clone it. // object cannot be a descendant of CipherDetails, so let's clone it.
var cipherClone = model.ToCipher(cipher).Clone(); var cipherClone = model.ToCipher(cipher).Clone();
await _cipherService.SaveAsync(cipherClone, userId, model.LastKnownRevisionDate, null, true, false); await _cipherService.SaveAsync(cipherClone, userId, model.LastKnownRevisionDate, collectionIds, true, false);
var response = new CipherMiniResponseModel(cipherClone, _globalSettings, cipher.OrganizationUseTotp); var response = new CipherMiniResponseModel(cipherClone, _globalSettings, cipher.OrganizationUseTotp);
return response; return response;

View File

@ -98,17 +98,13 @@ namespace Bit.Core.Services
} }
else else
{ {
if (collectionIds != null)
{
throw new ArgumentException("Cannot create cipher with collection ids at the same time.");
}
ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate); ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
cipher.RevisionDate = DateTime.UtcNow; cipher.RevisionDate = DateTime.UtcNow;
await _cipherRepository.ReplaceAsync(cipher); await _cipherRepository.ReplaceAsync(cipher);
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated); await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);
// push // push
await _pushService.PushSyncCipherUpdateAsync(cipher, null); await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
} }
} }
@ -156,17 +152,13 @@ namespace Bit.Core.Services
} }
else else
{ {
if (collectionIds != null)
{
throw new ArgumentException("Cannot create cipher with collection ids at the same time.");
}
ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate); ValidateCipherLastKnownRevisionDateAsync(cipher, lastKnownRevisionDate);
cipher.RevisionDate = DateTime.UtcNow; cipher.RevisionDate = DateTime.UtcNow;
await _cipherRepository.ReplaceAsync(cipher); await _cipherRepository.ReplaceAsync(cipher);
await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated); await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Updated);
// push // push
await _pushService.PushSyncCipherUpdateAsync(cipher, null); await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
} }
} }