From 6958a3fda578f59f11e77654f2121c131c9880bc Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 15 May 2017 16:37:56 -0400 Subject: [PATCH] logic tweaks for group assignments --- .../Controllers/OrganizationsController.cs | 8 ++++-- .../ImportOrganizationUsersRequestModel.cs | 4 +-- .../Implementations/OrganizationService.cs | 28 ++++++++++++------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index c63f09796c..3cb8e6eee5 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -239,8 +239,12 @@ namespace Bit.Api.Controllers } var userId = _userService.GetProperUserId(User); - await _organizationService.ImportAsync(orgIdGuid, userId.Value, model.Groups.Select(g => g.ToGroupTuple(orgIdGuid)), - model.NewUsers.Select(u => u.Email), model.RemoveUsers.Select(u => u.Email)); + await _organizationService.ImportAsync( + orgIdGuid, + userId.Value, + model.Groups.Select(g => g.ToGroupTuple(orgIdGuid)), + model.Users.Where(u => !u.Disabled).Select(u => u.Email), + model.Users.Where(u => u.Disabled).Select(u => u.Email)); } } } diff --git a/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs b/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs index 93b243dd7b..6e892064d5 100644 --- a/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs @@ -8,8 +8,7 @@ namespace Bit.Core.Models.Api public class ImportOrganizationUsersRequestModel { public Group[] Groups { get; set; } - public User[] NewUsers { get; set; } - public User[] RemoveUsers { get; set; } + public User[] Users { get; set; } public class Group { @@ -37,6 +36,7 @@ namespace Bit.Core.Models.Api [Required] [EmailAddress] public string Email { get; set; } + public bool Disabled { get; set; } } } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index c29a5c0c68..0d55f941d6 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -981,9 +981,6 @@ namespace Bit.Core.Services var newGroups = groups .Where(g => !existingGroupsDict.ContainsKey(g.Item1.ExternalId)) .Select(g => g.Item1); - var updateGroups = existingGroups - .Where(eg => groups.Any(g => g.Item1.ExternalId == eg.ExternalId && g.Item1.Name != eg.Name)) - .ToList(); foreach(var group in newGroups) { @@ -993,20 +990,30 @@ namespace Bit.Core.Services await UpdateUsersAsync(group, groupsDict[group.ExternalId].Item2, existingUsersIdDict); } + var updateGroups = existingGroups + .Where(g => groupsDict.ContainsKey(g.ExternalId)) + .ToList(); + if(updateGroups.Any()) { - var existingGroupUsers = (await _groupRepository.GetManyGroupUsersByOrganizationIdAsync(organizationId)) + var groupUsers = await _groupRepository.GetManyGroupUsersByOrganizationIdAsync(organizationId); + var existingGroupUsers = groupUsers .GroupBy(gu => gu.GroupId) - .ToDictionary(g => g.Key, g => new HashSet(g.Select(g => g.OrganizationUserId))); + .ToDictionary(g => g.Key, g => new HashSet(g.Select(gr => gr.OrganizationUserId))); foreach(var group in updateGroups) { - group.RevisionDate = DateTime.UtcNow; - group.Name = existingGroupsDict[group.ExternalId].Name; + var updatedGroup = groupsDict[group.ExternalId].Item1; + if(group.Name != updatedGroup.Name) + { + group.RevisionDate = DateTime.UtcNow; + group.Name = updatedGroup.Name; + + await _groupRepository.ReplaceAsync(group); + } - await _groupRepository.ReplaceAsync(group); await UpdateUsersAsync(group, groupsDict[group.ExternalId].Item2, existingUsersIdDict, - existingGroupUsers[group.Id]); + existingGroupUsers.ContainsKey(group.Id) ? existingGroupUsers[group.Id] : null); } } } @@ -1015,7 +1022,8 @@ namespace Bit.Core.Services private async Task UpdateUsersAsync(Group group, HashSet groupUsers, Dictionary existingUsersIdDict, HashSet existingUsers = null) { - var users = new HashSet(groupUsers.Union(existingUsersIdDict.Keys).Select(u => existingUsersIdDict[u])); + var availableUsers = groupUsers.Intersect(existingUsersIdDict.Keys); + var users = new HashSet(availableUsers.Select(u => existingUsersIdDict[u])); if(existingUsers != null && existingUsers.Count == users.Count && users.SetEquals(existingUsers)) { return;