#20368: Prevent creation of a tenant that already exists

Work Item: 20368
This commit is contained in:
jasperd
2013-12-20 21:41:18 +01:00
committed by Nicholas Mayne
parent 03e72cb931
commit 0e1c0c567e
2 changed files with 21 additions and 4 deletions

View File

@@ -1,4 +1,6 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Orchard.Commands;
using Orchard.Environment.Configuration;
using Orchard.MultiTenancy.Services;
@@ -42,6 +44,16 @@ namespace Orchard.MultiTenancy.Commands {
[OrchardSwitches("Host,UrlPrefix")]
public void Create(string tenantName) {
Context.Output.WriteLine(T("Creating tenant"));
if (string.IsNullOrWhiteSpace(tenantName) || !Regex.IsMatch(tenantName, @"^\w+$")) {
Context.Output.WriteLine(T("Invalid tenant name. Must contain characters only and no spaces."));
return;
}
if (_tenantService.GetTenants().Any(tenant => string.Equals(tenant.Name, tenantName, StringComparison.OrdinalIgnoreCase))) {
Context.Output.WriteLine(T("Could not create tenant \"{0}\". A tenant with the same name already exists.", tenantName));
return;
}
_tenantService.CreateTenant(
new ShellSettings {
Name = tenantName,

View File

@@ -9,7 +9,6 @@ using Orchard.MultiTenancy.Services;
using Orchard.MultiTenancy.ViewModels;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Utility.Extensions;
namespace Orchard.MultiTenancy.Controllers {
[ValidateInput(false)]
@@ -52,11 +51,17 @@ namespace Orchard.MultiTenancy.Controllers {
[HttpPost, ActionName("Add")]
public ActionResult AddPOST(TenantAddViewModel viewModel) {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't create tenant")))
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't create tenant"))) {
return new HttpUnauthorizedResult();
}
if (!EnsureDefaultTenant())
if (!EnsureDefaultTenant()) {
return new HttpUnauthorizedResult();
}
if (_tenantService.GetTenants().Any(tenant => string.Equals(tenant.Name, viewModel.Name, StringComparison.OrdinalIgnoreCase))) {
ModelState.AddModelError("Name", T("A tenant with the same name already exists.", viewModel.Name).Text);
}
// ensure tenants name are valid
if (!String.IsNullOrEmpty(viewModel.Name) && !Regex.IsMatch(viewModel.Name, @"^\w+$")) {