This commit is contained in:
Veljko Tosic
2026-02-12 22:04:48 +01:00
parent 5ecf8435f4
commit 8253e8bc3f
22 changed files with 250 additions and 126 deletions

View File

@@ -0,0 +1,64 @@
using AipsCore.Application.Common.Authentication;
using AipsCore.Domain.Common.Validation;
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.Validation;
using AipsCore.Infrastructure.Persistence.User.Mappers;
using Microsoft.AspNetCore.Identity;
namespace AipsCore.Infrastructure.Persistence.Authentication;
public class EfAuthService : IAuthService
{
private readonly UserManager<User.User> _userManager;
public EfAuthService(UserManager<User.User> userManager)
{
_userManager = userManager;
}
public async Task SignUpWithPasswordAsync(Domain.Models.User.User user, string password, CancellationToken cancellationToken = default)
{
var entity = user.MapToEntity();
var result = await _userManager.CreateAsync(entity, password);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
throw new Exception($"User registration failed: {errors}");
}
await _userManager.AddToRoleAsync(entity, UserRole.User.Name);
}
public async Task<LoginResult> LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default)
{
var entity = await _userManager.FindByEmailAsync(email);
if (entity is null)
{
throw new ValidationException(UserErrors.InvalidCredentials());
}
var isPasswordValid = await _userManager.CheckPasswordAsync(entity, password);
if (!isPasswordValid)
{
throw new ValidationException(UserErrors.InvalidCredentials());
}
var roles = new List<UserRole>();
var rolesNames = await _userManager.GetRolesAsync(entity);
foreach (var roleName in rolesNames)
{
var role = UserRole.FromString(roleName);
if (role is null) throw new Exception($"Role {roleName} not found");
roles.Add(role);
}
return new LoginResult(entity.MapToModel(), roles);
}
}

View File

@@ -2,6 +2,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Domain.Models.User.External;
using Microsoft.IdentityModel.Tokens;
namespace AipsCore.Infrastructure.Persistence.Authentication;
@@ -15,7 +16,7 @@ public class JwtTokenProvider : ITokenProvider
_jwtSettings = jwtSettings;
}
public string Generate(Domain.Models.User.User user, IList<string> roles)
public string Generate(Domain.Models.User.User user, IList<UserRole> roles)
{
var claims = new List<Claim>
{
@@ -25,7 +26,7 @@ public class JwtTokenProvider : ITokenProvider
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
claims.Add(new Claim(ClaimTypes.Role, role.Name));
}
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key));

View File

@@ -0,0 +1,39 @@
namespace AipsCore.Infrastructure.Persistence.User.Mappers;
public static class UserMappers
{
public static Domain.Models.User.User MapToModel(this User entity)
{
return Domain.Models.User.User.Create(
entity.Id.ToString(),
entity.Email!,
entity.UserName!,
entity.CreatedAt,
entity.DeletedAt
);
}
public static User MapToEntity(this Domain.Models.User.User model)
{
return new User
{
Id = new Guid(model.Id.IdValue),
Email = model.Email.EmailValue,
NormalizedEmail = model.Email.EmailValue.ToUpperInvariant(),
UserName = model.Username.UsernameValue,
NormalizedUserName = model.Username.UsernameValue.ToUpperInvariant(),
CreatedAt = model.CreatedAt.CreatedAtValue,
DeletedAt = model.DeletedAt.DeletedAtValue
};
}
public static void UpdateEntity(this User entity, Domain.Models.User.User model)
{
entity.Email = model.Email.EmailValue;
entity.NormalizedEmail = model.Email.EmailValue.ToUpperInvariant();
entity.UserName = model.Username.UsernameValue;
entity.NormalizedUserName = model.Username.UsernameValue.ToUpperInvariant();
entity.CreatedAt = model.CreatedAt.CreatedAtValue;
entity.DeletedAt = model.DeletedAt.DeletedAtValue;
}
}

View File

@@ -4,88 +4,31 @@ using AipsCore.Domain.Models.User.Validation;
using AipsCore.Domain.Models.User.ValueObjects;
using AipsCore.Infrastructure.Persistence.Abstract;
using AipsCore.Infrastructure.Persistence.Db;
using AipsCore.Infrastructure.Persistence.User.Mappers;
using Microsoft.AspNetCore.Identity;
namespace AipsCore.Infrastructure.Persistence.User;
public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId, User>, IUserRepository
{
private readonly UserManager<User> _userManager;
public UserRepository(AipsDbContext context, UserManager<User> userManager)
: base(context)
{
_userManager = userManager;
}
protected override Domain.Models.User.User MapToModel(User entity)
{
return Domain.Models.User.User.Create(
entity.Id.ToString(),
entity.Email,
entity.UserName,
entity.CreatedAt,
entity.DeletedAt
);
return entity.MapToModel();
}
protected override User MapToEntity(Domain.Models.User.User model)
{
return new User
{
Id = new Guid(model.Id.IdValue),
Email = model.Email.EmailValue,
NormalizedEmail = model.Email.EmailValue.ToUpperInvariant(),
UserName = model.Username.UsernameValue,
NormalizedUserName = model.Username.UsernameValue.ToUpperInvariant(),
CreatedAt = model.CreatedAt.CreatedAtValue,
DeletedAt = model.DeletedAt.DeletedAtValue
};
return model.MapToEntity();
}
protected override void UpdateEntity(User entity, Domain.Models.User.User model)
{
entity.Email = model.Email.EmailValue;
entity.NormalizedEmail = model.Email.EmailValue.ToUpperInvariant();
entity.UserName = model.Username.UsernameValue;
entity.NormalizedUserName = model.Username.UsernameValue.ToUpperInvariant();
entity.CreatedAt = model.CreatedAt.CreatedAtValue;
entity.DeletedAt = model.DeletedAt.DeletedAtValue;
}
public async Task SignUpWithPasswordAsync(Domain.Models.User.User user, string password, CancellationToken cancellationToken = default)
{
var entity = MapToEntity(user);
var result = await _userManager.CreateAsync(entity, password);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
throw new Exception($"User registration failed: {errors}");
}
await _userManager.AddToRoleAsync(entity, UserRole.User.Name);
}
public async Task<LoginResult> LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default)
{
var entity = await _userManager.FindByEmailAsync(email);
if (entity is null)
{
throw new ValidationException(UserErrors.LoginErrorUserNotFoundByEmail(email));
}
var isPasswordValid = await _userManager.CheckPasswordAsync(entity, password);
if (!isPasswordValid)
{
throw new ValidationException(UserErrors.LoginErrorIncorrectPassword());
}
var roles = await _userManager.GetRolesAsync(entity);
return new LoginResult(MapToModel(entity), roles);
entity.UpdateEntity(model);
}
}