2026-04-13 21:36:00 -03:00
|
|
|
namespace SIGCM2.Domain.Entities;
|
|
|
|
|
|
|
|
|
|
public sealed class Usuario
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; }
|
|
|
|
|
public string Username { get; }
|
|
|
|
|
public string PasswordHash { get; }
|
|
|
|
|
public string Nombre { get; }
|
|
|
|
|
public string Apellido { get; }
|
|
|
|
|
public string? Email { get; }
|
|
|
|
|
public string Rol { get; }
|
|
|
|
|
public string PermisosJson { get; }
|
|
|
|
|
public bool Activo { get; }
|
|
|
|
|
|
2026-04-15 17:36:46 -03:00
|
|
|
// UDT-008: new properties
|
|
|
|
|
public DateTime? FechaModificacion { get; }
|
|
|
|
|
public DateTime? UltimoLogin { get; }
|
|
|
|
|
public bool MustChangePassword { get; }
|
|
|
|
|
|
2026-04-13 21:36:00 -03:00
|
|
|
public Usuario(
|
|
|
|
|
int id,
|
|
|
|
|
string username,
|
|
|
|
|
string passwordHash,
|
|
|
|
|
string nombre,
|
|
|
|
|
string apellido,
|
|
|
|
|
string? email,
|
|
|
|
|
string rol,
|
|
|
|
|
string permisosJson,
|
2026-04-15 17:36:46 -03:00
|
|
|
bool activo,
|
|
|
|
|
DateTime? fechaModificacion = null,
|
|
|
|
|
DateTime? ultimoLogin = null,
|
|
|
|
|
bool mustChangePassword = false)
|
2026-04-13 21:36:00 -03:00
|
|
|
{
|
|
|
|
|
Id = id;
|
|
|
|
|
Username = username;
|
|
|
|
|
PasswordHash = passwordHash;
|
|
|
|
|
Nombre = nombre;
|
|
|
|
|
Apellido = apellido;
|
|
|
|
|
Email = email;
|
|
|
|
|
Rol = rol;
|
|
|
|
|
PermisosJson = permisosJson;
|
|
|
|
|
Activo = activo;
|
2026-04-15 17:36:46 -03:00
|
|
|
FechaModificacion = fechaModificacion;
|
|
|
|
|
UltimoLogin = ultimoLogin;
|
|
|
|
|
MustChangePassword = mustChangePassword;
|
2026-04-13 21:36:00 -03:00
|
|
|
}
|
2026-04-15 10:47:48 -03:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Factory for creating a new user (no Id — DB assigns via IDENTITY).
|
2026-04-15 17:36:46 -03:00
|
|
|
/// Defaults: Activo=true, PermisosJson="[]", MustChangePassword=false.
|
2026-04-15 10:47:48 -03:00
|
|
|
/// </summary>
|
|
|
|
|
public static Usuario ForCreation(
|
|
|
|
|
string username,
|
|
|
|
|
string passwordHash,
|
|
|
|
|
string nombre,
|
|
|
|
|
string apellido,
|
|
|
|
|
string? email,
|
|
|
|
|
string rol)
|
|
|
|
|
{
|
|
|
|
|
return new Usuario(
|
|
|
|
|
id: 0,
|
|
|
|
|
username: username,
|
|
|
|
|
passwordHash: passwordHash,
|
|
|
|
|
nombre: nombre,
|
|
|
|
|
apellido: apellido,
|
|
|
|
|
email: email,
|
|
|
|
|
rol: rol,
|
|
|
|
|
permisosJson: "[]",
|
2026-04-15 17:36:46 -03:00
|
|
|
activo: true,
|
|
|
|
|
fechaModificacion: null,
|
|
|
|
|
ultimoLogin: null,
|
|
|
|
|
mustChangePassword: false);
|
2026-04-15 10:47:48 -03:00
|
|
|
}
|
2026-04-15 17:36:46 -03:00
|
|
|
|
|
|
|
|
// ── UDT-008: copy-with factory methods ────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a new instance with updated profile fields.
|
|
|
|
|
/// Sets FechaModificacion = UtcNow. Username and PasswordHash are immutable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Usuario WithUpdatedProfile(string nombre, string apellido, string? email, string rol, bool activo)
|
|
|
|
|
=> new(
|
|
|
|
|
id: Id,
|
|
|
|
|
username: Username,
|
|
|
|
|
passwordHash: PasswordHash,
|
|
|
|
|
nombre: nombre,
|
|
|
|
|
apellido: apellido,
|
|
|
|
|
email: email,
|
|
|
|
|
rol: rol,
|
|
|
|
|
permisosJson: PermisosJson,
|
|
|
|
|
activo: activo,
|
|
|
|
|
fechaModificacion: DateTime.UtcNow,
|
|
|
|
|
ultimoLogin: UltimoLogin,
|
|
|
|
|
mustChangePassword: MustChangePassword);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a new instance with a new password hash and mustChangePassword flag.
|
|
|
|
|
/// Sets FechaModificacion = UtcNow.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Usuario WithNewPasswordHash(string hash, bool mustChangePassword)
|
|
|
|
|
=> new(
|
|
|
|
|
id: Id,
|
|
|
|
|
username: Username,
|
|
|
|
|
passwordHash: hash,
|
|
|
|
|
nombre: Nombre,
|
|
|
|
|
apellido: Apellido,
|
|
|
|
|
email: Email,
|
|
|
|
|
rol: Rol,
|
|
|
|
|
permisosJson: PermisosJson,
|
|
|
|
|
activo: Activo,
|
|
|
|
|
fechaModificacion: DateTime.UtcNow,
|
|
|
|
|
ultimoLogin: UltimoLogin,
|
|
|
|
|
mustChangePassword: mustChangePassword);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a new instance with only the MustChangePassword flag changed.
|
|
|
|
|
/// Sets FechaModificacion = UtcNow.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Usuario WithMustChangePassword(bool value)
|
|
|
|
|
=> new(
|
|
|
|
|
id: Id,
|
|
|
|
|
username: Username,
|
|
|
|
|
passwordHash: PasswordHash,
|
|
|
|
|
nombre: Nombre,
|
|
|
|
|
apellido: Apellido,
|
|
|
|
|
email: Email,
|
|
|
|
|
rol: Rol,
|
|
|
|
|
permisosJson: PermisosJson,
|
|
|
|
|
activo: Activo,
|
|
|
|
|
fechaModificacion: DateTime.UtcNow,
|
|
|
|
|
ultimoLogin: UltimoLogin,
|
|
|
|
|
mustChangePassword: value);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a new instance with only UltimoLogin updated.
|
|
|
|
|
/// Does NOT touch FechaModificacion.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Usuario WithUltimoLogin(DateTime utcNow)
|
|
|
|
|
=> new(
|
|
|
|
|
id: Id,
|
|
|
|
|
username: Username,
|
|
|
|
|
passwordHash: PasswordHash,
|
|
|
|
|
nombre: Nombre,
|
|
|
|
|
apellido: Apellido,
|
|
|
|
|
email: Email,
|
|
|
|
|
rol: Rol,
|
|
|
|
|
permisosJson: PermisosJson,
|
|
|
|
|
activo: Activo,
|
|
|
|
|
fechaModificacion: FechaModificacion,
|
|
|
|
|
ultimoLogin: utcNow,
|
|
|
|
|
mustChangePassword: MustChangePassword);
|
2026-04-13 21:36:00 -03:00
|
|
|
}
|