diff --git a/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs b/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs index 5623b7f..d48c2aa 100644 --- a/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs +++ b/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs @@ -10,6 +10,7 @@ namespace SecurityService.BusinessLogic.RequestHandlers{ using MessagingService.Client; using MessagingService.DataTransferObjects; using Microsoft.AspNetCore.Identity; + using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Requests; using SecurityService.Models; @@ -103,34 +104,40 @@ public async Task Handle(SecurityServiceCommands.CreateUserCommand comma Result addRolesToUserResult = await this.AddRolesToUser(newIdentityUser, command.Roles); Result addClaimsToUserResult = await this.AddClaimsToUser(newIdentityUser, command); - Result sendEmailResult; - if (createResult.IsSuccess && addRolesToUserResult.IsSuccess && addClaimsToUserResult.IsSuccess) { - // If we are here we have created the user - String confirmationToken = await this.UserManager.GenerateEmailConfirmationTokenAsync(newIdentityUser); - confirmationToken = UrlEncoder.Default.Encode(confirmationToken); - String uri = $"{this.ServiceOptions.PublicOrigin}/Account/EmailConfirmation/Confirm?userName={newIdentityUser.UserName}&confirmationToken={confirmationToken}"; - - TokenResponse token = await this.GetToken(); - SendEmailRequest emailRequest = this.BuildEmailConfirmationRequest(newIdentityUser, uri); - sendEmailResult = await this.MessagingServiceClient.SendEmail(token.AccessToken, emailRequest, cancellationToken); - if (sendEmailResult.IsFailed) - Logger.LogWarning($"Error sending email to {newIdentityUser.Email} as part of user creation {sendEmailResult}"); - } + Result result = (createResult.IsSuccess, addRolesToUserResult.IsSuccess, addClaimsToUserResult.IsSuccess) switch + { + (true, true, true) => await SendConfirmationEmail(newIdentityUser, cancellationToken), + _ => await DeleteUser(createResult, addRolesToUserResult,addClaimsToUserResult, newIdentityUser) + }; + return result; + } - if (createResult.IsFailed || addRolesToUserResult.IsFailed || addClaimsToUserResult.IsFailed) { - // User has been created so need to remove this - IdentityResult deleteResult = await this.UserManager.DeleteAsync(newIdentityUser); + private async Task SendConfirmationEmail(ApplicationUser newIdentityUser, CancellationToken cancellationToken) { + // If we are here we have created the user + String confirmationToken = await this.UserManager.GenerateEmailConfirmationTokenAsync(newIdentityUser); + confirmationToken = UrlEncoder.Default.Encode(confirmationToken); + String uri = $"{this.ServiceOptions.PublicOrigin}/Account/EmailConfirmation/Confirm?userName={newIdentityUser.UserName}&confirmationToken={confirmationToken}"; - if (deleteResult.Succeeded == false) { - return Result.Failure($"Error deleting user {newIdentityUser.UserName} as part of cleanup {deleteResult}"); - } + TokenResponse token = await this.GetToken(); + SendEmailRequest emailRequest = this.BuildEmailConfirmationRequest(newIdentityUser, uri); + Result sendEmailResult = await this.MessagingServiceClient.SendEmail(token.AccessToken, emailRequest, cancellationToken); + if (sendEmailResult.IsFailed) + Logger.LogWarning($"Error sending email to {newIdentityUser.Email} as part of user creation {sendEmailResult}"); + return Result.Success(); + } + + private async Task DeleteUser(Result createResult, Result addRolesToUserResult, Result addClaimsToUserResult , ApplicationUser identityUserToDelete) { + // User has been created so need to remove this + IdentityResult deleteResult = await this.UserManager.DeleteAsync(identityUserToDelete); - return Result.Failure($"At least one part of the user creation failed - createResult: {createResult.IsSuccess} addRolesToUserResult: {addRolesToUserResult.IsSuccess} addClaimsToUserResult: {addClaimsToUserResult.IsSuccess}"); + if (deleteResult.Succeeded == false) { + return Result.Failure($"Error deleting user {identityUserToDelete.UserName} as part of cleanup {deleteResult}"); } - return Result.Success(); + return Result.Failure($"At least one part of the user creation failed - createResult: {createResult.IsSuccess} addRolesToUserResult: {addRolesToUserResult.IsSuccess} addClaimsToUserResult: {addClaimsToUserResult.IsSuccess}"); } + private async Task CreateUser(ApplicationUser newIdentityUser) { var createResult = await this.UserManager.CreateAsync(newIdentityUser);