Files
Umbraco-CMS/src/Umbraco.Web/Install/InstallSteps/DatabaseConfigureStep.cs

97 lines
3.7 KiB
C#
Raw Normal View History

using System;
using System.Configuration;
using System.Threading.Tasks;
using Umbraco.Core;
2017-09-08 19:39:13 +02:00
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations.Install;
using Umbraco.Web.Install.Models;
namespace Umbraco.Web.Install.InstallSteps
{
[InstallSetupStep(InstallationType.NewInstall,
"DatabaseConfigure", "database", 10, "Setting up a database, so Umbraco has a place to store your website",
PerformsAppRestart = true)]
internal class DatabaseConfigureStep : InstallSetupStep<DatabaseModel>
{
2016-11-29 10:31:25 +01:00
private readonly DatabaseBuilder _databaseBuilder;
2017-09-08 19:39:13 +02:00
private readonly ILogger _logger;
2016-11-29 10:31:25 +01:00
public DatabaseConfigureStep(DatabaseBuilder databaseBuilder)
{
2016-11-29 10:31:25 +01:00
_databaseBuilder = databaseBuilder;
}
public override Task<InstallSetupResult> ExecuteAsync(DatabaseModel database)
{
//if the database model is null then we will apply the defaults
if (database == null)
{
database = new DatabaseModel();
}
2018-12-18 10:39:39 +01:00
if (_databaseBuilder.CanConnect(database.DatabaseType.ToString(), database.ConnectionString, database.Server, database.DatabaseName, database.Login, database.Password, database.IntegratedAuth) == false)
{
throw new InstallException("Could not connect to the database");
}
ConfigureConnection(database);
return Task.FromResult<InstallSetupResult>(null);
}
private void ConfigureConnection(DatabaseModel database)
{
if (database.ConnectionString.IsNullOrWhiteSpace() == false)
{
2016-11-29 10:31:25 +01:00
_databaseBuilder.ConfigureDatabaseConnection(database.ConnectionString);
}
else if (database.DatabaseType == DatabaseType.SqlCe)
{
2016-11-29 10:31:25 +01:00
_databaseBuilder.ConfigureEmbeddedDatabaseConnection();
}
else if (database.IntegratedAuth)
{
2016-11-29 10:31:25 +01:00
_databaseBuilder.ConfigureIntegratedSecurityDatabaseConnection(database.Server, database.DatabaseName);
}
else
{
var password = database.Password.Replace("&", "&amp;").Replace(">", "&gt;").Replace("<", "&lt;").Replace("\"", "&quot;").Replace("'", "''");
password = string.Format("'{0}'", password);
2016-06-16 06:09:59 -06:00
2016-11-29 10:31:25 +01:00
_databaseBuilder.ConfigureDatabaseConnection(
2016-06-16 06:09:59 -06:00
database.Server, database.DatabaseName, database.Login, password,
database.DatabaseType.ToString());
}
}
2016-11-29 10:31:25 +01:00
public override string View => ShouldDisplayView() ? base.View : "";
public override bool RequiresExecution(DatabaseModel model)
{
return ShouldDisplayView();
}
private bool ShouldDisplayView()
{
//If the connection string is already present in web.config we don't need to show the settings page and we jump to installing/upgrading.
2017-05-12 14:49:44 +02:00
var databaseSettings = ConfigurationManager.ConnectionStrings[Constants.System.UmbracoConnectionName];
2016-11-29 10:31:25 +01:00
if (_databaseBuilder.IsConnectionStringConfigured(databaseSettings))
{
try
{
//Since a connection string was present we verify the db can connect and query
2018-12-18 10:39:39 +01:00
_ = _databaseBuilder.ValidateSchema();
return false;
}
2017-09-08 19:39:13 +02:00
catch (Exception ex)
{
_logger.Error<DatabaseConfigureStep>(ex, "An error occurred, reconfiguring...");
//something went wrong, could not connect so probably need to reconfigure
return true;
}
}
return true;
}
}
}