1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-28 06:36:15 -05:00

cleanup params

This commit is contained in:
Brandon 2025-05-30 14:03:34 -04:00
parent af11c07d91
commit f6035d8881
No known key found for this signature in database
GPG Key ID: A0E0EF0B207BA40D
4 changed files with 22 additions and 31 deletions

View File

@ -3,7 +3,6 @@ using Bit.Api.AdminConsole.Public.Models.Request;
using Bit.Api.Models.Public.Response; using Bit.Api.Models.Public.Response;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces;
using Bit.Core.Context; using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Settings; using Bit.Core.Settings;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@ -52,8 +51,7 @@ public class OrganizationController : Controller
model.Groups.Select(g => g.ToImportedGroup(_currentContext.OrganizationId.Value)), model.Groups.Select(g => g.ToImportedGroup(_currentContext.OrganizationId.Value)),
model.Members.Where(u => !u.Deleted).Select(u => u.ToImportedOrganizationUser()), model.Members.Where(u => !u.Deleted).Select(u => u.ToImportedOrganizationUser()),
model.Members.Where(u => u.Deleted).Select(u => u.ExternalId), model.Members.Where(u => u.Deleted).Select(u => u.ExternalId),
model.OverwriteExisting.GetValueOrDefault(), model.OverwriteExisting.GetValueOrDefault());
EventSystemUser.PublicApi);
return new OkResult(); return new OkResult();
} }
} }

View File

@ -34,6 +34,8 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
private readonly IInviteOrganizationUsersCommand _inviteOrganizationUsersCommand; private readonly IInviteOrganizationUsersCommand _inviteOrganizationUsersCommand;
private readonly IPricingClient _pricingClient; private readonly IPricingClient _pricingClient;
private readonly EventSystemUser _EventSystemUser = EventSystemUser.PublicApi;
public ImportOrganizationUserCommand(IOrganizationRepository organizationRepository, public ImportOrganizationUserCommand(IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository, IOrganizationUserRepository organizationUserRepository,
IPaymentService paymentService, IPaymentService paymentService,
@ -62,8 +64,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
IEnumerable<ImportedGroup> groups, IEnumerable<ImportedGroup> groups,
IEnumerable<ImportedOrganizationUser> newUsers, IEnumerable<ImportedOrganizationUser> newUsers,
IEnumerable<string> removeUserExternalIds, IEnumerable<string> removeUserExternalIds,
bool overwriteExisting, bool overwriteExisting)
EventSystemUser eventSystemUser)
{ {
var organization = await GetOrgById(organizationId); var organization = await GetOrgById(organizationId);
if (organization == null) if (organization == null)
@ -95,13 +96,13 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
await OverwriteExisting(events, importUserData); await OverwriteExisting(events, importUserData);
} }
await UpsertExistingUsers(existingUsers, newUsers, organization, importUserData); await UpsertExistingUsers(organization, newUsers, importUserData);
await AddNewUsers(organization, newUsers, eventSystemUser, importUserData); await AddNewUsers(organization, newUsers, importUserData);
await ImportGroups(organization, groups, eventSystemUser, importUserData); await ImportGroups(organization, groups, importUserData);
await _eventService.LogOrganizationUserEventsAsync(events.Select(e => (e.ou, e.e, eventSystemUser, e.d))); await _eventService.LogOrganizationUserEventsAsync(events.Select(e => (e.ou, e.e, _EventSystemUser, e.d)));
await _referenceEventService.RaiseEventAsync( await _referenceEventService.RaiseEventAsync(
new ReferenceEvent(ReferenceEventType.DirectorySynced, organization, _currentContext)); new ReferenceEvent(ReferenceEventType.DirectorySynced, organization, _currentContext));
@ -144,10 +145,10 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
); );
} }
private async Task UpsertExistingUsers(IEnumerable<OrganizationUserUserDetails> existingUsers, private async Task UpsertExistingUsers(Organization organization,
IEnumerable<ImportedOrganizationUser> newUsers, IEnumerable<ImportedOrganizationUser> newUsers,
Organization organization, OrganizationUserImportData importUserData
OrganizationUserImportData importUserData) )
{ {
if (!newUsers.Any()) if (!newUsers.Any())
{ {
@ -155,15 +156,14 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
} }
// Marry existing users // Marry existing users
var existingUsersEmailsDict = existingUsers var existingUsersEmailsDict = importUserData.ExistingUsers
.Where(u => string.IsNullOrWhiteSpace(u.ExternalId)) .Where(u => string.IsNullOrWhiteSpace(u.ExternalId))
.ToDictionary(u => u.Email); .ToDictionary(u => u.Email);
var newUsersEmailsDict = newUsers.ToDictionary(u => u.Email); var newUsersEmailsDict = newUsers.ToDictionary(u => u.Email);
var newAndExistingUsersIntersection = existingUsersEmailsDict.Keys.Intersect(newUsersEmailsDict.Keys).ToList(); var newAndExistingUsersIntersection = existingUsersEmailsDict.Keys.Intersect(newUsersEmailsDict.Keys).ToList();
var organizationUsers = (await _organizationUserRepository.GetManyAsync(existingUsers.Select(u => u.Id).ToList())).ToDictionary(u => u.Id); var organizationUsers = (await _organizationUserRepository.GetManyAsync(importUserData.ExistingUsers.Select(u => u.Id).ToList())).ToDictionary(u => u.Id);
var usersToUpsert = new List<OrganizationUser>(); var usersToUpsert = new List<OrganizationUser>();
foreach (var user in newAndExistingUsersIntersection) foreach (var user in newAndExistingUsersIntersection)
{ {
var organizationUser = organizationUsers[existingUsersEmailsDict[user].Id]; var organizationUser = organizationUsers[existingUsersEmailsDict[user].Id];
@ -179,7 +179,6 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
private async Task AddNewUsers(Organization organization, private async Task AddNewUsers(Organization organization,
IEnumerable<ImportedOrganizationUser> newUsers, IEnumerable<ImportedOrganizationUser> newUsers,
EventSystemUser eventSystemUser,
OrganizationUserImportData importUserData) OrganizationUserImportData importUserData)
{ {
@ -242,7 +241,6 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
private async Task ImportGroups(Organization organization, private async Task ImportGroups(Organization organization,
IEnumerable<ImportedGroup> groups, IEnumerable<ImportedGroup> groups,
EventSystemUser eventSystemUser,
OrganizationUserImportData importUserData) OrganizationUserImportData importUserData)
{ {
@ -265,13 +263,11 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
ExistingExternalGroups = GetExistingExternalGroups(existingGroups) ExistingExternalGroups = GetExistingExternalGroups(existingGroups)
}; };
await SaveNewGroups(importGroupData, importUserData, eventSystemUser); await SaveNewGroups(importGroupData, importUserData);
await UpdateExistingGroups(importGroupData, importUserData, organization, eventSystemUser); await UpdateExistingGroups(importGroupData, importUserData, organization);
} }
private async Task SaveNewGroups(OrganizationGroupImportData importGroupData, private async Task SaveNewGroups(OrganizationGroupImportData importGroupData, OrganizationUserImportData importUserData)
OrganizationUserImportData importUserData,
EventSystemUser eventSystemUser)
{ {
var newGroups = importGroupData.Groups var newGroups = importGroupData.Groups
.Where(g => !importGroupData.ExistingExternalGroups.ToDictionary(g => g.ExternalId).ContainsKey(g.Group.ExternalId)) .Where(g => !importGroupData.ExistingExternalGroups.ToDictionary(g => g.ExternalId).ContainsKey(g.Group.ExternalId))
@ -288,13 +284,12 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
} }
await _eventService.LogGroupEventsAsync( await _eventService.LogGroupEventsAsync(
savedGroups.Select(g => (g, EventType.Group_Created, (EventSystemUser?)eventSystemUser, (DateTime?)DateTime.UtcNow))); savedGroups.Select(g => (g, EventType.Group_Created, (EventSystemUser?)_EventSystemUser, (DateTime?)DateTime.UtcNow)));
} }
private async Task UpdateExistingGroups(OrganizationGroupImportData importGroupData, private async Task UpdateExistingGroups(OrganizationGroupImportData importGroupData,
OrganizationUserImportData importUserData, OrganizationUserImportData importUserData,
Organization organization, Organization organization)
EventSystemUser eventSystemUser)
{ {
var updateGroups = importGroupData.ExistingExternalGroups var updateGroups = importGroupData.ExistingExternalGroups
.Where(g => importGroupData.GroupsDict.ContainsKey(g.ExternalId)) .Where(g => importGroupData.GroupsDict.ContainsKey(g.ExternalId))
@ -325,7 +320,7 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
} }
await _eventService.LogGroupEventsAsync( await _eventService.LogGroupEventsAsync(
updateGroups.Select(g => (g, EventType.Group_Updated, (EventSystemUser?)eventSystemUser, (DateTime?)DateTime.UtcNow))); updateGroups.Select(g => (g, EventType.Group_Updated, (EventSystemUser?)_EventSystemUser, (DateTime?)DateTime.UtcNow)));
} }
} }

View File

@ -1,5 +1,4 @@
using Bit.Core.AdminConsole.Models.Business; using Bit.Core.AdminConsole.Models.Business;
using Bit.Core.Enums;
using Bit.Core.Models.Business; using Bit.Core.Models.Business;
namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces;
@ -10,7 +9,6 @@ public interface IImportOrganizationUserCommand
IEnumerable<ImportedGroup> groups, IEnumerable<ImportedGroup> groups,
IEnumerable<ImportedOrganizationUser> newUsers, IEnumerable<ImportedOrganizationUser> newUsers,
IEnumerable<string> removeUserExternalIds, IEnumerable<string> removeUserExternalIds,
bool overwriteExisting, bool overwriteExisting
EventSystemUser eventSystemUser
); );
} }

View File

@ -60,7 +60,7 @@ public class ImportOrganizationUserCommandTests
sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>()) sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>())
.Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id))); .Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id)));
await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false, EventSystemUser.PublicApi); await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false);
await sutProvider.GetDependency<IInviteOrganizationUsersCommand>().Received(1) await sutProvider.GetDependency<IInviteOrganizationUsersCommand>().Received(1)
.InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>()); .InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>());
@ -112,7 +112,7 @@ public class ImportOrganizationUserCommandTests
sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>()) sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>())
.Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id))); .Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id)));
await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false, EventSystemUser.PublicApi); await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false);
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs() await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
.UpsertAsync(default); .UpsertAsync(default);