mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[PS-1806] Fix UpdateUsersAsync for EF (#2387)
* Fix UpdateUsersAsync * Update to make single call to DB * Loop through requested CollectionUsers * Delete unused code * Address PR Feedback
This commit is contained in:
@ -233,17 +233,48 @@ public class CollectionRepository : Repository<Core.Entities.Collection, Collect
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateUsersAsync(Guid id, IEnumerable<SelectionReadOnly> users)
|
||||
public async Task UpdateUsersAsync(Guid id, IEnumerable<SelectionReadOnly> requestedUsers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var procedure = new CollectionUserUpdateUsersQuery(id, users);
|
||||
var updateData = await procedure.Update.BuildInMemory(dbContext);
|
||||
dbContext.UpdateRange(updateData);
|
||||
var insertData = await procedure.Insert.BuildInMemory(dbContext);
|
||||
await dbContext.AddRangeAsync(insertData);
|
||||
dbContext.RemoveRange(await procedure.Delete.Run(dbContext).ToListAsync());
|
||||
|
||||
var organizationId = await dbContext.Collections
|
||||
.Where(c => c.Id == id)
|
||||
.Select(c => c.OrganizationId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var existingCollectionUsers = await dbContext.CollectionUsers
|
||||
.Where(cu => cu.CollectionId == id)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var requestedUser in requestedUsers)
|
||||
{
|
||||
var existingCollectionUser = existingCollectionUsers.FirstOrDefault(cu => cu.OrganizationUserId == requestedUser.Id);
|
||||
if (existingCollectionUser == null)
|
||||
{
|
||||
// This is a brand new entry
|
||||
dbContext.CollectionUsers.Add(new CollectionUser
|
||||
{
|
||||
CollectionId = id,
|
||||
OrganizationUserId = requestedUser.Id,
|
||||
HidePasswords = requestedUser.HidePasswords,
|
||||
ReadOnly = requestedUser.ReadOnly,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// It already exists, update it
|
||||
existingCollectionUser.HidePasswords = requestedUser.HidePasswords;
|
||||
existingCollectionUser.ReadOnly = requestedUser.ReadOnly;
|
||||
dbContext.CollectionUsers.Update(existingCollectionUser);
|
||||
}
|
||||
|
||||
// Remove all existing ones that are no longer requested
|
||||
var requestedUserIds = requestedUsers.Select(u => u.Id);
|
||||
dbContext.CollectionUsers.RemoveRange(existingCollectionUsers.Where(cu => !requestedUserIds.Contains(cu.OrganizationUserId)));
|
||||
await UserBumpAccountRevisionDateByCollectionId(id, organizationId);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user