IShellSettingsEventHandler to modify tenants in the running application

Added new methods for altering the tenants: Remove and Update

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-05-12 15:26:52 -07:00
parent 9689f2af41
commit f6c170679e
7 changed files with 77 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ namespace Orchard.MultiTenancy.Controllers {
public AdminController(ITenantService tenantService, IOrchardServices orchardServices, ShellSettings shellSettings) {
_tenantService = tenantService;
_thisShellSettings = shellSettings;
Services = orchardServices;
T = NullLocalizer.Instance;
}
@@ -36,6 +37,7 @@ namespace Orchard.MultiTenancy.Controllers {
try {
if (!Services.Authorizer.Authorize(Permissions.ManageTenants, T("Couldn't create tenant")))
return new HttpUnauthorizedResult();
_tenantService.CreateTenant(
new ShellSettings {
Name = viewModel.Name,

View File

@@ -66,6 +66,7 @@
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Extensions\UrlHelperExtensions.cs" />
<Compile Include="Services\ITenantService.cs" />
<Compile Include="Services\ShellSettingsEventHandler.cs" />
<Compile Include="Services\TenantService.cs" />
<Compile Include="ViewModels\TenantsAddViewModel.cs" />
<Compile Include="ViewModels\TenantsIndexViewModel.cs" />

View File

@@ -0,0 +1,27 @@
using System;
using Orchard.Events;
using Orchard.Environment.Configuration;
using Orchard.Environment;
namespace Orchard.MultiTenancy.Services {
public class ShellSettingsEventHandler : IShellSettingsEventHandler, IDependency {
private readonly IRunningShellTable _runningShellTable;
public ShellSettingsEventHandler(IRunningShellTable runningShellTable) {
_runningShellTable = runningShellTable;
}
public void Created(ShellSettings settings) {
_runningShellTable.Add(settings);
}
public void Deleted(ShellSettings settings) {
_runningShellTable.Remove(settings);
}
public void Updated(ShellSettings settings) {
_runningShellTable.Update(settings);
}
}
}

View File

@@ -2,15 +2,18 @@
using System.Linq;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Events;
namespace Orchard.MultiTenancy.Services {
public class TenantService : ITenantService {
private readonly IShellSettingsManager _shellSettingsManager;
private readonly IOrchardHost _orchardHost;
private readonly IShellSettingsEventHandler _shellSettingsEventHandler;
public TenantService(IShellSettingsManager shellSettingsManager, IOrchardHost orchardHost) {
public TenantService(IShellSettingsManager shellSettingsManager, IOrchardHost orchardHost, IShellSettingsEventHandler shellSettingsEventHandler) {
_shellSettingsManager = shellSettingsManager;
_orchardHost = orchardHost;
_shellSettingsEventHandler = shellSettingsEventHandler;
}
public IEnumerable<ShellSettings> GetTenants() {
@@ -19,12 +22,16 @@ namespace Orchard.MultiTenancy.Services {
public void CreateTenant(ShellSettings settings) {
_shellSettingsManager.SaveSettings(settings);
_shellSettingsEventHandler.Created(settings);
}
public void UpdateTenant(ShellSettings settings) {
var tenant = GetTenants().FirstOrDefault(ss => ss.Name == settings.Name);
if (tenant != null)
if ( tenant != null ) {
_shellSettingsManager.SaveSettings(settings);
_shellSettingsEventHandler.Updated(settings);
}
}
}
}

View File

@@ -8,6 +8,8 @@ using Orchard.Environment.Configuration;
namespace Orchard.Environment {
public interface IRunningShellTable {
void Add(ShellSettings settings);
void Remove(ShellSettings settings);
void Update(ShellSettings settings);
ShellSettings Match(HttpContextBase httpContext);
}
@@ -22,6 +24,30 @@ namespace Orchard.Environment {
.Concat(new[] { settings })
.ToArray();
Organize();
}
public void Remove(ShellSettings settings) {
_shells = _shells
.Where(s => s.Name != settings.Name)
.ToArray();
Organize();
}
public void Update(ShellSettings settings) {
_shells = _shells
.Where(s => s.Name != settings.Name)
.ToArray();
_shells = _shells
.Concat(new[] { settings })
.ToArray();
Organize();
}
private void Organize() {
var qualified =
_shells.Where(x => !string.IsNullOrEmpty(x.RequestUrlHost) || !string.IsNullOrEmpty(x.RequestUrlPrefix));
@@ -50,7 +76,8 @@ namespace Orchard.Environment {
}
public ShellSettings Match(HttpContextBase httpContext) {
var host = httpContext.Request.ServerVariables.Get("HTTP_HOST") ?? "";
// use Host header to prevent proxy alteration of the orignal request
var host = httpContext.Request.Headers["Host"];
var hostLength = host.IndexOf(':');
if (hostLength != -1)

View File

@@ -0,0 +1,9 @@
using Orchard.Environment.Configuration;
namespace Orchard.Events {
public interface IShellSettingsEventHandler : IEventHandler {
void Created(ShellSettings settings);
void Deleted(ShellSettings settings);
void Updated(ShellSettings settings);
}
}

View File

@@ -184,6 +184,7 @@
<Compile Include="Events\EventsInterceptor.cs" />
<Compile Include="Events\EventsModule.cs" />
<Compile Include="Events\EventsRegistrationSource.cs" />
<Compile Include="Events\IShellSettingsEventHandler.cs" />
<Compile Include="Events\IEventBus.cs" />
<Compile Include="Events\IEventBusHandler.cs" />
<Compile Include="Environment\Extensions\Folders\AreaFolders.cs" />