diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index fdf4131e59..6435976d1d 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -230,7 +230,7 @@ namespace Bit.Api.Controllers } [HttpPost("{id}/import")] - public Task Import(string id, [FromBody]ImportOrganizationUsersRequestModel model) + public async Task Import(string id, [FromBody]ImportOrganizationUsersRequestModel model) { var orgIdGuid = new Guid(id); if(!_currentContext.OrganizationAdmin(orgIdGuid)) @@ -238,7 +238,9 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - return Task.FromResult(0); + var userId = _userService.GetProperUserId(User); + await _organizationService.ImportAsync(orgIdGuid, userId.Value, model.Groups.Select(g => g.ToGroup(orgIdGuid)), + model.Users.Select(u => u.ToKvp())); } } } diff --git a/src/Core/Services/IOrganizationService.cs b/src/Core/Services/IOrganizationService.cs index 69d2f6a45a..1df0fad82d 100644 --- a/src/Core/Services/IOrganizationService.cs +++ b/src/Core/Services/IOrganizationService.cs @@ -29,5 +29,7 @@ namespace Bit.Core.Services Task SaveUserAsync(OrganizationUser user, Guid savingUserId, IEnumerable collections); Task DeleteUserAsync(Guid organizationId, Guid organizationUserId, Guid deletingUserId); Task DeleteUserAsync(Guid organizationId, Guid userId); + Task ImportAsync(Guid organizationId, Guid importingUserId, IEnumerable groups, + IEnumerable>> users); } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 6425c48c67..006a3a977c 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -921,10 +921,12 @@ namespace Bit.Core.Services var newGroups = groups.Where(g => !existingGroupsDict.ContainsKey(g.ExternalId)); var updateGroups = existingGroups.Where(eg => groups.Any(g => g.ExternalId == eg.ExternalId && g.Name != eg.Name)); + var createdGroups = new List(); foreach(var group in newGroups) { group.CreationDate = group.RevisionDate = DateTime.UtcNow; await _groupRepository.CreateAsync(group); + createdGroups.Add(group); } foreach(var group in updateGroups) @@ -935,12 +937,12 @@ namespace Bit.Core.Services } // Add the newly created groups to existing groups so that we have a complete list to reference below for users. - existingGroups.AddRange(newGroups); + existingGroups.AddRange(createdGroups); existingGroupsDict = existingGroups.ToDictionary(g => g.ExternalId); } // Users - if(users?.Any() ?? false) + if(!users?.Any() ?? true) { return; }