[FA-10] Adds user service and authentication service
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.UserService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.UserService.Models.Database;
|
||||
using Microsoft.EntityFrameworkCore; // Add this line to include the UserModel
|
||||
|
||||
namespace FictionArchive.Service.UserService.Services.EventHandlers;
|
||||
|
||||
public class AuthUserAddedEventHandler : IIntegrationEventHandler<AuthUserAddedEvent>
|
||||
{
|
||||
private readonly UserManagementService _userManagementService;
|
||||
private readonly ILogger<AuthUserAddedEventHandler> _logger;
|
||||
|
||||
public AuthUserAddedEventHandler(UserServiceDbContext dbContext, ILogger<AuthUserAddedEventHandler> logger, UserManagementService userManagementService)
|
||||
{
|
||||
_logger = logger;
|
||||
_userManagementService = userManagementService;
|
||||
}
|
||||
|
||||
public async Task Handle(AuthUserAddedEvent @event)
|
||||
{
|
||||
await _userManagementService.RegisterUser(@event.EventUserUsername, @event.EventUserEmail, @event.OAuthProviderId, @event.InviterOAuthProviderId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using FictionArchive.Service.UserService.Models.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.UserService.Services;
|
||||
|
||||
public class UserManagementService
|
||||
{
|
||||
private readonly ILogger<UserManagementService> _logger;
|
||||
private readonly UserServiceDbContext _dbContext;
|
||||
|
||||
public UserManagementService(UserServiceDbContext dbContext, ILogger<UserManagementService> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<User> RegisterUser(string username, string email, string oAuthProviderId,
|
||||
string? inviterOAuthProviderId)
|
||||
{
|
||||
var newUser = new User();
|
||||
User? inviter =
|
||||
await _dbContext.Users.FirstOrDefaultAsync(user => user.OAuthProviderId == inviterOAuthProviderId);
|
||||
if (inviter == null && inviterOAuthProviderId != null)
|
||||
{
|
||||
_logger.LogCritical(
|
||||
"A user with OAuthProviderId {OAuthProviderId} was marked as having inviter with OAuthProviderId {inviterOAuthProviderId}, but no user was found with that value.",
|
||||
inviterOAuthProviderId, inviterOAuthProviderId);
|
||||
newUser.Disabled = true;
|
||||
}
|
||||
|
||||
newUser.Username = username;
|
||||
newUser.Email = email;
|
||||
newUser.OAuthProviderId = oAuthProviderId;
|
||||
|
||||
_dbContext.Users.Add(newUser); // Add the new user to the DbContext
|
||||
await _dbContext.SaveChangesAsync(); // Save changes to the database
|
||||
|
||||
return newUser;
|
||||
}
|
||||
|
||||
public IQueryable<User> GetUsers()
|
||||
{
|
||||
return _dbContext.Users.AsQueryable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FictionArchive.Service.Shared.Services.Database;
|
||||
using FictionArchive.Service.UserService.Models.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.UserService.Services;
|
||||
|
||||
public class UserServiceDbContext : FictionArchiveDbContext
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
public UserServiceDbContext(DbContextOptions options, ILogger<UserServiceDbContext> logger) : base(options, logger)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user