mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#20368: Prevent creation of a tenant that already exists
Work Item: 20368
This commit is contained in:
@@ -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,
|
||||
|
@@ -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+$")) {
|
||||
|
Reference in New Issue
Block a user