Skip to content

Commit

Permalink
Create user using command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasson committed Oct 15, 2023
1 parent 24011a9 commit 84dc5b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/Quarter/Services/UserAuthorizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,27 +73,28 @@ public class UserAuthorizationService : IUserAuthorizationService
private readonly IUserRepository _userRepository;
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly ILogger<UserAuthorizationService> _logger;
private readonly ICommandHandler _commandHandler;
private readonly IOptions<AuthOptions> _authOptions;

public UserAuthorizationService(
AuthenticationStateProvider authenticationStateProvider,
IRepositoryFactory repositoryFactory,
ILogger<UserAuthorizationService> logger,
IOptions<AuthOptions> authOptions)
ICommandHandler commandHandler,
IOptions<AuthOptions> authOptions,
ILogger<UserAuthorizationService> logger)
{
_userRepository = repositoryFactory.UserRepository();
_authenticationStateProvider = authenticationStateProvider;
_logger = logger;
_commandHandler = commandHandler;
_authOptions = authOptions;
}

public async Task<AuthorizedResult> AuthorizeOrCreateUserAsync(string email, CancellationToken ct)
{
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)
{
Expand All @@ -104,10 +105,18 @@ public async Task<AuthorizedResult> 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<UserRole>.Empty);

await _commandHandler.ExecuteAsync(command, OperationContext.None, ct);
return await tryWithExistingUser();
}

async Task<AuthorizedResult> tryWithExistingUser()
{
var user = await _userRepository.GetUserByEmailAsync(email, ct);
_logger.LogInformation("Successfully authorized user {Email} at login", email);
return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,19 +129,21 @@ public async Task ItShouldCreateUser()

public class TestCase
{

private readonly IRepositoryFactory _repositoryFactory = new InMemoryRepositoryFactory();
protected readonly UserAuthorizationService Service;
private readonly TestAuthenticationStateProvider _authenticationStateProvider;
private readonly AuthOptions _authOptions = new () { OpenUserRegistration = false };

protected TestCase()
{
var commandHandler = new CommandHandler(_repositoryFactory);
_authenticationStateProvider= new TestAuthenticationStateProvider();

Service = new UserAuthorizationService(_authenticationStateProvider,
_repositoryFactory,
NullLogger<UserAuthorizationService>.Instance,
Options.Create(_authOptions));
commandHandler,
Options.Create(_authOptions),
NullLogger<UserAuthorizationService>.Instance);
}

protected void SetOpenRegistration(bool openRegistration)
Expand Down

0 comments on commit 84dc5b1

Please sign in to comment.