2010-04-21 15:43:41 -07:00
|
|
|
|
using System;
|
2010-05-11 16:14:18 -07:00
|
|
|
|
using System.Linq;
|
2010-04-21 15:43:41 -07:00
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.Environment.Configuration;
|
2010-04-21 12:25:23 -07:00
|
|
|
|
using Orchard.Localization;
|
2011-02-08 11:22:47 -08:00
|
|
|
|
using Orchard.Logging;
|
2010-04-21 13:53:03 -07:00
|
|
|
|
using Orchard.MultiTenancy.Services;
|
2010-04-21 12:25:23 -07:00
|
|
|
|
using Orchard.MultiTenancy.ViewModels;
|
2010-12-08 14:16:48 -08:00
|
|
|
|
using Orchard.Security;
|
2010-04-21 15:43:41 -07:00
|
|
|
|
using Orchard.UI.Notify;
|
2011-02-08 11:22:47 -08:00
|
|
|
|
using Orchard.Utility.Extensions;
|
2010-04-21 12:25:23 -07:00
|
|
|
|
|
|
|
|
|
namespace Orchard.MultiTenancy.Controllers {
|
|
|
|
|
[ValidateInput(false)]
|
|
|
|
|
public class AdminController : Controller {
|
2010-04-21 13:53:03 -07:00
|
|
|
|
private readonly ITenantService _tenantService;
|
2010-05-11 16:21:00 -07:00
|
|
|
|
private readonly ShellSettings _thisShellSettings;
|
2010-04-21 13:53:03 -07:00
|
|
|
|
|
2010-05-11 16:21:00 -07:00
|
|
|
|
public AdminController(ITenantService tenantService, IOrchardServices orchardServices, ShellSettings shellSettings) {
|
2010-04-21 13:53:03 -07:00
|
|
|
|
_tenantService = tenantService;
|
2010-05-11 16:21:00 -07:00
|
|
|
|
_thisShellSettings = shellSettings;
|
2010-05-12 15:26:52 -07:00
|
|
|
|
|
2010-04-21 15:43:41 -07:00
|
|
|
|
Services = orchardServices;
|
2010-04-21 12:25:23 -07:00
|
|
|
|
T = NullLocalizer.Instance;
|
2011-02-08 11:22:47 -08:00
|
|
|
|
Logger = NullLogger.Instance;
|
2010-04-21 12:25:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-02 13:54:50 -07:00
|
|
|
|
public Localizer T { get; set; }
|
2010-04-21 15:43:41 -07:00
|
|
|
|
public IOrchardServices Services { get; set; }
|
2011-02-08 11:22:47 -08:00
|
|
|
|
public ILogger Logger { get; set; }
|
2010-04-21 12:25:23 -07:00
|
|
|
|
|
2010-04-21 19:41:37 -07:00
|
|
|
|
public ActionResult Index() {
|
2010-04-22 17:38:02 -07:00
|
|
|
|
return View(new TenantsIndexViewModel { TenantSettings = _tenantService.GetTenants() });
|
2010-04-21 13:53:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Add() {
|
2010-12-08 14:16:48 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Cannot create tenant")))
|
2010-05-13 13:32:48 -07:00
|
|
|
|
return new HttpUnauthorizedResult();
|
2010-10-12 15:42:18 -07:00
|
|
|
|
|
|
|
|
|
if ( !EnsureDefaultTenant() )
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
return View(new TenantAddViewModel());
|
2010-04-21 12:25:23 -07:00
|
|
|
|
}
|
2010-04-21 15:43:41 -07:00
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Add")]
|
2010-05-13 13:32:48 -07:00
|
|
|
|
public ActionResult AddPOST(TenantAddViewModel viewModel) {
|
2010-04-21 15:43:41 -07:00
|
|
|
|
try {
|
2010-12-08 14:16:48 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't create tenant")))
|
2010-04-21 15:43:41 -07:00
|
|
|
|
return new HttpUnauthorizedResult();
|
2010-10-12 15:42:18 -07:00
|
|
|
|
|
|
|
|
|
if ( !EnsureDefaultTenant() )
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-04-21 15:43:41 -07:00
|
|
|
|
_tenantService.CreateTenant(
|
|
|
|
|
new ShellSettings {
|
|
|
|
|
Name = viewModel.Name,
|
2010-04-22 17:38:02 -07:00
|
|
|
|
RequestUrlHost = viewModel.RequestUrlHost,
|
|
|
|
|
RequestUrlPrefix = viewModel.RequestUrlPrefix,
|
2010-05-13 13:32:48 -07:00
|
|
|
|
DataProvider = viewModel.DataProvider,
|
2010-05-10 16:01:04 -07:00
|
|
|
|
DataConnectionString = viewModel.DatabaseConnectionString,
|
|
|
|
|
DataTablePrefix = viewModel.DatabaseTablePrefix,
|
2010-04-22 17:38:02 -07:00
|
|
|
|
State = new TenantState("Uninitialized")
|
2010-04-21 15:43:41 -07:00
|
|
|
|
});
|
|
|
|
|
|
2010-04-22 17:38:02 -07:00
|
|
|
|
return RedirectToAction("Index");
|
2011-02-08 11:22:47 -08:00
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
this.Error(exception, T("Creating Tenant failed: {0}", exception.Message), Logger, Services.Notifier);
|
|
|
|
|
|
2010-04-21 15:43:41 -07:00
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-11 16:14:18 -07:00
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
public ActionResult Edit(string name) {
|
2010-12-08 14:16:48 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Cannot edit tenant")))
|
2010-05-13 13:32:48 -07:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-10-12 15:42:18 -07:00
|
|
|
|
if ( !EnsureDefaultTenant() )
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
var tenant = _tenantService.GetTenants().FirstOrDefault(ss => ss.Name == name);
|
|
|
|
|
if (tenant == null)
|
2010-10-14 11:30:58 -07:00
|
|
|
|
return HttpNotFound();
|
2010-05-13 13:32:48 -07:00
|
|
|
|
|
|
|
|
|
return View(new TenantEditViewModel {
|
|
|
|
|
Name = tenant.Name,
|
|
|
|
|
RequestUrlHost = tenant.RequestUrlHost,
|
|
|
|
|
RequestUrlPrefix = tenant.RequestUrlPrefix,
|
|
|
|
|
DataProvider = tenant.DataProvider,
|
|
|
|
|
DatabaseConnectionString = tenant.DataConnectionString,
|
|
|
|
|
DatabaseTablePrefix = tenant.DataTablePrefix,
|
|
|
|
|
State = tenant.State
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Edit")]
|
|
|
|
|
public ActionResult EditPost(TenantEditViewModel viewModel) {
|
|
|
|
|
try {
|
2010-12-08 14:16:48 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't edit tenant")))
|
2010-05-13 13:32:48 -07:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-10-12 15:42:18 -07:00
|
|
|
|
if ( !EnsureDefaultTenant() )
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
var tenant = _tenantService.GetTenants().FirstOrDefault(ss => ss.Name == viewModel.Name);
|
|
|
|
|
if (tenant == null)
|
2010-10-14 11:30:58 -07:00
|
|
|
|
return HttpNotFound();
|
2010-05-13 13:32:48 -07:00
|
|
|
|
|
|
|
|
|
_tenantService.UpdateTenant(
|
|
|
|
|
new ShellSettings {
|
|
|
|
|
Name = tenant.Name,
|
|
|
|
|
RequestUrlHost = viewModel.RequestUrlHost,
|
|
|
|
|
RequestUrlPrefix = viewModel.RequestUrlPrefix,
|
|
|
|
|
DataProvider = viewModel.DataProvider,
|
|
|
|
|
DataConnectionString = viewModel.DatabaseConnectionString,
|
|
|
|
|
DataTablePrefix = viewModel.DatabaseTablePrefix,
|
|
|
|
|
State = tenant.State
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
2011-02-08 11:22:47 -08:00
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
this.Error(exception, T("Failed to edit tenant: {0} ", exception.Message), Logger, Services.Notifier);
|
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-11 16:14:18 -07:00
|
|
|
|
[HttpPost]
|
2010-05-13 13:32:48 -07:00
|
|
|
|
public ActionResult Disable(string name) {
|
2010-12-08 14:16:48 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't disable tenant")))
|
2010-05-11 16:14:18 -07:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-10-12 15:42:18 -07:00
|
|
|
|
if ( !EnsureDefaultTenant() )
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
var tenant = _tenantService.GetTenants().FirstOrDefault(ss => ss.Name == name);
|
2010-05-11 16:14:18 -07:00
|
|
|
|
|
2010-05-11 16:21:00 -07:00
|
|
|
|
if (tenant != null && tenant.Name != _thisShellSettings.Name) {
|
2010-05-11 16:14:18 -07:00
|
|
|
|
tenant.State.CurrentState = TenantState.State.Disabled;
|
|
|
|
|
_tenantService.UpdateTenant(tenant);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-12 15:42:18 -07:00
|
|
|
|
return RedirectToAction("Index");
|
2010-05-11 16:14:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2010-05-13 13:32:48 -07:00
|
|
|
|
public ActionResult Enable(string name) {
|
2010-12-08 14:16:48 -08:00
|
|
|
|
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't enable tenant")))
|
2010-05-11 16:14:18 -07:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-10-12 15:42:18 -07:00
|
|
|
|
if ( !EnsureDefaultTenant() )
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-05-13 13:32:48 -07:00
|
|
|
|
var tenant = _tenantService.GetTenants().FirstOrDefault(ss => ss.Name == name);
|
2010-05-11 16:14:18 -07:00
|
|
|
|
|
2010-05-11 16:21:00 -07:00
|
|
|
|
if (tenant != null && tenant.Name != _thisShellSettings.Name) {
|
2010-05-11 16:14:18 -07:00
|
|
|
|
tenant.State.CurrentState = TenantState.State.Running;
|
|
|
|
|
_tenantService.UpdateTenant(tenant);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-12 15:42:18 -07:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EnsureDefaultTenant() {
|
2011-02-01 13:17:03 -08:00
|
|
|
|
return _thisShellSettings.Name == ShellSettings.DefaultName;
|
2010-05-11 16:14:18 -07:00
|
|
|
|
}
|
2010-04-21 12:25:23 -07:00
|
|
|
|
}
|
|
|
|
|
}
|