mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +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.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);
|
||||||
}
|
}
|
||||||
|
@@ -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,10 +50,16 @@ 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;
|
||||||
|
// ignore the rule if it fails to execute
|
||||||
|
try {
|
||||||
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) {
|
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) {
|
||||||
activeLayerIds.Add(activeLayer.ContentItem.Id);
|
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.
|
// Build and add shape to zone.
|
||||||
var zones = workContext.Layout.Zones;
|
var zones = workContext.Layout.Zones;
|
||||||
|
Reference in New Issue
Block a user