Spaces:
Runtime error
Runtime error
Download src/SCM.Domain/Users/User.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Domain/Users/User.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Domain/Users/User.cs
-
curl -L -o User.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Domain/Users/User.cs
2.05 kB
| 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; | |
| } | |
| } | |