Skip to content

Instantly share code, notes, and snippets.

@darrenferguson
Created June 17, 2015 18:10
Show Gist options
  • Save darrenferguson/c6c13c7be26057b01d1d to your computer and use it in GitHub Desktop.
Save darrenferguson/c6c13c7be26057b01d1d to your computer and use it in GitHub Desktop.
using System.Configuration;
using Umbraco.Core;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using System.Web.Configuration;
using Newtonsoft.Json;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
namespace WebApplication9.Classes
{
public class UmbracoInstallingApplicationEventHandler : IApplicationEventHandler
{
private class StartupConfig
{
public string HashedPassword { get; set; }
public string SqlServerName { get; set; }
public string DatabaseName { get; set; }
public string TrustedConnectionString { get; set; }
}
private void ExceuteSql(string connectionString, string sql)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
using (var cmd = new SqlCommand(sql, conn))
{
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
conn.Dispose();
}
}
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var umbracoCoreAssembly = Assembly.GetAssembly(typeof(ApplicationContext));
var fileVersionInfo = FileVersionInfo.GetVersionInfo(umbracoCoreAssembly.Location);
var umbracoVersion = fileVersionInfo.ProductVersion;
var umbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
if (!string.IsNullOrEmpty(umbracoConnectionString))
return;
var config = JsonConvert.DeserializeObject<StartupConfig>(ConfigurationManager.AppSettings["moriyamaStartupConfig"]);
var sql = string.Format("if not exists(select * from sys.databases where name = '{0}') create database {0}", config.DatabaseName);
var connString = string.Format("Server={0};" + config.TrustedConnectionString, config.SqlServerName);
ExceuteSql(connString, sql);
var umbracoConnString = string.Format("server={0};database={1};" + config.TrustedConnectionString, config.SqlServerName, config.DatabaseName);
var db = new UmbracoDatabase(umbracoConnString, "System.Data.SqlClient");
SqlSyntaxContext.SqlSyntaxProvider = new SqlServerSyntaxProvider();
db.CreateDatabaseSchema();
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
configuration.ConnectionStrings.ConnectionStrings["umbracoDbDSN"].ConnectionString = umbracoConnString;
configuration.ConnectionStrings.ConnectionStrings["umbracoDbDSN"].ProviderName = "System.Data.SqlClient";
configuration.AppSettings.Settings["umbracoConfigurationStatus"].Value = umbracoVersion;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var umbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
if (string.IsNullOrEmpty(umbracoConnectionString))
return;
var config = JsonConvert.DeserializeObject<StartupConfig>(ConfigurationManager.AppSettings["moriyamaStartupConfig"]);
var sql = string.Format("UPDATE umbracoUser set userdisabled=0, userPassword='{0}'", config.HashedPassword);
ExceuteSql(umbracoConnectionString, sql);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment