#17215: Fixing tenants validation

Work Items: 17215

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-03-31 12:43:26 -07:00
parent 2d468fa229
commit 95b4b6e333
5 changed files with 55 additions and 32 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using Orchard.Environment.Configuration;
using Orchard.Localization;
@@ -45,13 +46,22 @@ namespace Orchard.MultiTenancy.Controllers {
[HttpPost, ActionName("Add")]
public ActionResult AddPOST(TenantAddViewModel viewModel) {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't create tenant")))
return new HttpUnauthorizedResult();
if (!EnsureDefaultTenant())
return new HttpUnauthorizedResult();
// ensure tenants name are valid
if (!String.IsNullOrEmpty(viewModel.Name) && !Regex.IsMatch(viewModel.Name, @"^\w+$")) {
ModelState.AddModelError("Name", T("Invalid tenant name. Must contain characters only and no spaces.").Text);
}
if (!ModelState.IsValid) {
return View(viewModel);
}
try {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't create tenant")))
return new HttpUnauthorizedResult();
if ( !EnsureDefaultTenant() )
return new HttpUnauthorizedResult();
_tenantService.CreateTenant(
new ShellSettings {
Name = viewModel.Name,
@@ -64,9 +74,9 @@ namespace Orchard.MultiTenancy.Controllers {
});
return RedirectToAction("Index");
} catch (Exception exception) {
}
catch (Exception exception) {
this.Error(exception, T("Creating Tenant failed: {0}", exception.Message), Logger, Services.Notifier);
return View(viewModel);
}
}
@@ -95,7 +105,7 @@ namespace Orchard.MultiTenancy.Controllers {
[HttpPost, ActionName("Edit")]
public ActionResult EditPost(TenantEditViewModel viewModel) {
try {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Couldn't edit tenant")))
return new HttpUnauthorizedResult();
@@ -105,24 +115,30 @@ namespace Orchard.MultiTenancy.Controllers {
var tenant = _tenantService.GetTenants().FirstOrDefault(ss => ss.Name == viewModel.Name);
if (tenant == null)
return HttpNotFound();
_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");
} catch (Exception exception) {
this.Error(exception, T("Failed to edit tenant: {0} ", exception.Message), Logger, Services.Notifier);
if (!ModelState.IsValid) {
return View(viewModel);
}
return View(viewModel);
}
try {
_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");
}
catch (Exception exception) {
this.Error(exception, T("Failed to edit tenant: {0} ", exception.Message), Logger, Services.Notifier);
return View(viewModel);
}
}
[HttpPost]

View File

@@ -3,8 +3,14 @@ using Orchard.MultiTenancy.Annotations;
namespace Orchard.MultiTenancy.ViewModels {
public class TenantAddViewModel {
public TenantAddViewModel() {
// define "Allow the tenant to set up the database" as default value
DataProvider = "";
}
[Required]
public string Name { get; set; }
[Required]
public string RequestUrlHost { get; set; }
public string RequestUrlPrefix { get; set; }
public string DataProvider { get; set; }

View File

@@ -6,6 +6,7 @@ namespace Orchard.MultiTenancy.ViewModels {
public class TenantEditViewModel {
[Required]
public string Name { get; set; }
[Required]
public string RequestUrlHost { get; set; }
public string RequestUrlPrefix { get; set; }
public string DataProvider { get; set; }

View File

@@ -6,13 +6,13 @@
@Html.ValidationSummary()
<fieldset>
<div>
<label for="Name">@T("Name")</label>
<input id="Name" class="textMedium" name="Name" type="text" />
</div>
<label for="@Html.FieldIdFor(m => m.Name)">@T("Name")</label>
@Html.TextBoxFor(m => m.Name, new { @class = "text" })
</div>
<div>
<label for="RequestUrlHost">@T("Host")</label>
<input id="RequestUrlHost" class="textMedium" name="RequestUrlHost" type="text" />
<span class="hint">@T("Example: If host is 'orchardproject.net', the tenant site URL is 'http://orchardproject.net/'")</span>
<label for="@Html.FieldIdFor(m => m.RequestUrlHost)">@T("Host")</label>
@Html.TextBoxFor(m => m.RequestUrlHost, new { @class = "textMedium" })
<span class="hint">@T("Example: If host is \"orchardproject.net\", the tenant site URL is \"http://orchardproject.net/\"")</span>
</div>
</fieldset>
<fieldset>

View File

@@ -10,7 +10,7 @@
<h2>@Model.Name</h2>
</div>
<div>
<label for="RequestUrlHost">@T("Host")</label>
<label for="@Html.FieldIdFor(m => m.RequestUrlHost)">@T("Host")</label>
@Html.TextBoxFor(m => m.RequestUrlHost, new {@class = "textMedium"})
<span class="hint">@T("Example: If host is \"orchardproject.net\", the tenant site URL is \"http://orchardproject.net/\"")</span>
</div>