2010-02-08 14:18:28 -08:00
|
|
|
|
using System;
|
|
|
|
|
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-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-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-04 12:24:18 -08:00
|
|
|
|
|
2010-02-05 18:03:58 -08:00
|
|
|
|
public SetupController(
|
|
|
|
|
INotifier notifier,
|
|
|
|
|
IDatabaseMigrationManager databaseMigrationManager,
|
|
|
|
|
IOrchardHost orchardHost) {
|
2010-02-05 13:36:01 -08:00
|
|
|
|
_notifier = notifier;
|
2010-02-05 18:03:58 -08:00
|
|
|
|
_databaseMigrationManager = databaseMigrationManager;
|
|
|
|
|
_orchardHost = orchardHost;
|
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) {
|
|
|
|
|
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-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
|
|
|
|
|
_databaseMigrationManager.CreateCoordinator(model.DatabaseOptions ? "SQLite" : "SqlServer", Server.MapPath("~/App_Data"), model.DatabaseConnectionString);
|
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
|
|
|
|
|
var shellSettings = new ShellSettings { Name = "temp" };
|
|
|
|
|
using (var finiteEnvironment = _orchardHost.CreateStandaloneEnvironment(shellSettings)) {
|
|
|
|
|
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));
|
2010-02-05 18:03:58 -08:00
|
|
|
|
|
2010-02-08 14:18:28 -08:00
|
|
|
|
// set site name and settings
|
|
|
|
|
contentManager.Create<SiteSettings>("site", item => {
|
|
|
|
|
item.Record.SiteSalt = Guid.NewGuid().ToString("N");
|
|
|
|
|
item.Record.SiteName = model.SiteName;
|
|
|
|
|
item.Record.SuperUser = model.AdminUsername;
|
|
|
|
|
item.Record.PageTitleSeparator = " - ";
|
|
|
|
|
});
|
2010-02-05 18:03:58 -08:00
|
|
|
|
|
2010-02-08 14:18:28 -08:00
|
|
|
|
// create home page as a CMS page
|
|
|
|
|
var page = contentManager.Create("page");
|
|
|
|
|
page.As<BodyAspect>().Text = "Welcome to Orchard";
|
2010-02-08 15:20:40 -08:00
|
|
|
|
page.As<RoutableAspect>().Slug = "";
|
2010-02-08 14:18:28 -08:00
|
|
|
|
page.As<RoutableAspect>().Title = model.SiteName;
|
2010-02-08 15:20:40 -08:00
|
|
|
|
page.As<HasComments>().CommentsShown = false;
|
2010-02-08 14:18:28 -08:00
|
|
|
|
contentManager.Publish(page);
|
|
|
|
|
|
|
|
|
|
var authenticationService = finiteEnvironment.Resolve<IAuthenticationService>();
|
|
|
|
|
authenticationService.SignIn(user, true);
|
|
|
|
|
}
|
2010-02-05 18:03:58 -08:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|