1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

[PM-2383] Bulk collection assignment (#3919)

* [PM-2383] Add bulk add/remove collection cipher repository methods

* [PM-2383] Add additional authorization helpers for CiphersControlle

* [PM-2383] Add /bulk-collections endpoint to CiphersController.cs

* [PM-2383] Add EF implementation for new CollectionCipherRepository methods

* [PM-2383] Ensure V1 logic only applies when the flag is enabled for new bulk functionality
This commit is contained in:
Shane Melton
2024-03-22 13:16:34 -07:00
committed by GitHub
parent 5dd1a9410a
commit 6a0f6e1dac
9 changed files with 448 additions and 1 deletions

View File

@ -248,4 +248,47 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
await dbContext.SaveChangesAsync();
}
}
public async Task AddCollectionsForManyCiphersAsync(Guid organizationId, IEnumerable<Guid> cipherIds,
IEnumerable<Guid> collectionIds)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var availableCollections = await (from c in dbContext.Collections
join o in dbContext.Organizations on c.OrganizationId equals o.Id
where o.Id == organizationId && o.Enabled
select c).ToListAsync();
var currentCollectionCiphers = await (from cc in dbContext.CollectionCiphers
where cipherIds.Contains(cc.CipherId)
select cc).ToListAsync();
var insertData = from collectionId in collectionIds
from cipherId in cipherIds
where
availableCollections.Select(c => c.Id).Contains(collectionId) &&
!currentCollectionCiphers.Any(cc => cc.CipherId == cipherId && cc.CollectionId == collectionId)
select new Models.CollectionCipher { CollectionId = collectionId, CipherId = cipherId, };
await dbContext.AddRangeAsync(insertData);
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
await dbContext.SaveChangesAsync();
}
}
public async Task RemoveCollectionsForManyCiphersAsync(Guid organizationId, IEnumerable<Guid> cipherIds,
IEnumerable<Guid> collectionIds)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var currentCollectionCiphersToBeRemoved = await (from cc in dbContext.CollectionCiphers
where cipherIds.Contains(cc.CipherId) && collectionIds.Contains(cc.CollectionId)
select cc).ToListAsync();
dbContext.RemoveRange(currentCollectionCiphersToBeRemoved);
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
await dbContext.SaveChangesAsync();
}
}
}