mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:36:18 +08:00
Refactoring framework (action) filters to use IWorkContextAccessor instead of BaseViewModel
--HG-- branch : dev
This commit is contained in:
@@ -86,7 +86,7 @@ namespace Orchard.Mvc.Html {
|
||||
manager.Render(html, html.ViewData.Model.Zones, null, null, except);
|
||||
}
|
||||
|
||||
//public static void ZoneBody<TModel>(this HtmlHelper<TModel> html, string zoneName) where TModel : BaseViewModel {
|
||||
//public static void ZoneBody<TModel>(this HtmlHelper<TModel> html, string zoneName) where TModel {
|
||||
// html.Zone(zoneName, () => html.RenderBody());
|
||||
//}
|
||||
|
||||
|
@@ -24,8 +24,8 @@ namespace Orchard.Mvc.ViewEngines {
|
||||
var skipLayoutViewEngine = false;
|
||||
if (string.IsNullOrEmpty(masterName) == false)
|
||||
skipLayoutViewEngine = true;
|
||||
//if (!ThemeFilter.IsApplied(controllerContext.RequestContext))
|
||||
// skipLayoutViewEngine = true;
|
||||
if (!ThemeFilter.IsApplied(controllerContext.RequestContext))
|
||||
skipLayoutViewEngine = true;
|
||||
if (_viewEngines == null || _viewEngines.Count == 0)
|
||||
skipLayoutViewEngine = true;
|
||||
if (skipLayoutViewEngine)
|
||||
|
@@ -407,6 +407,7 @@
|
||||
<Compile Include="Environment\IShellContainerRegistrations.cs" />
|
||||
<Compile Include="FileSystems\Dependencies\DynamicModuleVirtualPathProvider.cs" />
|
||||
<Compile Include="IWorkContextAccessor.cs" />
|
||||
<Compile Include="WorkContextExtensions.cs" />
|
||||
<Compile Include="Mvc\Html\Shapes.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\Razor\RazorCompilationEventsShim.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\Razor\RazorViewEngineProvider.cs" />
|
||||
@@ -827,6 +828,7 @@
|
||||
<Name>ClaySharp</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
@@ -2,39 +2,21 @@
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Security {
|
||||
[UsedImplicitly]
|
||||
public class SecurityFilter : FilterProvider, IResultFilter, IExceptionFilter {
|
||||
private readonly IAuthenticationService _authenticationService;
|
||||
|
||||
public SecurityFilter(IAuthenticationService authenticationService) {
|
||||
_authenticationService = authenticationService;
|
||||
public class SecurityFilter : FilterProvider, IExceptionFilter {
|
||||
public SecurityFilter() {
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
#if REFACTORING
|
||||
var baseViewModel = BaseViewModel.From(filterContext.Result);
|
||||
if (baseViewModel == null)
|
||||
return;
|
||||
|
||||
if (baseViewModel.CurrentUser == null)
|
||||
baseViewModel.CurrentUser = _authenticationService.GetAuthenticatedUser();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
|
||||
}
|
||||
|
||||
public void OnException(ExceptionContext filterContext) {
|
||||
if (!(filterContext.Exception is OrchardSecurityException)) {
|
||||
if (!(filterContext.Exception is OrchardSecurityException))
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Logger.Information(filterContext.Exception, "Security exception converted to access denied result");
|
||||
|
@@ -2,12 +2,9 @@ using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Themes {
|
||||
public class ThemeFilter : FilterProvider, IActionFilter, IResultFilter {
|
||||
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
||||
var attribute = GetThemedAttribute(filterContext.ActionDescriptor);
|
||||
if (attribute != null && attribute.Enabled) {
|
||||
@@ -15,8 +12,7 @@ namespace Orchard.Themes {
|
||||
}
|
||||
}
|
||||
|
||||
public void OnActionExecuted(ActionExecutedContext filterContext) {
|
||||
}
|
||||
public void OnActionExecuted(ActionExecutedContext filterContext) {}
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
#if REFACTORING
|
||||
@@ -24,34 +20,26 @@ namespace Orchard.Themes {
|
||||
if (viewResult == null)
|
||||
return;
|
||||
|
||||
var model = viewResult.ViewData.Model as BaseViewModel;
|
||||
if (model == null)
|
||||
return;
|
||||
|
||||
Apply(filterContext.RequestContext);
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
|
||||
}
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||
|
||||
public static void Apply(RequestContext context) {
|
||||
// the value isn't important
|
||||
context.HttpContext.Items[typeof(ThemeFilter)] = null;
|
||||
context.HttpContext.Items[typeof (ThemeFilter)] = null;
|
||||
}
|
||||
|
||||
public static bool IsApplied(RequestContext context) {
|
||||
return context.HttpContext.Items.Contains(typeof(ThemeFilter));
|
||||
return context.HttpContext.Items.Contains(typeof (ThemeFilter));
|
||||
}
|
||||
|
||||
|
||||
private static ThemedAttribute GetThemedAttribute(ActionDescriptor descriptor) {
|
||||
return descriptor.GetCustomAttributes(typeof(ThemedAttribute), true)
|
||||
.Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof(ThemedAttribute), true))
|
||||
return descriptor.GetCustomAttributes(typeof (ThemedAttribute), true)
|
||||
.Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof (ThemedAttribute), true))
|
||||
.OfType<ThemedAttribute>()
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -28,10 +28,9 @@ namespace Orchard.UI.Admin.Notification {
|
||||
return;
|
||||
|
||||
var shape = _shapeHelperFactory.CreateHelper();
|
||||
foreach(var messageEntry in messageEntries) {
|
||||
_workContextAccessor.GetContext(filterContext.RequestContext.HttpContext).CurrentPage.Zones["Messages"].Add(shape.Message(messageEntry));
|
||||
// need extension to enable -> _workContextAccessor.GetContext(filterContext).CurrentPage.Zones["Messages"].Add(shape.Message());
|
||||
}
|
||||
var messagesZone = _workContextAccessor.GetContext(filterContext).CurrentPage.Zones["Messages"];
|
||||
foreach(var messageEntry in messageEntries)
|
||||
messagesZone.Add(shape.Message(messageEntry));
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
using Orchard.UI.Zones;
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Orchard.UI {
|
||||
public interface IPage {
|
||||
|
||||
IZoneCollection Zones { get; }
|
||||
}
|
||||
|
||||
@@ -13,5 +13,7 @@ namespace Orchard.UI {
|
||||
public interface IZone {
|
||||
void Add(object item);
|
||||
void Add(object item, string position);
|
||||
void Add(Action<HtmlHelper> action);
|
||||
void Add(Action<HtmlHelper> action, string position);
|
||||
}
|
||||
}
|
@@ -1,34 +1,30 @@
|
||||
using System.Web.Mvc;
|
||||
using Orchard.DisplayManagement;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.UI.Admin;
|
||||
|
||||
namespace Orchard.UI.Navigation {
|
||||
public class MenuFilter : FilterProvider, IResultFilter {
|
||||
private readonly INavigationManager _navigationManager;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IShapeHelperFactory _shapeHelperFactory;
|
||||
|
||||
public MenuFilter(INavigationManager navigationManager, IWorkContextAccessor workContextAccessor) {
|
||||
public MenuFilter(INavigationManager navigationManager, IWorkContextAccessor workContextAccessor, IShapeHelperFactory shapeHelperFactory) {
|
||||
_navigationManager = navigationManager;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_shapeHelperFactory = shapeHelperFactory;
|
||||
}
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
#if REFACTORING
|
||||
var baseViewModel = BaseViewModel.From(filterContext.Result);
|
||||
if (baseViewModel == null)
|
||||
return;
|
||||
|
||||
var menuName = "main";
|
||||
if (AdminFilter.IsApplied(filterContext.RequestContext))
|
||||
menuName = "admin";
|
||||
|
||||
baseViewModel.Menu = _navigationManager.BuildMenu(menuName);
|
||||
#endif
|
||||
//todo: (heskew) does the menu need to be on Page?
|
||||
var shape = _shapeHelperFactory.CreateHelper();
|
||||
_workContextAccessor.GetContext(filterContext).CurrentPage.Zones["Navigation"].Add(shape.Menu(_navigationManager.BuildMenu(menuName)));
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
|
||||
}
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||
}
|
||||
}
|
@@ -3,17 +3,21 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.DisplayManagement;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.UI.Notify {
|
||||
public class NotifyFilter : FilterProvider, IActionFilter, IResultFilter {
|
||||
private const string TempDataMessages = "messages";
|
||||
private readonly INotifier _notifier;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IShapeHelperFactory _shapeHelperFactory;
|
||||
|
||||
public NotifyFilter(INotifier notifier) {
|
||||
public NotifyFilter(INotifier notifier, IWorkContextAccessor workContextAccessor, IShapeHelperFactory shapeHelperFactory) {
|
||||
_notifier = notifier;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_shapeHelperFactory = shapeHelperFactory;
|
||||
}
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
||||
@@ -54,11 +58,6 @@ namespace Orchard.UI.Notify {
|
||||
if (viewResult == null)
|
||||
return;
|
||||
|
||||
var baseViewModel = BaseViewModel.From(viewResult);
|
||||
// if it's not a view model that holds messages, don't touch temp data either
|
||||
if (baseViewModel == null)
|
||||
return;
|
||||
|
||||
var messages = Convert.ToString(viewResult.TempData[TempDataMessages]);
|
||||
if (string.IsNullOrEmpty(messages))
|
||||
return;// nothing to do, really
|
||||
@@ -81,13 +80,20 @@ namespace Orchard.UI.Notify {
|
||||
}
|
||||
}
|
||||
|
||||
baseViewModel.Messages = baseViewModel.Messages == null ? messageEntries : baseViewModel.Messages.Union(messageEntries).ToList();
|
||||
baseViewModel.Zones.AddRenderPartial("content:before", "Messages", baseViewModel.Messages);
|
||||
if (!messageEntries.Any())
|
||||
return;
|
||||
#endif
|
||||
|
||||
var shape = _shapeHelperFactory.CreateHelper();
|
||||
var messagesZone = _workContextAccessor.GetContext(filterContext).CurrentPage.Zones["Messages"];
|
||||
foreach(var messageEntry in messageEntries)
|
||||
messagesZone.Add(shape.Message(messageEntry));
|
||||
|
||||
//todo: (heskew) probably need to keep duplicate messages from being pushed into the zone like the previous behavior
|
||||
//baseViewModel.Messages = baseViewModel.Messages == null ? messageEntries .Messages.Union(messageEntries).ToList();
|
||||
//baseViewModel.Zones.AddRenderPartial("content:before", "Messages", baseViewModel.Messages);
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
|
||||
}
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||
}
|
||||
}
|
@@ -2,38 +2,36 @@ using System.IO;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewEngines;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.UI.Resources {
|
||||
#if REFACTORING
|
||||
public class ResourceFilter : FilterProvider, IResultFilter {
|
||||
private readonly IResourceManager _resourceManager;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
|
||||
public ResourceFilter(IResourceManager resourceManager) {
|
||||
public ResourceFilter(IResourceManager resourceManager, IWorkContextAccessor workContextAccessor) {
|
||||
_resourceManager = resourceManager;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
}
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
var model = BaseViewModel.From(filterContext.Result);
|
||||
if (model == null) {
|
||||
return;
|
||||
}
|
||||
var headZone = _workContextAccessor.GetContext().CurrentPage.Zones["Head"];
|
||||
headZone.Add(html => html.ViewContext.Writer.Write(_resourceManager.GetMetas()), ":metas");
|
||||
headZone.Add(html => html.ViewContext.Writer.Write(_resourceManager.GetStyles()), ":styles");
|
||||
headZone.Add(html => html.ViewContext.Writer.Write(_resourceManager.GetLinks(html)), ":links");
|
||||
headZone.Add(html => html.ViewContext.Writer.Write(_resourceManager.GetHeadScripts()), ":scripts");
|
||||
|
||||
model.Zones.AddAction("head:metas", html => html.ViewContext.Writer.Write(_resourceManager.GetMetas()));
|
||||
model.Zones.AddAction("head:styles", html => html.ViewContext.Writer.Write(_resourceManager.GetStyles()));
|
||||
model.Zones.AddAction("head:links", html => html.ViewContext.Writer.Write(_resourceManager.GetLinks(html)));
|
||||
model.Zones.AddAction("head:scripts", html => html.ViewContext.Writer.Write(_resourceManager.GetHeadScripts()));
|
||||
model.Zones.AddAction("body:after", html => {
|
||||
html.ViewContext.Writer.Write(_resourceManager.GetFootScripts());
|
||||
TextWriter captured;
|
||||
if (LayoutViewContext.From(html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured)) {
|
||||
html.ViewContext.Writer.Write(captured);
|
||||
}
|
||||
});
|
||||
_workContextAccessor.GetContext().CurrentPage.Zones["Body"].Add(
|
||||
html => {
|
||||
html.ViewContext.Writer.Write(_resourceManager.GetFootScripts());
|
||||
TextWriter captured;
|
||||
if (LayoutViewContext.From(html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured))
|
||||
html.ViewContext.Writer.Write(captured);
|
||||
},
|
||||
":after");
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
}
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
9
src/Orchard/WorkContextExtensions.cs
Normal file
9
src/Orchard/WorkContextExtensions.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Orchard {
|
||||
public static class WorkContextExtensions {
|
||||
public static WorkContext GetContext(this IWorkContextAccessor workContextAccessor, ControllerContext controllerContext) {
|
||||
return workContextAccessor.GetContext(controllerContext.RequestContext.HttpContext);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user