Added validation and moved Unattended Settings into their own model. + Added DataAnnonation validation on all our settings

This commit is contained in:
Bjarke Berg
2021-03-07 19:20:16 +01:00
parent c57a9b9d3b
commit 24d4b4c9fb
9 changed files with 183 additions and 111 deletions

View File

@@ -84,33 +84,6 @@ namespace Umbraco.Cms.Core.Configuration.Models
/// </summary>
public bool InstallMissingDatabase { get; set; } = false;
/// <summary>
/// Gets or sets a value indicating whether unattended installs are enabled.
/// </summary>
/// <remarks>
/// <para>By default, when a database connection string is configured and it is possible to connect to
/// the database, but the database is empty, the runtime enters the <c>Install</c> level.
/// If this option is set to <c>true</c> an unattended install will be performed and the runtime enters
/// the <c>Run</c> level.</para>
/// </remarks>
public bool InstallUnattended { get; set; } = false;
/// <summary>
/// Gets or sets a value to use for creating a user with a name for Unattended Installs
/// </summary>
public string UnattendedUserName { get; set; } = string.Empty;
/// <summary>
/// Gets or sets a value to use for creating a user with an email for Unattended Installs
/// </summary>
public string UnattendedUserEmail { get; set; } = string.Empty;
/// <summary>
/// Gets or sets a value to use for creating a user with a password for Unattended Installs
/// </summary>
public string UnattendedUserPassword { get; set; } = string.Empty;
/// <summary>
/// Gets or sets a value indicating whether to disable the election for a single server.
/// </summary>

View File

@@ -0,0 +1,38 @@
using System.ComponentModel.DataAnnotations;
namespace Umbraco.Cms.Core.Configuration.Models
{
/// <summary>
/// Typed configuration options for unattended settings.
/// </summary>
public class UnattendedSettings
{
/// <summary>
/// Gets or sets a value indicating whether unattended installs are enabled.
/// </summary>
/// <remarks>
/// <para>By default, when a database connection string is configured and it is possible to connect to
/// the database, but the database is empty, the runtime enters the <c>Install</c> level.
/// If this option is set to <c>true</c> an unattended install will be performed and the runtime enters
/// the <c>Run</c> level.</para>
/// </remarks>
public bool InstallUnattended { get; set; } = false;
/// <summary>
/// Gets or sets a value to use for creating a user with a name for Unattended Installs
/// </summary>
public string UnattendedUserName { get; set; } = null;
/// <summary>
/// Gets or sets a value to use for creating a user with an email for Unattended Installs
/// </summary>
[EmailAddress]
public string UnattendedUserEmail { get; set; } = null;
/// <summary>
/// Gets or sets a value to use for creating a user with a password for Unattended Installs
/// </summary>
public string UnattendedUserPassword { get; set; } = null;
}
}

View File

@@ -0,0 +1,44 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.Extensions.Options;
namespace Umbraco.Cms.Core.Configuration.Models.Validation
{
/// <summary>
/// Validator for configuration representated as <see cref="UnattendedSettings"/>.
/// </summary>
public class UnattendedSettingsValidator
: IValidateOptions<UnattendedSettings>
{
/// <inheritdoc/>
public ValidateOptionsResult Validate(string name, UnattendedSettings options)
{
if (options.InstallUnattended)
{
int setValues = 0;
if (!string.IsNullOrEmpty(options.UnattendedUserName))
{
setValues++;
}
if (!string.IsNullOrEmpty(options.UnattendedUserEmail))
{
setValues++;
}
if (!string.IsNullOrEmpty(options.UnattendedUserPassword))
{
setValues++;
}
if (0 < setValues && setValues < 3)
{
return ValidateOptionsResult.Fail($"Configuration entry {Constants.Configuration.ConfigUnattended} contains invalid values.\nIf any of the {nameof(options.UnattendedUserName)}, {nameof(options.UnattendedUserEmail)}, {nameof(options.UnattendedUserPassword)} are set, all of them are required.");
}
}
return ValidateOptionsResult.Success;
}
}
}