Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsViewOrPageResult() extension #14228

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
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))
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
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))
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
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))
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
// 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() &&
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
(
(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()
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
|| !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()
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
|| !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
@@ -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;

Expand All @@ -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())
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
var layout = await _layoutAccessor.GetLayoutAsync();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Microsoft.AspNetCore.Mvc.Filters;

public static class ResultExecutingContextExtensions
{
public static bool IsViewOrPageResult(this ResultExecutingContext context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might prefer to add two extensions IsViewResult() & IsPageResult() then you can apply "or" in the condition when it's needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it. But I think these maybe useless since they wont be used anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hishamco anyway, I added them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So no need for IsViewOrPageResult() anymore :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsViewOrPageResult() simply the code in all the filters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry @hishamco I removed what I consider useless IsViewResult and IsPageResult since it encouraged you to use them over the simpler IsViewOrPageResult

=> 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;
}
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())
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
await next();
return;
Expand Down