2010-02-08 14:18:28 -08:00
using System ;
2010-02-10 14:34:20 -08:00
using System.IO ;
2010-02-08 14:18:28 -08:00
using System.Web.Mvc ;
2010-02-08 15:20:40 -08:00
using Orchard.Comments.Models ;
2010-02-05 18:03:58 -08:00
using Orchard.ContentManagement ;
2010-02-08 14:18:28 -08:00
using Orchard.Core.Common.Models ;
using Orchard.Core.Settings.Models ;
2010-02-10 11:30:44 -08:00
using Orchard.Data ;
2010-02-05 18:03:58 -08:00
using Orchard.Data.Migrations ;
using Orchard.Environment ;
using Orchard.Environment.Configuration ;
2010-02-08 14:18:28 -08:00
using Orchard.Security ;
2010-02-09 00:41:23 -08:00
using Orchard.Settings ;
2010-02-05 15:29:49 -08:00
using Orchard.Setup.ViewModels ;
2010-02-04 12:24:18 -08:00
using Orchard.Localization ;
2010-02-05 13:36:01 -08:00
using Orchard.UI.Notify ;
2010-02-04 12:24:18 -08:00
2010-02-05 15:29:49 -08:00
namespace Orchard.Setup.Controllers {
2010-02-04 12:24:18 -08:00
public class SetupController : Controller {
2010-02-05 13:36:01 -08:00
private readonly INotifier _notifier ;
2010-02-05 18:03:58 -08:00
private readonly IDatabaseMigrationManager _databaseMigrationManager ;
private readonly IOrchardHost _orchardHost ;
2010-02-08 23:37:13 -08:00
private readonly IShellSettingsLoader _shellSettingsLoader ;
2010-02-04 12:24:18 -08:00
2010-02-05 18:03:58 -08:00
public SetupController (
INotifier notifier ,
IDatabaseMigrationManager databaseMigrationManager ,
2010-02-08 23:37:13 -08:00
IOrchardHost orchardHost ,
IShellSettingsLoader shellSettingsLoader ) {
2010-02-05 13:36:01 -08:00
_notifier = notifier ;
2010-02-05 18:03:58 -08:00
_databaseMigrationManager = databaseMigrationManager ;
_orchardHost = orchardHost ;
2010-02-08 23:37:13 -08:00
_shellSettingsLoader = shellSettingsLoader ;
2010-02-04 12:24:18 -08:00
T = NullLocalizer . Instance ;
}
private Localizer T { get ; set ; }
2010-02-08 15:20:40 -08:00
public ActionResult Index ( SetupViewModel model ) {
2010-02-10 14:34:20 -08:00
if ( ! CanWriteTo ( Server . MapPath ( "~/App_Data" ) ) ) {
_notifier . Error ( T ( "Hey, it looks like I can't write to the App_Data folder in the root of this application and that's where I need to save some of the information you're about to enter.\r\n\r\nPlease give me (the machine account this application is running under) write access to App_Data so I can get this app all set up for you.\r\n\r\nThanks!" ) ) ;
}
2010-02-08 15:20:40 -08:00
return View ( model ? ? new SetupViewModel { AdminUsername = "admin" } ) ;
2010-02-05 13:36:01 -08:00
}
2010-02-08 15:20:40 -08:00
[HttpPost, ActionName("Index")]
public ActionResult IndexPOST ( SetupViewModel model ) {
2010-02-08 14:18:28 -08:00
try {
if ( ! ModelState . IsValid ) {
2010-02-08 15:20:40 -08:00
return Index ( model ) ;
2010-02-08 14:18:28 -08:00
}
2010-02-05 13:36:01 -08:00
2010-02-09 14:25:38 -08:00
var shellSettings = new ShellSettings {
Name = "default" ,
DataProvider = model . DatabaseOptions ? "SQLite" : "SqlServer" ,
DataConnectionString = model . DatabaseConnectionString
} ;
2010-02-08 14:18:28 -08:00
// initialize the database:
// provider: SqlServer or SQLite
// dataFolder: physical path (map before calling). Builtin database will be created in this location
// connectionString: optional - if provided the dataFolder is essentially ignored, but should still be passed in
2010-02-10 00:34:35 -08:00
_databaseMigrationManager . CreateCoordinator ( shellSettings . DataProvider , Server . MapPath ( "~/App_Data" ) , shellSettings . DataConnectionString ) ;
2010-02-05 18:03:58 -08:00
2010-02-08 14:18:28 -08:00
// creating a standalone environment.
// in theory this environment can be used to resolve any normal components by interface, and those
// components will exist entirely in isolation - no crossover between the safemode container currently in effect
using ( var finiteEnvironment = _orchardHost . CreateStandaloneEnvironment ( shellSettings ) ) {
2010-02-10 11:30:44 -08:00
try {
var contentManager = finiteEnvironment . Resolve < IContentManager > ( ) ;
// create superuser
var membershipService = finiteEnvironment . Resolve < IMembershipService > ( ) ;
var user =
membershipService . CreateUser ( new CreateUserParams ( model . AdminUsername , model . AdminPassword ,
String . Empty , String . Empty , String . Empty ,
true ) ) ;
// set site name and settings
var siteService = finiteEnvironment . Resolve < ISiteService > ( ) ;
var siteSettings = siteService . GetSiteSettings ( ) . As < SiteSettings > ( ) ;
siteSettings . Record . SiteSalt = Guid . NewGuid ( ) . ToString ( "N" ) ;
siteSettings . Record . SiteName = model . SiteName ;
siteSettings . Record . SuperUser = model . AdminUsername ;
siteSettings . Record . PageTitleSeparator = " - " ;
// create home page as a CMS page
var page = contentManager . Create ( "page" ) ;
page . As < BodyAspect > ( ) . Text = "Welcome to Orchard" ;
page . As < RoutableAspect > ( ) . Slug = "" ;
page . As < RoutableAspect > ( ) . Title = model . SiteName ;
page . As < HasComments > ( ) . CommentsShown = false ;
page . As < CommonAspect > ( ) . Owner = user ;
contentManager . Publish ( page ) ;
var authenticationService = finiteEnvironment . Resolve < IAuthenticationService > ( ) ;
authenticationService . SignIn ( user , true ) ;
}
catch {
finiteEnvironment . Resolve < ITransactionManager > ( ) . Cancel ( ) ;
throw ;
}
2010-02-08 14:18:28 -08:00
}
2010-02-05 18:03:58 -08:00
2010-02-09 14:25:38 -08:00
_shellSettingsLoader . SaveSettings ( shellSettings ) ;
2010-02-09 00:41:23 -08:00
_orchardHost . Reinitialize ( ) ;
2010-02-08 14:18:28 -08:00
_notifier . Information ( T ( "Setup succeeded" ) ) ;
2010-02-05 13:36:01 -08:00
2010-02-08 14:18:28 -08:00
// redirect to the welcome page.
2010-02-08 15:20:40 -08:00
return Redirect ( "~/" ) ;
2010-02-08 14:18:28 -08:00
}
catch ( Exception exception ) {
_notifier . Error ( T ( "Setup failed: " + exception . Message ) ) ;
2010-02-08 15:20:40 -08:00
return Index ( model ) ;
2010-02-08 14:18:28 -08:00
}
2010-02-04 12:24:18 -08:00
}
2010-02-10 14:34:20 -08:00
static bool CanWriteTo ( string path ) {
try {
var systemCheckPath = Path . Combine ( path , "_systemcheck.txt" ) ;
System . IO . File . WriteAllText ( systemCheckPath , "Communicator check one two one two" ) ;
System . IO . File . AppendAllText ( systemCheckPath , "\r\nThis is Bones McCoy on a line to Sulu" ) ;
System . IO . File . Delete ( systemCheckPath ) ;
return true ;
} catch {
return false ;
}
}
2010-02-04 12:24:18 -08:00
}
}