1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +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())
{
var dbContext = GetDatabaseContext(scope);
var availableCollectionsCte = from c in dbContext.Collections
where c.OrganizationId == organizationId
select c;
var target = from cc in dbContext.CollectionCiphers
where cc.CipherId == cipherId
select new { cc.CollectionId, cc.CipherId };
var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId });
var merge1 = from t in target
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
var availableCollections = await (from c in dbContext.Collections
where c.OrganizationId == organizationId
select c).ToListAsync();
var currentCollectionCiphers = await (from cc in dbContext.CollectionCiphers
where cc.CipherId == cipherId
select cc).ToListAsync();
foreach (var requestedCollectionId in collectionIds)
{
var requestedCollectionCipher = currentCollectionCiphers
.FirstOrDefault(cc => cc.CollectionId == requestedCollectionId);
if (requestedCollectionCipher == null)
{
CollectionId = x.s.CollectionId,
CipherId = x.s.CipherId,
});
var delete = union
.Where(x => x.s == null && x.t.CipherId == cipherId)
.Select(x => new Models.CollectionCipher
{
CollectionId = x.t.CollectionId,
CipherId = x.t.CipherId,
});
await dbContext.AddRangeAsync(insert);
dbContext.RemoveRange(delete);
dbContext.CollectionCiphers.Add(new Models.CollectionCipher
{
CipherId = cipherId,
CollectionId = requestedCollectionId,
});
}
}
dbContext.RemoveRange(currentCollectionCiphers.Where(cc => !collectionIds.Contains(cc.CollectionId)));
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
await dbContext.SaveChangesAsync();
}