1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-26 17:37:36 +01:00

[PS-1948] Fix Group Collections Update (#2465)

This commit is contained in:
Justin Baur 2022-12-02 22:19:14 -05:00 committed by GitHub
parent 609f3adb7b
commit 8ea3ae0b07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -129,13 +129,50 @@ public class GroupRepository : Repository<Core.Entities.Group, Group, Guid>, IGr
} }
} }
public async Task ReplaceAsync(Core.Entities.Group obj, IEnumerable<SelectionReadOnly> collections) public async Task ReplaceAsync(Core.Entities.Group group, IEnumerable<SelectionReadOnly> requestedCollections)
{ {
await base.ReplaceAsync(obj); await base.ReplaceAsync(group);
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
var dbContext = GetDatabaseContext(scope); var dbContext = GetDatabaseContext(scope);
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(obj.OrganizationId);
var availableCollections = await dbContext.Collections
.Where(c => c.OrganizationId == group.OrganizationId)
.Select(c => c.Id)
.ToListAsync();
var existingCollectionGroups = await dbContext.CollectionGroups
.Where(cg => cg.GroupId == group.Id)
.ToListAsync();
foreach (var requestedCollection in requestedCollections)
{
var existingCollectionGroup = existingCollectionGroups
.FirstOrDefault(cg => cg.CollectionId == requestedCollection.Id);
if (existingCollectionGroup == null)
{
// It needs to be added
dbContext.CollectionGroups.Add(new CollectionGroup
{
CollectionId = requestedCollection.Id,
GroupId = group.Id,
ReadOnly = requestedCollection.ReadOnly,
HidePasswords = requestedCollection.HidePasswords,
});
continue;
}
existingCollectionGroup.ReadOnly = requestedCollection.ReadOnly;
existingCollectionGroup.HidePasswords = requestedCollection.HidePasswords;
}
var requestedCollectionIds = requestedCollections.Select(c => c.Id);
dbContext.CollectionGroups.RemoveRange(
existingCollectionGroups.Where(cg => !requestedCollectionIds.Contains(cg.CollectionId)));
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(group.OrganizationId);
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
} }
} }