using Khaemhat.Core.Entities;
namespace Khaemhat.Core.Security;
///
/// The three access levels of SPEC §7.1, ordered: a higher value can do everything a lower one can.
/// rows hold or ; company users are
/// .
///
public enum UserLevel
{
/// A company user, limited by the permissions of the roles.
Standard = 0,
/// An instance administrator, created by the super user.
Administrator = 1,
/// The only super user, created from the container variables at first start.
SuperUser = 2,
}
/// The kind of a company user.
public enum UserKind
{
/// A person who signs in with a password.
Human = 0,
/// A technical user for the API: no password, used only through API keys.
Technical = 1,
}
///
/// The credential fields shared by instance accounts (sys_users) and company users (users), so that
/// password, lockout, TOTP and recovery rules are written once.
///
public interface IAccount
{
/// The identifier.
long Id { get; }
/// The sign-in name, unique in its database (compared case-insensitively, stored lower-case).
string UserName { get; set; }
/// The name shown in the interface.
string DisplayName { get; set; }
/// The e-mail address for recovery and notifications.
string? Email { get; set; }
/// The password hash (pbkdf2-sha256$...); for technical users.
string? PasswordHash { get; set; }
/// The previous password hashes, newest first, as a JSON array (policy: no reuse of the last five).
string? PasswordHistory { get; set; }
/// When the password was last set.
DateTime? PasswordChangedAt { get; set; }
/// Whether the next sign-in must change the password (first access, temporary password, expiry).
bool MustChangePassword { get; set; }
/// When a temporary password stops working; for a normal password.
DateTime? TemporaryPasswordExpiresAt { get; set; }
/// Consecutive failed sign-ins since the last success.
int FailedLoginCount { get; set; }
/// The account is locked until this moment after too many failures.
DateTime? LockedUntil { get; set; }
/// The TOTP secret, encrypted with the instance key.
string? TotpSecret { get; set; }
/// Whether the two-step verification is active.
bool TotpEnabled { get; set; }
/// The last accepted TOTP time step: a code is never accepted twice.
long? TotpLastStep { get; set; }
/// The SHA-256 hashes of the unused recovery codes, as a JSON array.
string? TotpRecoveryCodes { get; set; }
/// The SHA-256 hash of the pending password-recovery token.
string? RecoveryTokenHash { get; set; }
/// When the recovery token expires.
DateTime? RecoveryTokenExpiresAt { get; set; }
/// Whether the account can sign in.
bool IsActive { get; set; }
/// The last successful sign-in.
DateTime? LastLoginAt { get; set; }
}
/// An instance account (sys_users): the super user or an administrator (SPEC §7.1).
public sealed class SystemUser : Entity, IAccount
{
///
public string UserName { get; set; } = string.Empty;
///
public string DisplayName { get; set; } = string.Empty;
///
public string? Email { get; set; }
/// or .
public UserLevel Level { get; set; }
///
public string? PasswordHash { get; set; }
///
public string? PasswordHistory { get; set; }
///
public DateTime? PasswordChangedAt { get; set; }
///
public bool MustChangePassword { get; set; }
///
public DateTime? TemporaryPasswordExpiresAt { get; set; }
///
public int FailedLoginCount { get; set; }
///
public DateTime? LockedUntil { get; set; }
///
public string? TotpSecret { get; set; }
///
public bool TotpEnabled { get; set; }
///
public long? TotpLastStep { get; set; }
///
public string? TotpRecoveryCodes { get; set; }
///
public string? RecoveryTokenHash { get; set; }
///
public DateTime? RecoveryTokenExpiresAt { get; set; }
///
public bool IsActive { get; set; } = true;
///
public DateTime? LastLoginAt { get; set; }
/// The company of the linked user, for an administrator who works as a company user without a second sign-in.
public string? LinkedCompanyCode { get; set; }
/// The linked company user.
public long? LinkedUserId { get; set; }
}
/// A company user (users in the company database).
public sealed class CompanyUser : Entity, IAccount
{
///
public string UserName { get; set; } = string.Empty;
///
public string DisplayName { get; set; } = string.Empty;
///
public string? Email { get; set; }
/// A person or a technical user for the API.
public UserKind Kind { get; set; }
///
public string? PasswordHash { get; set; }
///
public string? PasswordHistory { get; set; }
///
public DateTime? PasswordChangedAt { get; set; }
///
public bool MustChangePassword { get; set; }
///
public DateTime? TemporaryPasswordExpiresAt { get; set; }
///
public int FailedLoginCount { get; set; }
///
public DateTime? LockedUntil { get; set; }
///
public string? TotpSecret { get; set; }
///
public bool TotpEnabled { get; set; }
/// Whether this user must set up the two-step verification (configurable per user).
public bool TotpRequired { get; set; }
///
public long? TotpLastStep { get; set; }
///
public string? TotpRecoveryCodes { get; set; }
///
public string? RecoveryTokenHash { get; set; }
///
public DateTime? RecoveryTokenExpiresAt { get; set; }
///
public bool IsActive { get; set; } = true;
///
public DateTime? LastLoginAt { get; set; }
/// Sees only the customers assigned to the user (agents).
public bool OwnCustomersOnly { get; set; }
/// Sees costs and margins.
public bool CanSeeCosts { get; set; }
/// Can export lists and reports.
public bool CanExport { get; set; }
/// Can use the API.
public bool CanUseApi { get; set; }
}