Adding validation logic and syntax error management in layer rules

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-10-19 12:01:58 -07:00
parent 37b4735fa2
commit 99924a9279
2 changed files with 49 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ using Orchard.Core.Contents.Controllers;
using Orchard.Localization; using Orchard.Localization;
using Orchard.UI.Admin; using Orchard.UI.Admin;
using Orchard.UI.Notify; using Orchard.UI.Notify;
using Orchard.UI.Widgets;
using Orchard.Widgets.Models; using Orchard.Widgets.Models;
using Orchard.Widgets.Services; using Orchard.Widgets.Services;
using Orchard.Widgets.ViewModels; using Orchard.Widgets.ViewModels;
@@ -19,13 +20,16 @@ namespace Orchard.Widgets.Controllers {
private const string NotAuthorizedManageWidgetsLabel = "Not authorized to manage widgets"; private const string NotAuthorizedManageWidgetsLabel = "Not authorized to manage widgets";
private readonly IWidgetsService _widgetsService; private readonly IWidgetsService _widgetsService;
private readonly IRuleManager _ruleManager;
public AdminController( public AdminController(
IOrchardServices services, IOrchardServices services,
IWidgetsService widgetsService) { IWidgetsService widgetsService,
IRuleManager ruleManager) {
Services = services; Services = services;
_widgetsService = widgetsService; _widgetsService = widgetsService;
_ruleManager = ruleManager;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
} }
@@ -171,6 +175,9 @@ namespace Orchard.Widgets.Controllers {
return HttpNotFound(); return HttpNotFound();
var model = Services.ContentManager.UpdateEditor(layerPart, this); var model = Services.ContentManager.UpdateEditor(layerPart, this);
ValidateLayer(layerPart);
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
Services.TransactionManager.Cancel(); Services.TransactionManager.Cancel();
return View(model); return View(model);
@@ -216,6 +223,9 @@ namespace Orchard.Widgets.Controllers {
return HttpNotFound(); return HttpNotFound();
var model = Services.ContentManager.UpdateEditor(layerPart, this); var model = Services.ContentManager.UpdateEditor(layerPart, this);
ValidateLayer(layerPart);
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
Services.TransactionManager.Cancel(); Services.TransactionManager.Cancel();
return View(model); return View(model);
@@ -325,6 +335,27 @@ namespace Orchard.Widgets.Controllers {
RedirectToAction("Index"); RedirectToAction("Index");
} }
public bool ValidateLayer(LayerPart layer) {
if ( String.IsNullOrWhiteSpace(layer.LayerRule) ) {
layer.LayerRule = "true";
}
if(_widgetsService.GetLayers().Any(l => String.CompareOrdinal(l.Name, layer.Name) == 0)) {
ModelState.AddModelError("Name", T("A Layer with the same name already exists").Text);
return false;
}
try {
_ruleManager.Matches(layer.LayerRule);
}
catch ( Exception e ) {
ModelState.AddModelError("Description", T("The rule is not valid: {0}", e.Message).Text);
return false;
}
return true;
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) { bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return base.TryUpdateModel(model, prefix, includeProperties, excludeProperties); return base.TryUpdateModel(model, prefix, includeProperties, excludeProperties);
} }

View File

@@ -1,7 +1,10 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects; using Orchard.ContentManagement.Aspects;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc.Filters; using Orchard.Mvc.Filters;
using Orchard.UI.Admin; using Orchard.UI.Admin;
using Orchard.UI.Widgets; using Orchard.UI.Widgets;
@@ -17,8 +20,13 @@ namespace Orchard.Widgets.Filters {
_contentManager = contentManager; _contentManager = contentManager;
_workContextAccessor = workContextAccessor; _workContextAccessor = workContextAccessor;
_ruleManager = ruleManager; _ruleManager = ruleManager;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
} }
public ILogger Logger { get; set; }
public Localizer T { get; private set; }
public void OnResultExecuting(ResultExecutingContext filterContext) { public void OnResultExecuting(ResultExecutingContext filterContext) {
// layers and widgets should only run on a full view rendering result // layers and widgets should only run on a full view rendering result
var viewResult = filterContext.Result as ViewResult; var viewResult = filterContext.Result as ViewResult;
@@ -42,8 +50,14 @@ namespace Orchard.Widgets.Filters {
List<int> activeLayerIds = new List<int>(); List<int> activeLayerIds = new List<int>();
foreach (var activeLayer in activeLayers) { foreach (var activeLayer in activeLayers) {
var context = workContext.HttpContext; var context = workContext.HttpContext;
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) { // ignore the rule if it fails to execute
activeLayerIds.Add(activeLayer.ContentItem.Id); try {
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) {
activeLayerIds.Add(activeLayer.ContentItem.Id);
}
}
catch(Exception e) {
Logger.Debug(e, T("An error occured during layer evaluation").Text);
} }
} }