mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactored LayerPart validation
- Log syntax errors on Warning level - Move the validation to the Driver --HG-- branch : dev
This commit is contained in:
@@ -7,7 +7,6 @@ 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;
|
||||||
@@ -20,16 +19,13 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -179,8 +175,6 @@ namespace Orchard.Widgets.Controllers {
|
|||||||
|
|
||||||
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);
|
||||||
@@ -227,8 +221,6 @@ namespace Orchard.Widgets.Controllers {
|
|||||||
|
|
||||||
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);
|
||||||
@@ -339,27 +331,6 @@ namespace Orchard.Widgets.Controllers {
|
|||||||
RedirectToAction("Index");
|
RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ValidateLayer(LayerPart layer) {
|
|
||||||
if ( String.IsNullOrWhiteSpace(layer.LayerRule) ) {
|
|
||||||
layer.LayerRule = "true";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(_widgetsService.GetLayers().Count(l => String.Equals(l.Name, layer.Name, StringComparison.InvariantCultureIgnoreCase)) > 1) { // the current layer counts for 1
|
|
||||||
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,12 +1,31 @@
|
|||||||
using JetBrains.Annotations;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.ContentManagement.Drivers;
|
using Orchard.ContentManagement.Drivers;
|
||||||
|
using Orchard.Localization;
|
||||||
|
using Orchard.UI.Widgets;
|
||||||
using Orchard.Widgets.Models;
|
using Orchard.Widgets.Models;
|
||||||
|
using Orchard.Widgets.Services;
|
||||||
|
|
||||||
namespace Orchard.Widgets.Drivers {
|
namespace Orchard.Widgets.Drivers {
|
||||||
|
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class LayerPartDriver : ContentPartDriver<LayerPart> {
|
public class LayerPartDriver : ContentPartDriver<LayerPart> {
|
||||||
|
private readonly IRuleManager _ruleManager;
|
||||||
|
private readonly IWidgetsService _widgetsService;
|
||||||
|
|
||||||
|
public LayerPartDriver(
|
||||||
|
IRuleManager ruleManager,
|
||||||
|
IWidgetsService widgetsService) {
|
||||||
|
|
||||||
|
_ruleManager = ruleManager;
|
||||||
|
_widgetsService = widgetsService;
|
||||||
|
|
||||||
|
T = NullLocalizer.Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
protected override DriverResult Editor(LayerPart layerPart, dynamic shapeHelper) {
|
protected override DriverResult Editor(LayerPart layerPart, dynamic shapeHelper) {
|
||||||
return ContentShape("Parts_Widgets_LayerPart",
|
return ContentShape("Parts_Widgets_LayerPart",
|
||||||
@@ -14,7 +33,23 @@ namespace Orchard.Widgets.Drivers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected override DriverResult Editor(LayerPart layerPart, IUpdateModel updater, dynamic shapeHelper) {
|
protected override DriverResult Editor(LayerPart layerPart, IUpdateModel updater, dynamic shapeHelper) {
|
||||||
updater.TryUpdateModel(layerPart, Prefix, null, null);
|
if(updater.TryUpdateModel(layerPart, Prefix, null, null)) {
|
||||||
|
if ( String.IsNullOrWhiteSpace(layerPart.LayerRule) ) {
|
||||||
|
layerPart.LayerRule = "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( _widgetsService.GetLayers().Any(l => String.Equals(l.Name, layerPart.Name, StringComparison.InvariantCultureIgnoreCase))) {
|
||||||
|
updater.AddModelError("Name", T("A Layer with the same name already exists"));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
_ruleManager.Matches(layerPart.LayerRule);
|
||||||
|
}
|
||||||
|
catch ( Exception e ) {
|
||||||
|
updater.AddModelError("Description", T("The rule is not valid: {0}", e.Message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Editor(layerPart, shapeHelper);
|
return Editor(layerPart, shapeHelper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,9 +47,8 @@ namespace Orchard.Widgets.Filters {
|
|||||||
IEnumerable<WidgetPart> widgetParts = _contentManager.Query<WidgetPart, WidgetPartRecord>().List();
|
IEnumerable<WidgetPart> widgetParts = _contentManager.Query<WidgetPart, WidgetPartRecord>().List();
|
||||||
IEnumerable<LayerPart> activeLayers = _contentManager.Query<LayerPart, LayerPartRecord>().List();
|
IEnumerable<LayerPart> activeLayers = _contentManager.Query<LayerPart, LayerPartRecord>().List();
|
||||||
|
|
||||||
List<int> activeLayerIds = new List<int>();
|
var activeLayerIds = new List<int>();
|
||||||
foreach (var activeLayer in activeLayers) {
|
foreach (var activeLayer in activeLayers) {
|
||||||
var context = workContext.HttpContext;
|
|
||||||
// ignore the rule if it fails to execute
|
// ignore the rule if it fails to execute
|
||||||
try {
|
try {
|
||||||
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) {
|
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) {
|
||||||
@@ -57,7 +56,7 @@ namespace Orchard.Widgets.Filters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception e) {
|
catch(Exception e) {
|
||||||
Logger.Debug(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
|
Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user