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:
parent
609f3adb7b
commit
8ea3ae0b07
@ -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())
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user