From fb1f59a7bdff3f0e20a77990497146a7105728c0 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 28 Aug 2023 19:44:41 -0700 Subject: [PATCH 1/4] Add IsViewOrPageResult() extension --- .../OrchardCore.Admin/AdminMenuFilter.cs | 4 +--- .../OrchardCore.Facebook/Filters/FBInitFilter.cs | 8 +++----- .../OrchardCore.Facebook/Filters/FacebookPixelFilter.cs | 5 +---- .../Analytics/GoogleAnalyticsFilter.cs | 5 +---- .../TagManager/GoogleTagManagerFilter.cs | 5 +---- .../OrchardCore.Layers/Services/LayerFilter.cs | 5 +---- .../OrchardCore.MiniProfiler/MiniProfilerFilter.cs | 4 +--- .../Filters/NotificationResultFilter.cs | 4 +--- .../OrchardCore.ReCaptcha/ReCaptchaLoginFilter.cs | 4 +--- .../OrchardCore.Users/Filters/LoginMenuFilter.cs | 4 +--- .../Extensions/ResultExecutingContextExtensions.cs | 9 +++++++++ .../OrchardCore.DisplayManagement/Notify/NotifyFilter.cs | 2 +- 12 files changed, 22 insertions(+), 37 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs diff --git a/src/OrchardCore.Modules/OrchardCore.Admin/AdminMenuFilter.cs b/src/OrchardCore.Modules/OrchardCore.Admin/AdminMenuFilter.cs index 81ff997452c..6ac854384bd 100644 --- a/src/OrchardCore.Modules/OrchardCore.Admin/AdminMenuFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Admin/AdminMenuFilter.cs @@ -1,7 +1,5 @@ using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Layout; using OrchardCore.DisplayManagement.Shapes; @@ -27,7 +25,7 @@ public AdminMenuFilter(ILayoutAccessor layoutAccessor, public async Task OnResultExecutionAsync(ResultExecutingContext filterContext, ResultExecutionDelegate next) { // Should only run on a full view rendering result - if (filterContext.Result is not ViewResult && filterContext.Result is not PageResult) + if (!filterContext.IsViewOrPageResult()) { await next(); return; diff --git a/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FBInitFilter.cs b/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FBInitFilter.cs index 03609e35a27..7618d431c25 100644 --- a/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FBInitFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FBInitFilter.cs @@ -1,7 +1,6 @@ +using System; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.Admin; using OrchardCore.Entities; using OrchardCore.Facebook.Settings; @@ -26,12 +25,11 @@ public FBInitFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { // Should only run on the front-end for a full view - if ((context.Result is ViewResult || context.Result is PageResult) && - !AdminAttribute.IsApplied(context.HttpContext)) + if (context.IsViewOrPageResult() && !AdminAttribute.IsApplied(context.HttpContext)) { var site = (await _siteService.GetSiteSettingsAsync()); var settings = site.As(); - if (!string.IsNullOrWhiteSpace(settings?.AppId)) + if (!String.IsNullOrWhiteSpace(settings?.AppId)) { if (settings.FBInit) { diff --git a/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FacebookPixelFilter.cs b/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FacebookPixelFilter.cs index 08bd745b4db..88048afa91f 100644 --- a/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FacebookPixelFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Facebook/Filters/FacebookPixelFilter.cs @@ -2,9 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Http.Features; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.Admin; using OrchardCore.Entities; using OrchardCore.Facebook.Settings; @@ -32,8 +30,7 @@ public FacebookPixelFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { // Should only run on the front-end for a full view. - if ((context.Result is ViewResult || context.Result is PageResult) - && !AdminAttribute.IsApplied(context.HttpContext)) + if (context.IsViewOrPageResult() && !AdminAttribute.IsApplied(context.HttpContext)) { var canTrack = context.HttpContext.Features.Get()?.CanTrack ?? true; diff --git a/src/OrchardCore.Modules/OrchardCore.Google/Analytics/GoogleAnalyticsFilter.cs b/src/OrchardCore.Modules/OrchardCore.Google/Analytics/GoogleAnalyticsFilter.cs index 0f0e2f33132..b6a605222d6 100644 --- a/src/OrchardCore.Modules/OrchardCore.Google/Analytics/GoogleAnalyticsFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Google/Analytics/GoogleAnalyticsFilter.cs @@ -2,9 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Http.Features; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.Admin; using OrchardCore.Entities; using OrchardCore.Google.Analytics.Settings; @@ -31,8 +29,7 @@ public GoogleAnalyticsFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { // Should only run on the front-end for a full view - if ((context.Result is ViewResult || context.Result is PageResult) && - !AdminAttribute.IsApplied(context.HttpContext)) + if (context.IsViewOrPageResult() && !AdminAttribute.IsApplied(context.HttpContext)) { var canTrack = context.HttpContext.Features.Get()?.CanTrack ?? true; diff --git a/src/OrchardCore.Modules/OrchardCore.Google/TagManager/GoogleTagManagerFilter.cs b/src/OrchardCore.Modules/OrchardCore.Google/TagManager/GoogleTagManagerFilter.cs index 290f9aa4207..9b762f24ee2 100644 --- a/src/OrchardCore.Modules/OrchardCore.Google/TagManager/GoogleTagManagerFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Google/TagManager/GoogleTagManagerFilter.cs @@ -2,9 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Http.Features; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.Admin; using OrchardCore.Entities; using OrchardCore.Google.TagManager.Settings; @@ -31,8 +29,7 @@ public GoogleTagManagerFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { // Should only run on the front-end for a full view - if ((context.Result is ViewResult || context.Result is PageResult) && - !AdminAttribute.IsApplied(context.HttpContext)) + if (context.IsViewOrPageResult() && !AdminAttribute.IsApplied(context.HttpContext)) { var canTrack = context.HttpContext.Features.Get()?.CanTrack ?? true; diff --git a/src/OrchardCore.Modules/OrchardCore.Layers/Services/LayerFilter.cs b/src/OrchardCore.Modules/OrchardCore.Layers/Services/LayerFilter.cs index 0f9b5ad4288..7024fc41e4b 100644 --- a/src/OrchardCore.Modules/OrchardCore.Layers/Services/LayerFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Layers/Services/LayerFilter.cs @@ -1,9 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Caching.Memory; using OrchardCore.Admin; using OrchardCore.ContentManagement.Display; @@ -62,8 +60,7 @@ public LayerFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { // Should only run on the front-end for a full view - if ((context.Result is ViewResult || context.Result is PageResult) && - !AdminAttribute.IsApplied(context.HttpContext)) + if (context.IsViewOrPageResult() && !AdminAttribute.IsApplied(context.HttpContext)) { // Even if the Admin attribute is not applied we might be using the admin theme, for instance in Login views. // In this case don't render Layers. diff --git a/src/OrchardCore.Modules/OrchardCore.MiniProfiler/MiniProfilerFilter.cs b/src/OrchardCore.Modules/OrchardCore.MiniProfiler/MiniProfilerFilter.cs index d51a0e422fe..0e969bc221b 100644 --- a/src/OrchardCore.Modules/OrchardCore.MiniProfiler/MiniProfilerFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.MiniProfiler/MiniProfilerFilter.cs @@ -1,8 +1,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.Admin; using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Layout; @@ -31,7 +29,7 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE var viewMiniProfilerOnFrontEnd = await _authorizationService.AuthorizeAsync(context.HttpContext.User, Permissions.ViewMiniProfilerOnFrontEnd); var viewMiniProfilerOnBackEnd = await _authorizationService.AuthorizeAsync(context.HttpContext.User, Permissions.ViewMiniProfilerOnBackEnd); if ( - (context.Result is ViewResult || context.Result is PageResult) && + context.IsViewOrPageResult() && ( (viewMiniProfilerOnFrontEnd && !AdminAttribute.IsApplied(context.HttpContext)) || (viewMiniProfilerOnBackEnd && AdminAttribute.IsApplied(context.HttpContext)) diff --git a/src/OrchardCore.Modules/OrchardCore.Notifications/Filters/NotificationResultFilter.cs b/src/OrchardCore.Modules/OrchardCore.Notifications/Filters/NotificationResultFilter.cs index a44557e85bc..ee598546a2b 100644 --- a/src/OrchardCore.Modules/OrchardCore.Notifications/Filters/NotificationResultFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Notifications/Filters/NotificationResultFilter.cs @@ -3,9 +3,7 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Layout; using OrchardCore.DisplayManagement.ModelBinding; @@ -42,7 +40,7 @@ public NotificationResultFilter(ILayoutAccessor layoutAccessor, public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - if (context.Result is not (ViewResult or PageResult) + if (!context.IsViewOrPageResult() || !await _authorizationService.AuthorizeAsync(context.HttpContext.User, NotificationPermissions.ManageNotifications)) { await next(); diff --git a/src/OrchardCore.Modules/OrchardCore.ReCaptcha/ReCaptchaLoginFilter.cs b/src/OrchardCore.Modules/OrchardCore.ReCaptcha/ReCaptchaLoginFilter.cs index f85a1615f8a..d9bf794056f 100644 --- a/src/OrchardCore.Modules/OrchardCore.ReCaptcha/ReCaptchaLoginFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.ReCaptcha/ReCaptchaLoginFilter.cs @@ -1,8 +1,6 @@ using System; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Layout; using OrchardCore.Entities; @@ -33,7 +31,7 @@ public ReCaptchaLoginFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - if (!(context.Result is ViewResult || context.Result is PageResult) + if (!context.IsViewOrPageResult() || !String.Equals("OrchardCore.Users", Convert.ToString(context.RouteData.Values["area"]), StringComparison.OrdinalIgnoreCase)) { await next(); diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs b/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs index 0bd3ef1a673..b233d4794fe 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs @@ -1,7 +1,5 @@ using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.RazorPages; using OrchardCore.DisplayManagement; using OrchardCore.DisplayManagement.Layout; @@ -22,7 +20,7 @@ public LoginMenuFilter( public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - if (context.Result is ViewResult || context.Result is PageResult) + if (context.IsViewOrPageResult()) { var layout = await _layoutAccessor.GetLayoutAsync(); diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs new file mode 100644 index 00000000000..20d97ea256a --- /dev/null +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace Microsoft.AspNetCore.Mvc.Filters; + +public static class ResultExecutingContextExtensions +{ + public static bool IsViewOrPageResult(this ResultExecutingContext context) + => context.Result is ViewResult || context.Result is PageResult; +} diff --git a/src/OrchardCore/OrchardCore.DisplayManagement/Notify/NotifyFilter.cs b/src/OrchardCore/OrchardCore.DisplayManagement/Notify/NotifyFilter.cs index 768fcfac0b7..c596099b53e 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement/Notify/NotifyFilter.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement/Notify/NotifyFilter.cs @@ -131,7 +131,7 @@ public async Task OnResultExecutionAsync(ResultExecutingContext filterContext, R return; } - if (filterContext.Result is not ViewResult && filterContext.Result is not PageResult) + if (!filterContext.IsViewOrPageResult()) { await next(); return; From 934b8f08fa0bf9c90db6fa7948db3b1a29bb2c17 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Tue, 29 Aug 2023 13:40:58 -0700 Subject: [PATCH 2/4] change per feedback --- .../Extensions/ResultExecutingContextExtensions.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs index 20d97ea256a..14751a8b83a 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs @@ -5,5 +5,11 @@ namespace Microsoft.AspNetCore.Mvc.Filters; public static class ResultExecutingContextExtensions { public static bool IsViewOrPageResult(this ResultExecutingContext context) - => context.Result is ViewResult || context.Result is PageResult; + => context.IsViewResult() || context.IsPageResult(); + + public static bool IsViewResult(this ResultExecutingContext context) + => context.Result is ViewResult; + + public static bool IsPageResult(this ResultExecutingContext context) + => context.Result is PageResult; } From e316f3d86e01cb2b01fbfab31e10171ce11aa4f2 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Tue, 29 Aug 2023 14:23:02 -0700 Subject: [PATCH 3/4] cleanup --- .../Extensions/ResultExecutingContextExtensions.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs index 14751a8b83a..4b67a4cfe9c 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs @@ -5,11 +5,5 @@ namespace Microsoft.AspNetCore.Mvc.Filters; public static class ResultExecutingContextExtensions { public static bool IsViewOrPageResult(this ResultExecutingContext context) - => context.IsViewResult() || context.IsPageResult(); - - public static bool IsViewResult(this ResultExecutingContext context) - => context.Result is ViewResult; - - public static bool IsPageResult(this ResultExecutingContext context) - => context.Result is PageResult; + => context.Result is ViewResult or PageResult; } From c88768c642bafdb13223a543cfca21d01c5bc22b Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 1 Sep 2023 06:05:13 -0700 Subject: [PATCH 4/4] removing conflict --- .../Filters/LoginMenuFilter.cs | 33 ------------------- 1 file changed, 33 deletions(-) delete mode 100644 src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs b/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs deleted file mode 100644 index b233d4794fe..00000000000 --- a/src/OrchardCore.Modules/OrchardCore.Users/Filters/LoginMenuFilter.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Filters; -using OrchardCore.DisplayManagement; -using OrchardCore.DisplayManagement.Layout; - -namespace OrchardCore.Users.Filters; - -public class LoginMenuFilter : IAsyncResultFilter -{ - private readonly ILayoutAccessor _layoutAccessor; - private readonly IShapeFactory _shapeFactory; - - public LoginMenuFilter( - ILayoutAccessor layoutAccessor, - IShapeFactory shapeFactory) - { - _layoutAccessor = layoutAccessor; - _shapeFactory = shapeFactory; - } - - public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) - { - if (context.IsViewOrPageResult()) - { - var layout = await _layoutAccessor.GetLayoutAsync(); - - var userMenuItemZone = layout.Zones["UserMenuItems"]; - await userMenuItemZone.AddAsync(await _shapeFactory.CreateAsync("TwoFactorUserMenuItem")); - } - - await next(); - } -}