1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-01 13:43:23 +01:00

[PS-1930] Fix UpdateCollectionsForAdminAsync (#2473)

* Fix UpdateCollectionsForAdminAsync

* Formatting
This commit is contained in:
Justin Baur 2022-12-05 13:59:11 -05:00 committed by GitHub
parent f3b4363440
commit c25ef0be5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -147,42 +147,30 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
var dbContext = GetDatabaseContext(scope); var dbContext = GetDatabaseContext(scope);
var availableCollectionsCte = from c in dbContext.Collections var availableCollections = await (from c in dbContext.Collections
where c.OrganizationId == organizationId where c.OrganizationId == organizationId
select c; select c).ToListAsync();
var target = from cc in dbContext.CollectionCiphers
var currentCollectionCiphers = await (from cc in dbContext.CollectionCiphers
where cc.CipherId == cipherId where cc.CipherId == cipherId
select new { cc.CollectionId, cc.CipherId }; select cc).ToListAsync();
var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId });
var merge1 = from t in target foreach (var requestedCollectionId in collectionIds)
join s in source
on t.CollectionId equals s.CollectionId into s_g
from s in s_g.DefaultIfEmpty()
where t.CipherId == s.CipherId
select new { t, s };
var merge2 = from s in source
join t in target
on s.CollectionId equals t.CollectionId into t_g
from t in t_g.DefaultIfEmpty()
where t.CipherId == s.CipherId
select new { t, s };
var union = merge1.Union(merge2).Distinct();
var insert = union
.Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId))
.Select(x => new Models.CollectionCipher
{ {
CollectionId = x.s.CollectionId, var requestedCollectionCipher = currentCollectionCiphers
CipherId = x.s.CipherId, .FirstOrDefault(cc => cc.CollectionId == requestedCollectionId);
});
var delete = union if (requestedCollectionCipher == null)
.Where(x => x.s == null && x.t.CipherId == cipherId)
.Select(x => new Models.CollectionCipher
{ {
CollectionId = x.t.CollectionId, dbContext.CollectionCiphers.Add(new Models.CollectionCipher
CipherId = x.t.CipherId, {
CipherId = cipherId,
CollectionId = requestedCollectionId,
}); });
await dbContext.AddRangeAsync(insert); }
dbContext.RemoveRange(delete); }
dbContext.RemoveRange(currentCollectionCiphers.Where(cc => !collectionIds.Contains(cc.CollectionId)));
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId); await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
} }