femto-backend/Femto.Modules.Auth/Models/UserIdentity.cs
2025-05-15 17:47:20 +02:00

60 lines
1.3 KiB
C#

using System.Text;
using System.Text.Unicode;
using Femto.Common.Domain;
using Femto.Modules.Auth.Models.Events;
using Geralt;
namespace Femto.Modules.Auth.Models;
internal class UserIdentity : Entity
{
public Guid Id { get; private set; }
public string Username { get; private set; }
public UserPassword Password { get; private set; }
public ICollection<UserSession> Sessions { get; private set; } = [];
private UserIdentity() { }
public UserIdentity(string username)
{
this.Id = Guid.CreateVersion7();
this.Username = username;
this.AddDomainEvent(new UserWasCreatedEvent(this));
}
public UserIdentity WithPassword(string password)
{
this.SetPassword(password);
return this;
}
public void SetPassword(string password)
{
this.Password = new UserPassword(password);
}
public bool HasPassword(string requestPassword)
{
if (this.Password is null)
{
return false;
}
return this.Password.Check(requestPassword);
}
public UserSession StartNewSession()
{
var session = UserSession.Create();
this.Sessions.Add(session);
return session;
}
}
public class SetPasswordError(string message, Exception inner) : DomainError(message, inner);