diff --git a/src/Quarter/Services/UserAuthorizationService.cs b/src/Quarter/Services/UserAuthorizationService.cs index 88a0aab..eab1bcc 100644 --- a/src/Quarter/Services/UserAuthorizationService.cs +++ b/src/Quarter/Services/UserAuthorizationService.cs @@ -7,8 +7,8 @@ using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using Quarter.Auth; using Quarter.Core.Auth; +using Quarter.Core.Commands; using Quarter.Core.Exceptions; using Quarter.Core.Models; using Quarter.Core.Options; @@ -73,17 +73,20 @@ public class UserAuthorizationService : IUserAuthorizationService private readonly IUserRepository _userRepository; private readonly AuthenticationStateProvider _authenticationStateProvider; private readonly ILogger _logger; + private readonly ICommandHandler _commandHandler; private readonly IOptions _authOptions; public UserAuthorizationService( AuthenticationStateProvider authenticationStateProvider, IRepositoryFactory repositoryFactory, - ILogger logger, - IOptions authOptions) + ICommandHandler commandHandler, + IOptions authOptions, + ILogger logger) { _userRepository = repositoryFactory.UserRepository(); _authenticationStateProvider = authenticationStateProvider; _logger = logger; + _commandHandler = commandHandler; _authOptions = authOptions; } @@ -91,9 +94,7 @@ public async Task AuthorizeOrCreateUserAsync(string email, Can { try { - var user = await _userRepository.GetUserByEmailAsync(email, ct); - _logger.LogInformation("Successfully authorized user {Email} at login", email); - return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray()); + return await tryWithExistingUser(); } catch (NotFoundException) { @@ -104,10 +105,18 @@ public async Task AuthorizeOrCreateUserAsync(string email, Can } _logger.LogInformation("Unauthorized user {Email} tried to login, creating new user and granting access", email); - var user = User.StandardUser(new Email(email)); - user = await _userRepository.CreateAsync(user, ct); - return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray()); + var command = new AddUserCommand(new Email(email), ArraySegment.Empty); + + await _commandHandler.ExecuteAsync(command, OperationContext.None, ct); + return await tryWithExistingUser(); + } + + async Task tryWithExistingUser() + { + var user = await _userRepository.GetUserByEmailAsync(email, ct); + _logger.LogInformation("Successfully authorized user {Email} at login", email); + return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray()); } } diff --git a/test/unit/Quarter.UnitTest/Services/UserAuthorizationServiceTest.cs b/test/unit/Quarter.UnitTest/Services/UserAuthorizationServiceTest.cs index 0e26d87..ec9d4d3 100644 --- a/test/unit/Quarter.UnitTest/Services/UserAuthorizationServiceTest.cs +++ b/test/unit/Quarter.UnitTest/Services/UserAuthorizationServiceTest.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using NUnit.Framework; using Quarter.Core.Auth; +using Quarter.Core.Commands; using Quarter.Core.Exceptions; using Quarter.Core.Models; using Quarter.Core.Options; @@ -128,7 +129,6 @@ public async Task ItShouldCreateUser() public class TestCase { - private readonly IRepositoryFactory _repositoryFactory = new InMemoryRepositoryFactory(); protected readonly UserAuthorizationService Service; private readonly TestAuthenticationStateProvider _authenticationStateProvider; @@ -136,11 +136,14 @@ public class TestCase protected TestCase() { + var commandHandler = new CommandHandler(_repositoryFactory); _authenticationStateProvider= new TestAuthenticationStateProvider(); + Service = new UserAuthorizationService(_authenticationStateProvider, _repositoryFactory, - NullLogger.Instance, - Options.Create(_authOptions)); + commandHandler, + Options.Create(_authOptions), + NullLogger.Instance); } protected void SetOpenRegistration(bool openRegistration)