Základní backend: migrace, autentizace JWT, role, Swagger
- Helpdesk.Migrator (DbUp) aplikuje db/migrations, evidence v schema_version - Domain/Application/Infrastructure/Api podle Clean Architecture - registrace, ověření e-mailu, login, refresh s rotací, logout, reset hesla - JWT podle RFC 7519 evidované jen podle jti v auth_token - správa uživatelů a rolí, číselníky, ProblemDetails, Swagger UI - Docker Compose (db, migrator, api), oddělené DB účty - notifikace mají cizí klíče na issue a proposal - unit testy domény a zabezpečení Closes #1, closes #2, closes #3, closes #4, closes #5, closes #9
This commit is contained in:
1 parent
fc76be7d69
commit
a51d386278
64 files changed
+4394
-1
No files matched your search
@@ -0,0 +1,82 @@
|
||||
namespace Helpdesk.Domain.Users;
|
||||
|
||||
/// <summary>
|
||||
/// Uživatelský účet (tabulka <c>app_user</c>). Entita záměrně nemá žádné kolekce
|
||||
/// (závady, události, …) – vazby se konfigurují jen ze závislé strany (kap. 12.3).
|
||||
/// </summary>
|
||||
public sealed class User : IHasCreatedAt
|
||||
{
|
||||
public const int MaxFailedLogins = 5;
|
||||
public static readonly TimeSpan LockoutDuration = TimeSpan.FromMinutes(15);
|
||||
|
||||
private User() { } // EF Core
|
||||
|
||||
public int Id { get; private set; }
|
||||
public string Email { get; private set; } = null!;
|
||||
public string EmailNormalized { get; private set; } = null!;
|
||||
public string PasswordHash { get; private set; } = null!;
|
||||
public string FirstName { get; private set; } = null!;
|
||||
public string LastName { get; private set; } = null!;
|
||||
/// <summary>Telefon v otevřené podobě; v DB je šifrovaný (AES-256-GCM, sloupec <c>phone_encrypted</c>).</summary>
|
||||
public string? Phone { get; private set; }
|
||||
public UserRole Role { get; private set; }
|
||||
public bool IsActive { get; private set; } = true;
|
||||
public bool EmailConfirmed { get; private set; }
|
||||
public bool EmailNotificationsEnabled { get; private set; } = true;
|
||||
public int FailedLoginCount { get; private set; }
|
||||
public DateTime? LockoutUntil { get; private set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public string FullName => $"{FirstName} {LastName}";
|
||||
|
||||
public static User Create(string email, string passwordHash, string firstName, string lastName, UserRole role) => new()
|
||||
{
|
||||
Email = email.Trim(),
|
||||
EmailNormalized = NormalizeEmail(email),
|
||||
PasswordHash = passwordHash,
|
||||
FirstName = firstName.Trim(),
|
||||
LastName = lastName.Trim(),
|
||||
Role = role,
|
||||
};
|
||||
|
||||
public static string NormalizeEmail(string email) => email.Trim().ToUpperInvariant();
|
||||
|
||||
public bool IsLockedOut(DateTime now) => LockoutUntil is { } until && until > now;
|
||||
|
||||
/// <summary>Neúspěšné přihlášení; po <see cref="MaxFailedLogins"/> pokusech se účet na 15 minut zamkne.</summary>
|
||||
public void RegisterFailedLogin(DateTime now)
|
||||
{
|
||||
FailedLoginCount++;
|
||||
if (FailedLoginCount >= MaxFailedLogins)
|
||||
{
|
||||
LockoutUntil = now + LockoutDuration;
|
||||
FailedLoginCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterSuccessfulLogin()
|
||||
{
|
||||
FailedLoginCount = 0;
|
||||
LockoutUntil = null;
|
||||
}
|
||||
|
||||
public void ConfirmEmail() => EmailConfirmed = true;
|
||||
|
||||
public void ChangePasswordHash(string passwordHash)
|
||||
{
|
||||
PasswordHash = passwordHash;
|
||||
RegisterSuccessfulLogin();
|
||||
}
|
||||
|
||||
public void UpdateProfile(string firstName, string lastName, string? phone, bool emailNotificationsEnabled)
|
||||
{
|
||||
FirstName = firstName.Trim();
|
||||
LastName = lastName.Trim();
|
||||
Phone = string.IsNullOrWhiteSpace(phone) ? null : phone.Trim();
|
||||
EmailNotificationsEnabled = emailNotificationsEnabled;
|
||||
}
|
||||
|
||||
public void ChangeRole(UserRole role) => Role = role;
|
||||
|
||||
public void SetActive(bool isActive) => IsActive = isActive;
|
||||
}
|
||||
Reference in new issue
Block a user