From 00853bc250062f7065c88adf3950f6026daabf6c Mon Sep 17 00:00:00 2001 From: Addison Beck Date: Sat, 30 Jan 2021 17:56:37 -0500 Subject: [PATCH] Import Invite Error (#1121) * throw error if user is already invited on import * added back the single InviteUser OrgService method --- .../Public/Controllers/MembersController.cs | 4 ++-- src/Core/Services/IOrganizationService.cs | 2 ++ .../Implementations/OrganizationService.cs | 24 ++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Api/Public/Controllers/MembersController.cs b/src/Api/Public/Controllers/MembersController.cs index a3b1febad2..58ae110560 100644 --- a/src/Api/Public/Controllers/MembersController.cs +++ b/src/Api/Public/Controllers/MembersController.cs @@ -124,8 +124,8 @@ namespace Bit.Api.Public.Controllers AccessAll = model.AccessAll.Value, Collections = associations }; - var userPromise = await _organizationService.InviteUserAsync(_currentContext.OrganizationId.Value, null, model.ExternalId, invite); - var user = userPromise.FirstOrDefault(); + var user = await _organizationService.InviteUserAsync(_currentContext.OrganizationId.Value, null, + model.Email, model.Type.Value, model.AccessAll.Value, model.ExternalId, associations); var response = new MemberResponseModel(user, associations); return new JsonResult(response); } diff --git a/src/Core/Services/IOrganizationService.cs b/src/Core/Services/IOrganizationService.cs index 618073bcd3..3a158ec743 100644 --- a/src/Core/Services/IOrganizationService.cs +++ b/src/Core/Services/IOrganizationService.cs @@ -30,6 +30,8 @@ namespace Bit.Core.Services Task UpdateAsync(Organization organization, bool updateBilling = false); Task UpdateTwoFactorProviderAsync(Organization organization, TwoFactorProviderType type); Task DisableTwoFactorProviderAsync(Organization organization, TwoFactorProviderType type); + Task InviteUserAsync(Guid organizationId, Guid? invitingUserId, string email, + OrganizationUserType type, bool accessAll, string externalId, IEnumerable collections); Task> InviteUserAsync(Guid organizationId, Guid? invitingUserId, string externalId, OrganizationUserInvite orgUserInvite); Task ResendInviteAsync(Guid organizationId, Guid? invitingUserId, Guid organizationUserId); Task AcceptUserAsync(Guid organizationUserId, User user, string token, diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 79537c284f..62586858bb 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -1388,6 +1388,25 @@ namespace Bit.Core.Services return new OrganizationLicense(organization, subInfo, installationId, _licensingService, version); } + public async Task InviteUserAsync(Guid organizationId, Guid? invitingUserId, string email, + OrganizationUserType type, bool accessAll, string externalId, IEnumerable collections) + { + var invite = new OrganizationUserInvite() + { + Emails = new List { email }, + Type = type, + AccessAll = accessAll, + Collections = collections, + }; + var results = await InviteUserAsync(organizationId, invitingUserId, externalId, invite); + var result = results.FirstOrDefault(); + if (result == null) + { + throw new BadRequestException("This user has already been invited."); + } + return result; + } + public async Task ImportAsync(Guid organizationId, Guid? importingUserId, IEnumerable groups, @@ -1499,9 +1518,8 @@ namespace Bit.Core.Services AccessAll = false, Collections = new List(), }; - var newUserPromise = await InviteUserAsync(organizationId, importingUserId, user.ExternalId, invite); - var newUser = newUserPromise.FirstOrDefault(); - + var newUser = await InviteUserAsync(organizationId, importingUserId, user.Email, + OrganizationUserType.User, false, user.ExternalId, new List()); existingExternalUsersIdDict.Add(newUser.ExternalId, newUser.Id); } catch (BadRequestException)