Modified user entity for identity
This commit is contained in:
@@ -1,21 +1,11 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
namespace AipsCore.Infrastructure.Persistence.User;
|
namespace AipsCore.Infrastructure.Persistence.User;
|
||||||
|
|
||||||
|
|
||||||
public class User
|
public class User : IdentityUser<Guid>
|
||||||
{
|
{
|
||||||
[Key]
|
|
||||||
public Guid Id { get; set; }
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
[MaxLength(255)]
|
|
||||||
public string Username { get; set; } = null!;
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
[MaxLength(255)]
|
|
||||||
public string Email { get; set; } = null!;
|
|
||||||
|
|
||||||
public DateTime CreatedAt { get; set; }
|
public DateTime CreatedAt { get; set; }
|
||||||
|
|
||||||
public DateTime? DeletedAt { get; set; }
|
public DateTime? DeletedAt { get; set; }
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
|
using AipsCore.Domain.Common.Validation;
|
||||||
using AipsCore.Domain.Models.User.External;
|
using AipsCore.Domain.Models.User.External;
|
||||||
|
using AipsCore.Domain.Models.User.Validation;
|
||||||
using AipsCore.Domain.Models.User.ValueObjects;
|
using AipsCore.Domain.Models.User.ValueObjects;
|
||||||
using AipsCore.Infrastructure.Persistence.Abstract;
|
using AipsCore.Infrastructure.Persistence.Abstract;
|
||||||
using AipsCore.Infrastructure.Persistence.Db;
|
using AipsCore.Infrastructure.Persistence.Db;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
namespace AipsCore.Infrastructure.Persistence.User;
|
namespace AipsCore.Infrastructure.Persistence.User;
|
||||||
|
|
||||||
public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId, User>, IUserRepository
|
public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId, User>, IUserRepository
|
||||||
{
|
{
|
||||||
public UserRepository(AipsDbContext context)
|
private readonly UserManager<User> _userManager;
|
||||||
|
|
||||||
|
public UserRepository(AipsDbContext context, UserManager<User> userManager)
|
||||||
: base(context)
|
: base(context)
|
||||||
{
|
{
|
||||||
|
_userManager = userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Domain.Models.User.User MapToModel(User entity)
|
protected override Domain.Models.User.User MapToModel(User entity)
|
||||||
@@ -17,7 +23,7 @@ public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId
|
|||||||
return Domain.Models.User.User.Create(
|
return Domain.Models.User.User.Create(
|
||||||
entity.Id.ToString(),
|
entity.Id.ToString(),
|
||||||
entity.Email,
|
entity.Email,
|
||||||
entity.Username,
|
entity.UserName,
|
||||||
entity.CreatedAt,
|
entity.CreatedAt,
|
||||||
entity.DeletedAt
|
entity.DeletedAt
|
||||||
);
|
);
|
||||||
@@ -29,7 +35,9 @@ public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId
|
|||||||
{
|
{
|
||||||
Id = new Guid(model.Id.IdValue),
|
Id = new Guid(model.Id.IdValue),
|
||||||
Email = model.Email.EmailValue,
|
Email = model.Email.EmailValue,
|
||||||
Username = model.Username.UsernameValue,
|
NormalizedEmail = model.Email.EmailValue.ToUpperInvariant(),
|
||||||
|
UserName = model.Username.UsernameValue,
|
||||||
|
NormalizedUserName = model.Username.UsernameValue.ToUpperInvariant(),
|
||||||
CreatedAt = model.CreatedAt.CreatedAtValue,
|
CreatedAt = model.CreatedAt.CreatedAtValue,
|
||||||
DeletedAt = model.DeletedAt.DeletedAtValue
|
DeletedAt = model.DeletedAt.DeletedAtValue
|
||||||
};
|
};
|
||||||
@@ -38,8 +46,47 @@ public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId
|
|||||||
protected override void UpdateEntity(User entity, Domain.Models.User.User model)
|
protected override void UpdateEntity(User entity, Domain.Models.User.User model)
|
||||||
{
|
{
|
||||||
entity.Email = model.Email.EmailValue;
|
entity.Email = model.Email.EmailValue;
|
||||||
entity.Username = model.Username.UsernameValue;
|
entity.NormalizedEmail = model.Email.EmailValue.ToUpperInvariant();
|
||||||
|
entity.UserName = model.Username.UsernameValue;
|
||||||
|
entity.NormalizedUserName = model.Username.UsernameValue.ToUpperInvariant();
|
||||||
entity.CreatedAt = model.CreatedAt.CreatedAtValue;
|
entity.CreatedAt = model.CreatedAt.CreatedAtValue;
|
||||||
entity.DeletedAt = model.DeletedAt.DeletedAtValue;
|
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}");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Realno nebitno, ali postoji infrastruktura ako treba da se koristi kasnije
|
||||||
|
//await _userManager.AddToRoleAsync(entity, "User");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user