1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-21 04:55:08 -05:00

variable renames and null checks

This commit is contained in:
Kyle Spearrin 2017-05-18 10:41:47 -04:00
parent fca12ecda6
commit c3060e7801

View File

@ -917,16 +917,16 @@ namespace Bit.Core.Services
} }
var newUsersSet = new HashSet<string>(newUsers.Select(u => u.ExternalId)); var newUsersSet = new HashSet<string>(newUsers.Select(u => u.ExternalId));
var existingUsersOriginal = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(organizationId); var existingUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(organizationId);
var existingUsers = existingUsersOriginal.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId)).ToList(); var existingExternalUsers = existingUsers.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId)).ToList();
var existingUsersIdDict = existingUsers.ToDictionary(u => u.ExternalId, u => u.Id); var existingExternalUsersIdDict = existingExternalUsers.ToDictionary(u => u.ExternalId, u => u.Id);
// Users // Users
// Remove Users // Remove Users
if(removeUserExternalIds.Any()) if(removeUserExternalIds.Any())
{ {
var removeUsersSet = new HashSet<string>(removeUserExternalIds); var removeUsersSet = new HashSet<string>(removeUserExternalIds);
var existingUsersDict = existingUsers.ToDictionary(u => u.ExternalId); var existingUsersDict = existingExternalUsers.ToDictionary(u => u.ExternalId);
var usersToRemove = removeUsersSet var usersToRemove = removeUsersSet
.Except(newUsersSet) .Except(newUsersSet)
@ -936,14 +936,14 @@ namespace Bit.Core.Services
foreach(var user in usersToRemove) foreach(var user in usersToRemove)
{ {
await _organizationUserRepository.DeleteAsync(new OrganizationUser { Id = user.Id }); await _organizationUserRepository.DeleteAsync(new OrganizationUser { Id = user.Id });
existingUsersIdDict.Remove(user.ExternalId); existingExternalUsersIdDict.Remove(user.ExternalId);
} }
} }
// Add new users // Add new users
if(newUsers.Any()) if(newUsers.Any())
{ {
var existingUsersSet = new HashSet<string>(existingUsers.Select(u => u.ExternalId)); var existingUsersSet = new HashSet<string>(existingExternalUsers.Select(u => u.ExternalId));
var usersToAdd = newUsersSet.Except(existingUsersSet).ToList(); var usersToAdd = newUsersSet.Except(existingUsersSet).ToList();
var seatsAvailable = int.MaxValue; var seatsAvailable = int.MaxValue;
@ -968,7 +968,7 @@ namespace Bit.Core.Services
{ {
var newUser = await InviteUserAsync(organizationId, importingUserId, user.Email, var newUser = await InviteUserAsync(organizationId, importingUserId, user.Email,
OrganizationUserType.User, false, user.ExternalId, new List<SelectionReadOnly>()); OrganizationUserType.User, false, user.ExternalId, new List<SelectionReadOnly>());
existingUsersIdDict.Add(newUser.ExternalId, newUser.Id); existingExternalUsersIdDict.Add(newUser.ExternalId, newUser.Id);
} }
catch(BadRequestException) catch(BadRequestException)
{ {
@ -977,7 +977,7 @@ namespace Bit.Core.Services
} }
} }
var existingUsersEmailsDict = existingUsersOriginal var existingUsersEmailsDict = existingUsers
.Where(u => string.IsNullOrWhiteSpace(u.ExternalId)) .Where(u => string.IsNullOrWhiteSpace(u.ExternalId))
.ToDictionary(u => u.Email); .ToDictionary(u => u.Email);
var newUsersEmailsDict = newUsers.ToDictionary(u => u.Email); var newUsersEmailsDict = newUsers.ToDictionary(u => u.Email);
@ -990,7 +990,7 @@ namespace Bit.Core.Services
{ {
orgUser.ExternalId = newUsersEmailsDict[user].ExternalId; orgUser.ExternalId = newUsersEmailsDict[user].ExternalId;
await _organizationUserRepository.UpsertAsync(orgUser); await _organizationUserRepository.UpsertAsync(orgUser);
existingUsersIdDict.Add(orgUser.ExternalId, orgUser.Id); existingExternalUsersIdDict.Add(orgUser.ExternalId, orgUser.Id);
} }
} }
} }
@ -999,11 +999,12 @@ namespace Bit.Core.Services
if(groups?.Any() ?? false) if(groups?.Any() ?? false)
{ {
var groupsDict = groups.ToDictionary(g => g.Group.ExternalId); var groupsDict = groups.ToDictionary(g => g.Group.ExternalId);
var existingGroups = (await _groupRepository.GetManyByOrganizationIdAsync(organizationId)).ToList(); var existingGroups = await _groupRepository.GetManyByOrganizationIdAsync(organizationId);
var existingGroupsDict = existingGroups.ToDictionary(g => g.ExternalId); var existingExternalGroups = existingGroups.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId)).ToList();
var existingExternalGroupsDict = existingExternalGroups.ToDictionary(g => g.ExternalId);
var newGroups = groups var newGroups = groups
.Where(g => !existingGroupsDict.ContainsKey(g.Group.ExternalId)) .Where(g => !existingExternalGroupsDict.ContainsKey(g.Group.ExternalId))
.Select(g => g.Group); .Select(g => g.Group);
foreach(var group in newGroups) foreach(var group in newGroups)
@ -1011,10 +1012,10 @@ namespace Bit.Core.Services
group.CreationDate = group.RevisionDate = DateTime.UtcNow; group.CreationDate = group.RevisionDate = DateTime.UtcNow;
await _groupRepository.CreateAsync(group); await _groupRepository.CreateAsync(group);
await UpdateUsersAsync(group, groupsDict[group.ExternalId].ExternalUserIds, existingUsersIdDict); await UpdateUsersAsync(group, groupsDict[group.ExternalId].ExternalUserIds, existingExternalUsersIdDict);
} }
var updateGroups = existingGroups var updateGroups = existingExternalGroups
.Where(g => groupsDict.ContainsKey(g.ExternalId)) .Where(g => groupsDict.ContainsKey(g.ExternalId))
.ToList(); .ToList();
@ -1036,7 +1037,7 @@ namespace Bit.Core.Services
await _groupRepository.ReplaceAsync(group); await _groupRepository.ReplaceAsync(group);
} }
await UpdateUsersAsync(group, groupsDict[group.ExternalId].ExternalUserIds, existingUsersIdDict, await UpdateUsersAsync(group, groupsDict[group.ExternalId].ExternalUserIds, existingExternalUsersIdDict,
existingGroupUsers.ContainsKey(group.Id) ? existingGroupUsers[group.Id] : null); existingGroupUsers.ContainsKey(group.Id) ? existingGroupUsers[group.Id] : null);
} }
} }