- Optionele Email:LogoDarkUrl, zelfde validatie als LogoUrl; leeg = geen swap - Twee img-klassen; dark-blok verbergt het lichte logo (incl. mso-hide) en toont het donkere - Lichte afgeronde plaat (bgcolor + vlak verloop) achter het logo als vangnet voor clients die media queries negeren of inverteren - ASF: LogoDarkUrl = /admin/brand-logo-on-dark.png (publiek bereikbaar gecontroleerd); SLP leeg (geen on-dark logo) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
76 lines
3.8 KiB
C#
76 lines
3.8 KiB
C#
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)
|
|
{
|
|
var failures = Check(options, app.Value);
|
|
return failures.Count == 0 ? ValidateOptionsResult.Success : ValidateOptionsResult.Fail(failures);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The problems of a mail configuration (setting names only, never values). Empty when mail is off (no host)
|
|
/// or the configuration is valid.
|
|
/// </summary>
|
|
public static IReadOnlyList<string> Check(EmailOptions options, AppMailOptions appOptions)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(options.Smtp.Host))
|
|
return []; // 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 > 120)
|
|
failures.Add("Email:Smtp:TimeoutSeconds must be between 1 and 120.");
|
|
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 = appOptions.PublicBaseUrl;
|
|
if (!IsValidPublicBaseUrl(baseUrl))
|
|
failures.Add("App:PublicBaseUrl must be an absolute https URL (http only for localhost) without a path.");
|
|
if (!MailLanguages.IsSupported(appOptions.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 '/'.");
|
|
if (!string.IsNullOrEmpty(options.LogoDarkUrl) && !IsValidLogoUrl(options.LogoDarkUrl))
|
|
failures.Add("Email:LogoDarkUrl must be an https URL or a path starting with '/'.");
|
|
|
|
return 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;
|
|
}
|
|
}
|