Skip to content

Commit

Permalink
Add IsViewOrPageResult() extension (#14228)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Sep 12, 2023
1 parent 27930f7 commit 08de169
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 34 deletions.
4 changes: 1 addition & 3 deletions src/OrchardCore.Modules/OrchardCore.Admin/AdminMenuFilter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<FacebookSettings>();
if (!string.IsNullOrWhiteSpace(settings?.AppId))
if (!String.IsNullOrWhiteSpace(settings?.AppId))
{
if (settings.FBInit)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ITrackingConsentFeature>()?.CanTrack ?? true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ITrackingConsentFeature>()?.CanTrack ?? true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ITrackingConsentFeature>()?.CanTrack ?? true;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 08de169

Please sign in to comment.