mirror of
https://github.com/bitwarden/server.git
synced 2025-06-28 14:46:14 -05:00
cr feedback
This commit is contained in:
parent
36879fe1f9
commit
4af676e959
@ -5,8 +5,23 @@ namespace Bit.Core.Models.Data.Organizations;
|
|||||||
|
|
||||||
public class OrganizationGroupImportData
|
public class OrganizationGroupImportData
|
||||||
{
|
{
|
||||||
public IEnumerable<ImportedGroup> Groups { get; set; }
|
public IEnumerable<ImportedGroup> Groups { get; init; }
|
||||||
public ICollection<Group> ExistingGroups { get; set; }
|
public ICollection<Group> ExistingGroups { get; init; }
|
||||||
public IEnumerable<Group> ExistingExternalGroups { get; set; }
|
public IEnumerable<Group> ExistingExternalGroups { get; init; }
|
||||||
public IDictionary<string, ImportedGroup> GroupsDict { get; set; }
|
public IDictionary<string, ImportedGroup> GroupsDict { get; init; }
|
||||||
|
|
||||||
|
public OrganizationGroupImportData(IEnumerable<ImportedGroup> groups, ICollection<Group> existingGroups)
|
||||||
|
{
|
||||||
|
Groups = groups;
|
||||||
|
GroupsDict = groups.ToDictionary(g => g.Group.ExternalId);
|
||||||
|
ExistingGroups = existingGroups;
|
||||||
|
ExistingExternalGroups = GetExistingExternalGroups(existingGroups);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<Group> GetExistingExternalGroups(ICollection<Group> existingGroups)
|
||||||
|
{
|
||||||
|
return existingGroups
|
||||||
|
.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,23 @@
|
|||||||
|
|
||||||
public class OrganizationUserImportData
|
public class OrganizationUserImportData
|
||||||
{
|
{
|
||||||
public HashSet<string> NewUsersSet { get; set; }
|
public HashSet<string> NewUsersSet { get; init; }
|
||||||
public ICollection<OrganizationUserUserDetails> ExistingUsers { get; set; }
|
public ICollection<OrganizationUserUserDetails> ExistingUsers { get; init; }
|
||||||
public IEnumerable<OrganizationUserUserDetails> ExistingExternalUsers { get; set; }
|
public IEnumerable<OrganizationUserUserDetails> ExistingExternalUsers { get; init; }
|
||||||
public Dictionary<string, Guid> ExistingExternalUsersIdDict { get; set; }
|
public Dictionary<string, Guid> ExistingExternalUsersIdDict { get; init; }
|
||||||
|
|
||||||
|
public OrganizationUserImportData(ICollection<OrganizationUserUserDetails> existingUsers, HashSet<string> newUsersSet)
|
||||||
|
{
|
||||||
|
NewUsersSet = newUsersSet;
|
||||||
|
ExistingUsers = existingUsers;
|
||||||
|
ExistingExternalUsers = GetExistingExternalUsers(existingUsers);
|
||||||
|
ExistingExternalUsersIdDict = GetExistingExternalUsers(existingUsers).ToDictionary(u => u.ExternalId, u => u.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<OrganizationUserUserDetails> GetExistingExternalUsers(ICollection<OrganizationUserUserDetails> existingUsers)
|
||||||
|
{
|
||||||
|
return existingUsers
|
||||||
|
.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
private readonly IOrganizationService _organizationService;
|
private readonly IOrganizationService _organizationService;
|
||||||
private readonly IInviteOrganizationUsersCommand _inviteOrganizationUsersCommand;
|
private readonly IInviteOrganizationUsersCommand _inviteOrganizationUsersCommand;
|
||||||
private readonly IPricingClient _pricingClient;
|
private readonly IPricingClient _pricingClient;
|
||||||
|
private readonly TimeProvider _timeProvider;
|
||||||
|
|
||||||
private readonly EventSystemUser _EventSystemUser = EventSystemUser.PublicApi;
|
private readonly EventSystemUser _EventSystemUser = EventSystemUser.PublicApi;
|
||||||
|
|
||||||
@ -40,7 +41,8 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
ICurrentContext currentContext,
|
ICurrentContext currentContext,
|
||||||
IOrganizationService organizationService,
|
IOrganizationService organizationService,
|
||||||
IInviteOrganizationUsersCommand inviteOrganizationUsersCommand,
|
IInviteOrganizationUsersCommand inviteOrganizationUsersCommand,
|
||||||
IPricingClient pricingClient
|
IPricingClient pricingClient,
|
||||||
|
TimeProvider timeProvider
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_organizationRepository = organizationRepository;
|
_organizationRepository = organizationRepository;
|
||||||
@ -52,6 +54,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
_organizationService = organizationService;
|
_organizationService = organizationService;
|
||||||
_inviteOrganizationUsersCommand = inviteOrganizationUsersCommand;
|
_inviteOrganizationUsersCommand = inviteOrganizationUsersCommand;
|
||||||
_pricingClient = pricingClient;
|
_pricingClient = pricingClient;
|
||||||
|
_timeProvider = timeProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ImportAsync(Guid organizationId,
|
public async Task ImportAsync(Guid organizationId,
|
||||||
@ -72,15 +75,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
var existingUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(organizationId);
|
var existingUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(organizationId);
|
||||||
|
var importUserData = new OrganizationUserImportData(existingUsers, new HashSet<string>(newUsers?.Select(u => u.ExternalId) ?? new List<string>()));
|
||||||
var importUserData = new OrganizationUserImportData
|
|
||||||
{
|
|
||||||
NewUsersSet = new HashSet<string>(newUsers?.Select(u => u.ExternalId) ?? new List<string>()),
|
|
||||||
ExistingUsers = existingUsers,
|
|
||||||
ExistingExternalUsers = GetExistingExternalUsers(existingUsers),
|
|
||||||
ExistingExternalUsersIdDict = GetExistingExternalUsers(existingUsers).ToDictionary(u => u.ExternalId, u => u.Id)
|
|
||||||
};
|
|
||||||
|
|
||||||
var events = new List<(OrganizationUserUserDetails ou, EventType e, DateTime? d)>();
|
var events = new List<(OrganizationUserUserDetails ou, EventType e, DateTime? d)>();
|
||||||
|
|
||||||
await RemoveExistingExternalUsers(removeUserExternalIds, events, importUserData);
|
await RemoveExistingExternalUsers(removeUserExternalIds, events, importUserData);
|
||||||
@ -90,7 +85,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
await OverwriteExisting(events, importUserData);
|
await OverwriteExisting(events, importUserData);
|
||||||
}
|
}
|
||||||
|
|
||||||
await UpsertExistingUsers(organization, newUsers, importUserData);
|
await UpsertExistingUsers(newUsers, importUserData);
|
||||||
|
|
||||||
await AddNewUsers(organization, newUsers, importUserData);
|
await AddNewUsers(organization, newUsers, importUserData);
|
||||||
|
|
||||||
@ -136,10 +131,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UpsertExistingUsers(Organization organization,
|
private async Task UpsertExistingUsers(IEnumerable<ImportedOrganizationUser> newUsers, OrganizationUserImportData importUserData)
|
||||||
IEnumerable<ImportedOrganizationUser> newUsers,
|
|
||||||
OrganizationUserImportData importUserData
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
if (!newUsers.Any())
|
if (!newUsers.Any())
|
||||||
{
|
{
|
||||||
@ -157,7 +149,9 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
|
|
||||||
foreach (var user in newAndExistingUsersIntersection)
|
foreach (var user in newAndExistingUsersIntersection)
|
||||||
{
|
{
|
||||||
var organizationUser = organizationUsers[existingUsersEmailsDict[user].Id];
|
existingUsersEmailsDict.TryGetValue(user, out var existingUser);
|
||||||
|
organizationUsers.TryGetValue(existingUser.Id, out var organizationUser);
|
||||||
|
|
||||||
if (organizationUser != null)
|
if (organizationUser != null)
|
||||||
{
|
{
|
||||||
organizationUser.ExternalId = newUsersEmailsDict[user].ExternalId;
|
organizationUser.ExternalId = newUsersEmailsDict[user].ExternalId;
|
||||||
@ -172,14 +166,11 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
IEnumerable<ImportedOrganizationUser> newUsers,
|
IEnumerable<ImportedOrganizationUser> newUsers,
|
||||||
OrganizationUserImportData importUserData)
|
OrganizationUserImportData importUserData)
|
||||||
{
|
{
|
||||||
|
|
||||||
var hasStandaloneSecretsManager = await _paymentService.HasSecretsManagerStandalone(organization);
|
|
||||||
var userInvites = new List<OrganizationUserInviteCommandModel>();
|
var userInvites = new List<OrganizationUserInviteCommandModel>();
|
||||||
|
|
||||||
foreach (var user in newUsers)
|
foreach (var user in newUsers)
|
||||||
{
|
{
|
||||||
var invite = new OrganizationUserInviteCommandModel(user.Email, user.ExternalId);
|
userInvites.Add(new OrganizationUserInviteCommandModel(user.Email, user.ExternalId));
|
||||||
userInvites.Add(new OrganizationUserInviteCommandModel(invite, hasStandaloneSecretsManager));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var commandResult = await InviteUsersAsync(userInvites, organization);
|
var commandResult = await InviteUsersAsync(userInvites, organization);
|
||||||
@ -246,13 +237,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
var existingGroups = await _groupRepository.GetManyByOrganizationIdAsync(organization.Id);
|
var existingGroups = await _groupRepository.GetManyByOrganizationIdAsync(organization.Id);
|
||||||
var importGroupData = new OrganizationGroupImportData
|
var importGroupData = new OrganizationGroupImportData(groups, existingGroups);
|
||||||
{
|
|
||||||
Groups = groups,
|
|
||||||
GroupsDict = groups.ToDictionary(g => g.Group.ExternalId),
|
|
||||||
ExistingGroups = existingGroups,
|
|
||||||
ExistingExternalGroups = GetExistingExternalGroups(existingGroups)
|
|
||||||
};
|
|
||||||
|
|
||||||
await SaveNewGroups(importGroupData, importUserData);
|
await SaveNewGroups(importGroupData, importUserData);
|
||||||
await UpdateExistingGroups(importGroupData, importUserData, organization);
|
await UpdateExistingGroups(importGroupData, importUserData, organization);
|
||||||
@ -316,20 +301,6 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<OrganizationUserUserDetails> GetExistingExternalUsers(ICollection<OrganizationUserUserDetails> existingUsers)
|
|
||||||
{
|
|
||||||
return existingUsers
|
|
||||||
.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId))
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<Group> GetExistingExternalGroups(ICollection<Group> existingGroups)
|
|
||||||
{
|
|
||||||
return existingGroups
|
|
||||||
.Where(u => !string.IsNullOrWhiteSpace(u.ExternalId))
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<Organization> GetOrgById(Guid id)
|
private async Task<Organization> GetOrgById(Guid id)
|
||||||
{
|
{
|
||||||
return await _organizationRepository.GetByIdAsync(id);
|
return await _organizationRepository.GetByIdAsync(id);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user