mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +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);
|
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());
|
// html.Zone(zoneName, () => html.RenderBody());
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
@@ -24,8 +24,8 @@ namespace Orchard.Mvc.ViewEngines {
|
|||||||
var skipLayoutViewEngine = false;
|
var skipLayoutViewEngine = false;
|
||||||
if (string.IsNullOrEmpty(masterName) == false)
|
if (string.IsNullOrEmpty(masterName) == false)
|
||||||
skipLayoutViewEngine = true;
|
skipLayoutViewEngine = true;
|
||||||
//if (!ThemeFilter.IsApplied(controllerContext.RequestContext))
|
if (!ThemeFilter.IsApplied(controllerContext.RequestContext))
|
||||||
// skipLayoutViewEngine = true;
|
skipLayoutViewEngine = true;
|
||||||
if (_viewEngines == null || _viewEngines.Count == 0)
|
if (_viewEngines == null || _viewEngines.Count == 0)
|
||||||
skipLayoutViewEngine = true;
|
skipLayoutViewEngine = true;
|
||||||
if (skipLayoutViewEngine)
|
if (skipLayoutViewEngine)
|
||||||
|
@@ -407,6 +407,7 @@
|
|||||||
<Compile Include="Environment\IShellContainerRegistrations.cs" />
|
<Compile Include="Environment\IShellContainerRegistrations.cs" />
|
||||||
<Compile Include="FileSystems\Dependencies\DynamicModuleVirtualPathProvider.cs" />
|
<Compile Include="FileSystems\Dependencies\DynamicModuleVirtualPathProvider.cs" />
|
||||||
<Compile Include="IWorkContextAccessor.cs" />
|
<Compile Include="IWorkContextAccessor.cs" />
|
||||||
|
<Compile Include="WorkContextExtensions.cs" />
|
||||||
<Compile Include="Mvc\Html\Shapes.cs" />
|
<Compile Include="Mvc\Html\Shapes.cs" />
|
||||||
<Compile Include="Mvc\ViewEngines\Razor\RazorCompilationEventsShim.cs" />
|
<Compile Include="Mvc\ViewEngines\Razor\RazorCompilationEventsShim.cs" />
|
||||||
<Compile Include="Mvc\ViewEngines\Razor\RazorViewEngineProvider.cs" />
|
<Compile Include="Mvc\ViewEngines\Razor\RazorViewEngineProvider.cs" />
|
||||||
@@ -827,6 +828,7 @@
|
|||||||
<Name>ClaySharp</Name>
|
<Name>ClaySharp</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- 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.
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
@@ -2,39 +2,21 @@
|
|||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
using Orchard.Mvc.Filters;
|
using Orchard.Mvc.Filters;
|
||||||
using Orchard.Mvc.ViewModels;
|
|
||||||
|
|
||||||
namespace Orchard.Security {
|
namespace Orchard.Security {
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class SecurityFilter : FilterProvider, IResultFilter, IExceptionFilter {
|
public class SecurityFilter : FilterProvider, IExceptionFilter {
|
||||||
private readonly IAuthenticationService _authenticationService;
|
public SecurityFilter() {
|
||||||
|
|
||||||
public SecurityFilter(IAuthenticationService authenticationService) {
|
|
||||||
_authenticationService = authenticationService;
|
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ILogger Logger { get; set; }
|
public ILogger Logger { get; set; }
|
||||||
|
|
||||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
|
||||||
#if REFACTORING
|
#if REFACTORING
|
||||||
var baseViewModel = BaseViewModel.From(filterContext.Result);
|
|
||||||
if (baseViewModel == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (baseViewModel.CurrentUser == null)
|
|
||||||
baseViewModel.CurrentUser = _authenticationService.GetAuthenticatedUser();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnException(ExceptionContext filterContext) {
|
public void OnException(ExceptionContext filterContext) {
|
||||||
if (!(filterContext.Exception is OrchardSecurityException)) {
|
if (!(filterContext.Exception is OrchardSecurityException))
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Logger.Information(filterContext.Exception, "Security exception converted to access denied result");
|
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.Mvc;
|
||||||
using System.Web.Routing;
|
using System.Web.Routing;
|
||||||
using Orchard.Mvc.Filters;
|
using Orchard.Mvc.Filters;
|
||||||
using Orchard.Mvc.ViewModels;
|
|
||||||
|
|
||||||
namespace Orchard.Themes {
|
namespace Orchard.Themes {
|
||||||
public class ThemeFilter : FilterProvider, IActionFilter, IResultFilter {
|
public class ThemeFilter : FilterProvider, IActionFilter, IResultFilter {
|
||||||
|
|
||||||
|
|
||||||
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
||||||
var attribute = GetThemedAttribute(filterContext.ActionDescriptor);
|
var attribute = GetThemedAttribute(filterContext.ActionDescriptor);
|
||||||
if (attribute != null && attribute.Enabled) {
|
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) {
|
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||||
#if REFACTORING
|
#if REFACTORING
|
||||||
@@ -24,17 +20,11 @@ namespace Orchard.Themes {
|
|||||||
if (viewResult == null)
|
if (viewResult == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var model = viewResult.ViewData.Model as BaseViewModel;
|
|
||||||
if (model == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Apply(filterContext.RequestContext);
|
Apply(filterContext.RequestContext);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Apply(RequestContext context) {
|
public static void Apply(RequestContext context) {
|
||||||
// the value isn't important
|
// the value isn't important
|
||||||
@@ -45,13 +35,11 @@ namespace Orchard.Themes {
|
|||||||
return context.HttpContext.Items.Contains(typeof (ThemeFilter));
|
return context.HttpContext.Items.Contains(typeof (ThemeFilter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static ThemedAttribute GetThemedAttribute(ActionDescriptor descriptor) {
|
private static ThemedAttribute GetThemedAttribute(ActionDescriptor descriptor) {
|
||||||
return descriptor.GetCustomAttributes(typeof (ThemedAttribute), true)
|
return descriptor.GetCustomAttributes(typeof (ThemedAttribute), true)
|
||||||
.Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof (ThemedAttribute), true))
|
.Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof (ThemedAttribute), true))
|
||||||
.OfType<ThemedAttribute>()
|
.OfType<ThemedAttribute>()
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -28,10 +28,9 @@ namespace Orchard.UI.Admin.Notification {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var shape = _shapeHelperFactory.CreateHelper();
|
var shape = _shapeHelperFactory.CreateHelper();
|
||||||
foreach(var messageEntry in messageEntries) {
|
var messagesZone = _workContextAccessor.GetContext(filterContext).CurrentPage.Zones["Messages"];
|
||||||
_workContextAccessor.GetContext(filterContext.RequestContext.HttpContext).CurrentPage.Zones["Messages"].Add(shape.Message(messageEntry));
|
foreach(var messageEntry in messageEntries)
|
||||||
// need extension to enable -> _workContextAccessor.GetContext(filterContext).CurrentPage.Zones["Messages"].Add(shape.Message());
|
messagesZone.Add(shape.Message(messageEntry));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
using Orchard.UI.Zones;
|
using System;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
|
||||||
namespace Orchard.UI {
|
namespace Orchard.UI {
|
||||||
public interface IPage {
|
public interface IPage {
|
||||||
|
|
||||||
IZoneCollection Zones { get; }
|
IZoneCollection Zones { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,5 +13,7 @@ namespace Orchard.UI {
|
|||||||
public interface IZone {
|
public interface IZone {
|
||||||
void Add(object item);
|
void Add(object item);
|
||||||
void Add(object item, string position);
|
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 System.Web.Mvc;
|
||||||
|
using Orchard.DisplayManagement;
|
||||||
using Orchard.Mvc.Filters;
|
using Orchard.Mvc.Filters;
|
||||||
using Orchard.Mvc.ViewModels;
|
|
||||||
using Orchard.UI.Admin;
|
using Orchard.UI.Admin;
|
||||||
|
|
||||||
namespace Orchard.UI.Navigation {
|
namespace Orchard.UI.Navigation {
|
||||||
public class MenuFilter : FilterProvider, IResultFilter {
|
public class MenuFilter : FilterProvider, IResultFilter {
|
||||||
private readonly INavigationManager _navigationManager;
|
private readonly INavigationManager _navigationManager;
|
||||||
private readonly IWorkContextAccessor _workContextAccessor;
|
private readonly IWorkContextAccessor _workContextAccessor;
|
||||||
|
private readonly IShapeHelperFactory _shapeHelperFactory;
|
||||||
|
|
||||||
public MenuFilter(INavigationManager navigationManager, IWorkContextAccessor workContextAccessor) {
|
public MenuFilter(INavigationManager navigationManager, IWorkContextAccessor workContextAccessor, IShapeHelperFactory shapeHelperFactory) {
|
||||||
_navigationManager = navigationManager;
|
_navigationManager = navigationManager;
|
||||||
_workContextAccessor = workContextAccessor;
|
_workContextAccessor = workContextAccessor;
|
||||||
|
_shapeHelperFactory = shapeHelperFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||||
#if REFACTORING
|
|
||||||
var baseViewModel = BaseViewModel.From(filterContext.Result);
|
|
||||||
if (baseViewModel == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var menuName = "main";
|
var menuName = "main";
|
||||||
if (AdminFilter.IsApplied(filterContext.RequestContext))
|
if (AdminFilter.IsApplied(filterContext.RequestContext))
|
||||||
menuName = "admin";
|
menuName = "admin";
|
||||||
|
|
||||||
baseViewModel.Menu = _navigationManager.BuildMenu(menuName);
|
//todo: (heskew) does the menu need to be on Page?
|
||||||
#endif
|
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.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
|
using Orchard.DisplayManagement;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Mvc.Filters;
|
using Orchard.Mvc.Filters;
|
||||||
using Orchard.Mvc.ViewModels;
|
|
||||||
|
|
||||||
namespace Orchard.UI.Notify {
|
namespace Orchard.UI.Notify {
|
||||||
public class NotifyFilter : FilterProvider, IActionFilter, IResultFilter {
|
public class NotifyFilter : FilterProvider, IActionFilter, IResultFilter {
|
||||||
private const string TempDataMessages = "messages";
|
private const string TempDataMessages = "messages";
|
||||||
private readonly INotifier _notifier;
|
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;
|
_notifier = notifier;
|
||||||
|
_workContextAccessor = workContextAccessor;
|
||||||
|
_shapeHelperFactory = shapeHelperFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
||||||
@@ -54,11 +58,6 @@ namespace Orchard.UI.Notify {
|
|||||||
if (viewResult == null)
|
if (viewResult == null)
|
||||||
return;
|
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]);
|
var messages = Convert.ToString(viewResult.TempData[TempDataMessages]);
|
||||||
if (string.IsNullOrEmpty(messages))
|
if (string.IsNullOrEmpty(messages))
|
||||||
return;// nothing to do, really
|
return;// nothing to do, really
|
||||||
@@ -81,13 +80,20 @@ namespace Orchard.UI.Notify {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
baseViewModel.Messages = baseViewModel.Messages == null ? messageEntries : baseViewModel.Messages.Union(messageEntries).ToList();
|
if (!messageEntries.Any())
|
||||||
baseViewModel.Zones.AddRenderPartial("content:before", "Messages", baseViewModel.Messages);
|
return;
|
||||||
#endif
|
#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 System.Web.Mvc;
|
||||||
using Orchard.Mvc.Filters;
|
using Orchard.Mvc.Filters;
|
||||||
using Orchard.Mvc.ViewEngines;
|
using Orchard.Mvc.ViewEngines;
|
||||||
using Orchard.Mvc.ViewModels;
|
|
||||||
|
|
||||||
namespace Orchard.UI.Resources {
|
namespace Orchard.UI.Resources {
|
||||||
#if REFACTORING
|
#if REFACTORING
|
||||||
public class ResourceFilter : FilterProvider, IResultFilter {
|
public class ResourceFilter : FilterProvider, IResultFilter {
|
||||||
private readonly IResourceManager _resourceManager;
|
private readonly IResourceManager _resourceManager;
|
||||||
|
private readonly IWorkContextAccessor _workContextAccessor;
|
||||||
|
|
||||||
public ResourceFilter(IResourceManager resourceManager) {
|
public ResourceFilter(IResourceManager resourceManager, IWorkContextAccessor workContextAccessor) {
|
||||||
_resourceManager = resourceManager;
|
_resourceManager = resourceManager;
|
||||||
|
_workContextAccessor = workContextAccessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||||
var model = BaseViewModel.From(filterContext.Result);
|
var headZone = _workContextAccessor.GetContext().CurrentPage.Zones["Head"];
|
||||||
if (model == null) {
|
headZone.Add(html => html.ViewContext.Writer.Write(_resourceManager.GetMetas()), ":metas");
|
||||||
return;
|
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()));
|
_workContextAccessor.GetContext().CurrentPage.Zones["Body"].Add(
|
||||||
model.Zones.AddAction("head:styles", html => html.ViewContext.Writer.Write(_resourceManager.GetStyles()));
|
html => {
|
||||||
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());
|
html.ViewContext.Writer.Write(_resourceManager.GetFootScripts());
|
||||||
TextWriter captured;
|
TextWriter captured;
|
||||||
if (LayoutViewContext.From(html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured)) {
|
if (LayoutViewContext.From(html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured))
|
||||||
html.ViewContext.Writer.Write(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