mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Adding validation logic and syntax error management in layer rules
--HG-- branch : dev
This commit is contained in:
@@ -7,6 +7,7 @@ using Orchard.Core.Contents.Controllers;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Admin;
|
||||
using Orchard.UI.Notify;
|
||||
using Orchard.UI.Widgets;
|
||||
using Orchard.Widgets.Models;
|
||||
using Orchard.Widgets.Services;
|
||||
using Orchard.Widgets.ViewModels;
|
||||
@@ -19,13 +20,16 @@ namespace Orchard.Widgets.Controllers {
|
||||
private const string NotAuthorizedManageWidgetsLabel = "Not authorized to manage widgets";
|
||||
|
||||
private readonly IWidgetsService _widgetsService;
|
||||
private readonly IRuleManager _ruleManager;
|
||||
|
||||
public AdminController(
|
||||
IOrchardServices services,
|
||||
IWidgetsService widgetsService) {
|
||||
IWidgetsService widgetsService,
|
||||
IRuleManager ruleManager) {
|
||||
|
||||
Services = services;
|
||||
_widgetsService = widgetsService;
|
||||
_ruleManager = ruleManager;
|
||||
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
@@ -171,6 +175,9 @@ namespace Orchard.Widgets.Controllers {
|
||||
return HttpNotFound();
|
||||
|
||||
var model = Services.ContentManager.UpdateEditor(layerPart, this);
|
||||
|
||||
ValidateLayer(layerPart);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(model);
|
||||
@@ -216,6 +223,9 @@ namespace Orchard.Widgets.Controllers {
|
||||
return HttpNotFound();
|
||||
|
||||
var model = Services.ContentManager.UpdateEditor(layerPart, this);
|
||||
|
||||
ValidateLayer(layerPart);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(model);
|
||||
@@ -325,6 +335,27 @@ namespace Orchard.Widgets.Controllers {
|
||||
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) {
|
||||
return base.TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
||||
}
|
||||
|
@@ -1,7 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.UI.Admin;
|
||||
using Orchard.UI.Widgets;
|
||||
@@ -17,8 +20,13 @@ namespace Orchard.Widgets.Filters {
|
||||
_contentManager = contentManager;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_ruleManager = ruleManager;
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
public Localizer T { get; private set; }
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
// layers and widgets should only run on a full view rendering result
|
||||
var viewResult = filterContext.Result as ViewResult;
|
||||
@@ -42,10 +50,16 @@ namespace Orchard.Widgets.Filters {
|
||||
List<int> activeLayerIds = new List<int>();
|
||||
foreach (var activeLayer in activeLayers) {
|
||||
var context = workContext.HttpContext;
|
||||
// ignore the rule if it fails to execute
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Build and add shape to zone.
|
||||
var zones = workContext.Layout.Zones;
|
||||
|
Reference in New Issue
Block a user