using Khaemhat.Core.Security; namespace Khaemhat.Application.Security; /// /// Who performs an action: an instance account (super user or administrator) or a company user. Services check the /// actor; the interface only hides what the actor cannot do. /// /// The user name, written in the audit columns and logs. /// The level. /// The instance account, for super user and administrators. /// The company user (or the user an administrator is linked to). /// The company the actor works in. public sealed record Actor(string UserName, UserLevel Level, long? SystemUserId, long? CompanyUserId, string? CompanyCode) { /// The instance itself: bootstrap, scheduled jobs. public static Actor System { get; } = new("system", UserLevel.SuperUser, null, null, null); /// Whether the actor is an instance account. public bool IsInstanceAccount => Level >= UserLevel.Administrator; /// Whether the actor is the super user. public bool IsSuperUser => Level == UserLevel.SuperUser; } /// An action the actor may not perform. is one of . public sealed class AccessDeniedException : Exception { /// Creates the exception. public AccessDeniedException() : this(SecurityErrors.PermissionDenied, "Access denied.") { } /// Creates the exception. /// The message. public AccessDeniedException(string message) : this(SecurityErrors.PermissionDenied, message) { } /// Creates the exception. /// The message. /// The cause. public AccessDeniedException(string message, Exception innerException) : base(message, innerException) => Code = SecurityErrors.PermissionDenied; /// Creates the exception. /// The error code. /// The message. public AccessDeniedException(string code, string message) : base(message) => Code = code; /// The error code. public string Code { get; } } /// A request the rules refuse (policy, duplicates); holds error codes for the interface. public sealed class ValidationFailedException : Exception { /// Creates the exception. public ValidationFailedException() : this([]) { } /// Creates the exception. /// The message. public ValidationFailedException(string message) : base(message) => Errors = [message]; /// Creates the exception. /// The message. /// The cause. public ValidationFailedException(string message, Exception innerException) : base(message, innerException) => Errors = [message]; /// Creates the exception. /// The error codes. public ValidationFailedException(IReadOnlyList errors) : base("The request is not valid: " + string.Join(", ", errors ?? [])) => Errors = errors ?? []; /// The error codes. public IReadOnlyList Errors { get; } }