1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

overwrite existing users on import

This commit is contained in:
Kyle Spearrin 2019-05-06 21:31:20 -04:00
parent afdf29da78
commit 0c760cf9e1
4 changed files with 24 additions and 3 deletions

View File

@ -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")]

View File

@ -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
{

View File

@ -42,7 +42,8 @@ namespace Bit.Core.Services
Task<OrganizationLicense> GenerateLicenseAsync(Guid organizationId, Guid installationId);
Task<OrganizationLicense> GenerateLicenseAsync(Organization organization, Guid installationId);
Task ImportAsync(Guid organizationId, Guid importingUserId, IEnumerable<ImportedGroup> groups,
IEnumerable<ImportedOrganizationUser> newUsers, IEnumerable<string> removeUserExternalIds);
IEnumerable<ImportedOrganizationUser> newUsers, IEnumerable<string> removeUserExternalIds,
bool overwriteExisting);
Task RotateApiKeyAsync(Organization organization);
}
}

View File

@ -1143,7 +1143,8 @@ namespace Bit.Core.Services
Guid importingUserId,
IEnumerable<ImportedGroup> groups,
IEnumerable<ImportedOrganizationUser> newUsers,
IEnumerable<string> removeUserExternalIds)
IEnumerable<string> 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