diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index add6c9e16..91cce7f3f 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -376,7 +376,8 @@ namespace Bit.Api.Controllers userId.Value, model.Groups.Select(g => g.ToImportedGroup(orgIdGuid)), model.Users.Where(u => !u.Deleted).Select(u => u.ToImportedOrganizationUser()), - model.Users.Where(u => u.Deleted).Select(u => u.ExternalId)); + model.Users.Where(u => u.Deleted).Select(u => u.ExternalId), + model.OverwriteExisting); } [HttpPost("{id}/api-key")] diff --git a/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs b/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs index 33ba7455e..e9a421e15 100644 --- a/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/ImportOrganizationUsersRequestModel.cs @@ -10,6 +10,7 @@ namespace Bit.Core.Models.Api { public Group[] Groups { get; set; } public User[] Users { get; set; } + public bool OverwriteExisting { get; set; } public class Group { diff --git a/src/Core/Services/IOrganizationService.cs b/src/Core/Services/IOrganizationService.cs index 000c438c9..3e4180c0e 100644 --- a/src/Core/Services/IOrganizationService.cs +++ b/src/Core/Services/IOrganizationService.cs @@ -42,7 +42,8 @@ namespace Bit.Core.Services Task GenerateLicenseAsync(Guid organizationId, Guid installationId); Task GenerateLicenseAsync(Organization organization, Guid installationId); Task ImportAsync(Guid organizationId, Guid importingUserId, IEnumerable groups, - IEnumerable newUsers, IEnumerable removeUserExternalIds); + IEnumerable newUsers, IEnumerable removeUserExternalIds, + bool overwriteExisting); Task RotateApiKeyAsync(Organization organization); } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 2de4b5a5a..f10928b26 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -1143,7 +1143,8 @@ namespace Bit.Core.Services Guid importingUserId, IEnumerable groups, IEnumerable newUsers, - IEnumerable removeUserExternalIds) + IEnumerable removeUserExternalIds, + bool overwriteExisting) { var organization = await GetOrgById(organizationId); if(organization == null) @@ -1181,6 +1182,23 @@ namespace Bit.Core.Services } } + if(overwriteExisting) + { + // Remove existing external users that are not in new user set + foreach(var user in existingExternalUsers) + { + if(!newUsersSet.Contains(user.ExternalId) && + existingExternalUsersIdDict.ContainsKey(user.ExternalId)) + { + await _organizationUserRepository.DeleteAsync(new OrganizationUser + { + Id = user.Id + }); + existingExternalUsersIdDict.Remove(user.ExternalId); + } + } + } + if(newUsers?.Any() ?? false) { // Marry existing users