From 08de1694bcca9bdce07ce7a26ad50ac9997f4de1 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Tue, 12 Sep 2023 13:07:06 -0700 Subject: [PATCH] Add IsViewOrPageResult() extension (#14228) --- .../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 +--- .../Extensions/ResultExecutingContextExtensions.cs | 9 +++++++++ .../OrchardCore.DisplayManagement/Notify/NotifyFilter.cs | 2 +- 11 files changed, 21 insertions(+), 34 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/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement.Abstractions/Extensions/ResultExecutingContextExtensions.cs new file mode 100644 index 00000000000..4b67a4cfe9c --- /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 or 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;