U1 mail-foundation (backend): verzender, opties, sjabloon, SiteSettings met toegestane afzenderlijst
IEmailSender met MailKit-implementatie (StartTls, timeout, fouten als waarde), opties met startvalidatie, nl/en sjabloon, taalkeuze, tabel SiteSettings (additieve migratie), SiteSettings-API (Beheerder leest, Eigenaar wijzigt) en emailEnabled/defaultLanguage in de capabilities. Zonder SMTP-host verandert er niets. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace SlpModularCms.Core.Email;
|
||||
|
||||
/// <summary>
|
||||
/// Start-up validation of the mail configuration. Only runs checks when mail is enabled (an SMTP host
|
||||
/// is set); a host without mail configuration starts exactly as before. Messages never contain the
|
||||
/// credentials.
|
||||
/// </summary>
|
||||
public sealed class EmailOptionsValidator(IOptions<AppMailOptions> app) : IValidateOptions<EmailOptions>
|
||||
{
|
||||
public ValidateOptionsResult Validate(string? name, EmailOptions options)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(options.Smtp.Host))
|
||||
return ValidateOptionsResult.Success; // mail disabled
|
||||
|
||||
var failures = new List<string>();
|
||||
|
||||
if (!EmailAddressRules.IsValidAddress(options.FromAddress))
|
||||
failures.Add("Email:FromAddress must be a valid e-mail address.");
|
||||
if (!EmailAddressRules.IsValidDisplayName(options.FromName))
|
||||
failures.Add($"Email:FromName is required (1..{EmailAddressRules.MaxDisplayNameLength} characters, no control characters).");
|
||||
if (options.Smtp.Port is < 1 or > 65535)
|
||||
failures.Add("Email:Smtp:Port must be between 1 and 65535.");
|
||||
if (options.Smtp.TimeoutSeconds is < 1 or > 60)
|
||||
failures.Add("Email:Smtp:TimeoutSeconds must be between 1 and 60.");
|
||||
if (!string.IsNullOrEmpty(options.Smtp.Username) && string.IsNullOrEmpty(options.Smtp.Password))
|
||||
failures.Add("Email:Smtp:Password is required when Email:Smtp:Username is set.");
|
||||
if (!string.IsNullOrEmpty(options.BrandColor) && !EmailAddressRules.IsValidColour(options.BrandColor))
|
||||
failures.Add("Email:BrandColor must look like #rrggbb.");
|
||||
foreach (var allowed in options.AllowedFromAddresses)
|
||||
{
|
||||
if (!EmailAddressRules.IsValidAddress(allowed))
|
||||
failures.Add("Email:AllowedFromAddresses contains an invalid e-mail address.");
|
||||
}
|
||||
|
||||
var baseUrl = app.Value.PublicBaseUrl;
|
||||
if (!IsValidPublicBaseUrl(baseUrl))
|
||||
failures.Add("App:PublicBaseUrl must be an absolute https URL (http only for localhost) without a path.");
|
||||
if (!MailLanguages.IsSupported(app.Value.DefaultLanguage))
|
||||
failures.Add("App:DefaultLanguage must be 'nl' or 'en'.");
|
||||
|
||||
if (!string.IsNullOrEmpty(options.LogoUrl) && !IsValidLogoUrl(options.LogoUrl))
|
||||
failures.Add("Email:LogoUrl must be an https URL or a path starting with '/'.");
|
||||
|
||||
return failures.Count == 0 ? ValidateOptionsResult.Success : ValidateOptionsResult.Fail(failures);
|
||||
}
|
||||
|
||||
public static bool IsValidPublicBaseUrl(string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value) || !Uri.TryCreate(value, UriKind.Absolute, out var uri)) return false;
|
||||
var local = uri.IsLoopback;
|
||||
if (uri.Scheme != Uri.UriSchemeHttps && !(local && uri.Scheme == Uri.UriSchemeHttp)) return false;
|
||||
return (uri.AbsolutePath == "/" || uri.AbsolutePath == "") && string.IsNullOrEmpty(uri.Query) && string.IsNullOrEmpty(uri.Fragment)
|
||||
&& string.IsNullOrEmpty(uri.UserInfo);
|
||||
}
|
||||
|
||||
private static bool IsValidLogoUrl(string value)
|
||||
{
|
||||
if (value.StartsWith('/') && !value.StartsWith("//") && !value.Any(char.IsControl)) return true;
|
||||
return Uri.TryCreate(value, UriKind.Absolute, out var uri) && uri.Scheme == Uri.UriSchemeHttps;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user