Spaces:
Runtime error
Runtime error
File size: 2,047 Bytes
de8b914 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using SCM.Shared.Constants;
using SCM.Shared.Exceptions;
namespace SCM.Domain.Users;
/// <summary>Usuário do sistema com um papel (RBAC). Setters públicos para hidratação via Dapper.</summary>
public sealed class User
{
public Guid Id { get; set; }
public string Username { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string PasswordHash { get; set; } = string.Empty;
public string Role { get; set; } = Roles.Leitor;
public bool IsActive { get; set; } = true;
public DateTime CreatedAt { get; set; }
public static User Create(string username, string email, string role, string passwordHash, DateTime agora)
{
return new User
{
Id = Guid.NewGuid(),
Username = NormalizeUsername(username),
Email = NormalizeEmail(email),
Role = NormalizeRole(role),
PasswordHash = passwordHash,
IsActive = true,
CreatedAt = agora
};
}
public void Atualizar(string email, string role)
{
Email = NormalizeEmail(email);
Role = NormalizeRole(role);
}
public void DefinirAtivo(bool ativo) => IsActive = ativo;
public void RedefinirSenha(string passwordHash)
{
if (string.IsNullOrWhiteSpace(passwordHash))
throw new DomainException("O hash da senha é obrigatório.");
PasswordHash = passwordHash;
}
private static string NormalizeUsername(string v)
{
if (string.IsNullOrWhiteSpace(v)) throw new DomainException("O usuário é obrigatório.");
return v.Trim();
}
private static string NormalizeEmail(string v)
{
if (string.IsNullOrWhiteSpace(v) || !v.Contains('@')) throw new DomainException("E-mail inválido.");
return v.Trim();
}
private static string NormalizeRole(string v)
{
if (!Roles.All.Contains(v)) throw new DomainException($"Papel inválido. Use um de: {string.Join(", ", Roles.All)}.");
return v;
}
}
|