Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 5 additions & 6 deletions src/OrchardCoreContrib.ContentPreview/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace OrchardCoreContrib.ContentPreview
namespace OrchardCoreContrib.ContentPreview;

public class Constants
{
internal class Constants
{
public const string PreviewSlug = "preview";
public const string PreviewSlug = "preview";

public const string PagePreviewBarFeatureId = "OrchardCoreContrib.ContentPreview.PagePreviewBar";
}
public const string PagePreviewBarFeatureId = "OrchardCoreContrib.ContentPreview.PagePreviewBar";
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Modules;

namespace OrchardCoreContrib.ContentPreview.Controllers
namespace OrchardCoreContrib.ContentPreview.Controllers;

[Feature("OrchardCoreContrib.ContentPreview.PagePreviewBar")]
public class PreviewController : Controller
{
[Feature("OrchardCoreContrib.ContentPreview.PagePreviewBar")]
public class PreviewController : Controller
{
public IActionResult Index() => View();
}
public IActionResult Index() => View();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@
using OrchardCore.Environment.Shell;
using OrchardCore.Users;
using OrchardCoreContrib.ContentPreview;
using System;
using OrchardCoreContrib.Infrastructure;

namespace Microsoft.AspNetCore.Builder
namespace Microsoft.AspNetCore.Builder;

/// <summary>
/// Provides an extension methods for <see cref="IApplicationBuilder"/> to enable page preview.
/// </summary>
public static class PagePreviewOrchardCoreExtensions
{
/// <summary>
/// Provides an extension methods for <see cref="IApplicationBuilder"/> to enable page preview.
/// Uses the page preview middleware.
/// </summary>
public static class PagePreviewOrchardCoreExtensions
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
public static IApplicationBuilder UsePagePreview(this IApplicationBuilder app)
{
/// <summary>
/// Uses the page preview middleware.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
public static IApplicationBuilder UsePagePreview(this IApplicationBuilder app)
{
if (app is null)
{
throw new ArgumentNullException(nameof(app));
}
Guard.ArgumentNotNull(app, nameof(app));

var adminOptions = app.ApplicationServices.GetService<IOptions<AdminOptions>>();
var userOptions = app.ApplicationServices.GetService<IOptions<UserOptions>>();
var shellFeaturesManager = app.ApplicationServices.CreateScope().ServiceProvider.GetService<IShellFeaturesManager>();
var adminOptions = app.ApplicationServices.GetService<IOptions<AdminOptions>>();
var userOptions = app.ApplicationServices.GetService<IOptions<UserOptions>>();
var shellFeaturesManager = app.ApplicationServices.CreateScope()
.ServiceProvider
.GetService<IShellFeaturesManager>();

app.UseMiddleware<PagePreviewMiddleware>(adminOptions, userOptions, shellFeaturesManager);
app.UseMiddleware<PagePreviewMiddleware>(adminOptions, userOptions, shellFeaturesManager);

return app;
}
return app;
}
}
2 changes: 1 addition & 1 deletion src/OrchardCoreContrib.ContentPreview/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Name = "Content Preview",
Author = ManifestConstants.Author,
Website = ManifestConstants.Website,
Version = "1.3.1",
Version = "1.5.0",
Category = "Content Management"
)]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

<PropertyGroup>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>
<Authors>The Orchard Core Contrib Team</Authors>
<Company />
<Description>Provides a list of content preview features such as page preview bar.</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/OrchardCoreContrib/OrchardCoreContrib.Modules/tree/main/src/OrchardCoreContrib.ContentPreview/README.md</PackageProjectUrl>
<RepositoryUrl>https://github.com/OrchardCoreContrib/OrchardCoreContrib.Modules</RepositoryUrl>
Expand All @@ -26,6 +27,7 @@

<ItemGroup>
<None Include="../../images/icon.png" Pack="true" PackagePath="icon.png" />
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
Expand All @@ -35,6 +37,7 @@
<PackageReference Include="OrchardCore.ResourceManagement" />
<PackageReference Include="OrchardCore.Users.Abstractions" />
<PackageReference Include="OrchardCoreContrib.Abstractions" />
<PackageReference Include="OrchardCoreContrib.Infrastructure.Abstractions" />
</ItemGroup>

</Project>
116 changes: 54 additions & 62 deletions src/OrchardCoreContrib.ContentPreview/PagePreviewMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,78 @@
using OrchardCore.Admin;
using OrchardCore.Environment.Shell;
using OrchardCore.Users;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace OrchardCoreContrib.ContentPreview
namespace OrchardCoreContrib.ContentPreview;

/// <summary>
/// Represents a middleware for a page preview.
/// </summary>
/// <remarks>
/// Craates a new instance of <see cref="PagePreviewMiddleware"/>.
/// </remarks>
/// <param name="next">The <see cref="RequestDelegate"/>.</param>
/// <param name="adminOptions">The <see cref="IOptions{AdminOptions}"/>.</param>
/// <param name="userOptions">The <see cref="IOptions{UserOptions}"/>.</param>
/// <param name="shellFeaturesManager">The <see cref="IShellFeaturesManager"/>.</param>
public class PagePreviewMiddleware(
RequestDelegate next,
IOptions<AdminOptions> adminOptions,
IOptions<UserOptions> userOptions,
IShellFeaturesManager shellFeaturesManager)
{
private readonly AdminOptions _adminOptions = adminOptions.Value;
private readonly UserOptions _userOptions = userOptions.Value;

/// <summary>
/// Represents a middleware for a page preview.
/// Invokes the logic of the middleware.
/// </summary>
public class PagePreviewMiddleware
/// <param name="context">The <see cref="HttpContext"/>.</param>
public async Task InvokeAsync(HttpContext context)
{
private readonly RequestDelegate _next;
private readonly AdminOptions _adminOptions;
private readonly UserOptions _userOptions;
private readonly IShellFeaturesManager _shellFeaturesManager;
var path = context.Request.Path.Value;

/// <summary>
/// Craates a new instance of <see cref="PagePreviewMiddleware"/>.
/// </summary>
/// <param name="next">The <see cref="RequestDelegate"/>.</param>
/// <param name="adminOptions">The <see cref="IOptions{AdminOptions}"/>.</param>
/// <param name="userOptions">The <see cref="IOptions{UserOptions}"/>.</param>
/// <param name="shellFeaturesManager">The <see cref="IShellFeaturesManager"/>.</param>
public PagePreviewMiddleware(
RequestDelegate next,
IOptions<AdminOptions> adminOptions,
IOptions<UserOptions> userOptions,
IShellFeaturesManager shellFeaturesManager)
// Skip if the user is not authenticated
if (!context.User.Identity.IsAuthenticated)
{
_next = next;
_adminOptions = adminOptions.Value;
_userOptions = userOptions.Value;
_shellFeaturesManager = shellFeaturesManager;
await next(context);

return;
}

/// <summary>
/// Invokes the logic of the middleware.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
public Task Invoke(HttpContext context)
// Skip if the current request for a login page
if (path.StartsWith($"/{_userOptions.LoginPath}", StringComparison.OrdinalIgnoreCase))
{
var path = context.Request.Path.Value;
await next(context);

// Skip if the user is not authenticated
if (!context.User.Identity.IsAuthenticated)
{
return _next(context);
}
return;
}

// Skip if the current request for a login page
if (path.StartsWith($"/{_userOptions.LoginPath}", StringComparison.OrdinalIgnoreCase))
{
return _next(context);
}
// Skip if the current request for an admin page
if (path.StartsWith($"/{_adminOptions.AdminUrlPrefix}", StringComparison.OrdinalIgnoreCase))
{
await next(context);

// Skip if the current request for an admin page
if (path.StartsWith($"/{_adminOptions.AdminUrlPrefix}", StringComparison.OrdinalIgnoreCase))
{
return _next(context);
}
return;
}

var featureEnabled = _shellFeaturesManager
.GetEnabledFeaturesAsync().Result
.Any(f => f.Id == Constants.PagePreviewBarFeatureId);
var enabledFeatures = await shellFeaturesManager.GetEnabledFeaturesAsync();

if (!featureEnabled)
{
return _next(context);
}
if (!enabledFeatures.Any(f => f.Id == Constants.PagePreviewBarFeatureId))
{
await next(context);

var isPreview = context.Request.Query.ContainsKey(Constants.PreviewSlug);
return;
}

if (!path.Contains(Constants.PreviewSlug) && !isPreview)
{
var url = String.Concat(context.Request.PathBase.Value, $"/{Constants.PreviewSlug}", context.Request.Path.Value);

context.Response.Redirect(url);
}
var isPreview = context.Request.Query.ContainsKey(Constants.PreviewSlug);

return _next(context);
if (!path.Contains(Constants.PreviewSlug) && !isPreview)
{
var url = string.Concat(context.Request.PathBase.Value, $"/{Constants.PreviewSlug}", context.Request.Path.Value);

context.Response.Redirect(url);
}

await next(context);
}
}
3 changes: 2 additions & 1 deletion src/OrchardCoreContrib.ContentPreview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This module allows you to preview your page in different devices.

## Version

1.3.1
1.5.0

## Category

Expand All @@ -26,6 +26,7 @@ This module has no dependencies.

| Name | Version |
|---------------------------------------------------------------------------------------------------------------------|-------------|
| [`OrchardCoreContrib.ContentPreview`](https://www.nuget.org/packages/OrchardCoreContrib.ContentPreview/1.5.0) | 1.5.0 |
| [`OrchardCoreContrib.ContentPreview`](https://www.nuget.org/packages/OrchardCoreContrib.ContentPreview/1.4.0) | 1.4.0 |
| [`OrchardCoreContrib.ContentPreview`](https://www.nuget.org/packages/OrchardCoreContrib.ContentPreview/1.3.1) | 1.3.1 |
| [`OrchardCoreContrib.ContentPreview`](https://www.nuget.org/packages/OrchardCoreContrib.ContentPreview/1.3.0) | 1.3.0 |
Expand Down
18 changes: 0 additions & 18 deletions src/OrchardCoreContrib.ContentPreview/RequestDestination.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
using Microsoft.Extensions.Options;
using OrchardCore.ResourceManagement;

namespace OrchardCoreContrib.ContentPreview
namespace OrchardCoreContrib.ContentPreview;

/// <summary>
/// Configure the resources that will be used in the module.
/// </summary>
public class ResourceManagementOptionsConfiguration : IConfigureOptions<ResourceManagementOptions>
{
/// <summary>
/// Configure the resources that will be used in the module.
/// </summary>
public class ResourceManagementOptionsConfiguration : IConfigureOptions<ResourceManagementOptions>
{
private static readonly ResourceManifest _manifest;
private static readonly ResourceManifest _manifest;

static ResourceManagementOptionsConfiguration()
{
_manifest = new ResourceManifest();
static ResourceManagementOptionsConfiguration()
{
_manifest = new ResourceManifest();

_manifest
.DefineScript("page-preview-bar")
.SetUrl("~/OrchardCoreContrib.ContentPreview/Scripts/PagePreviewBar.js")
.SetVersion("1.0.0");
_manifest
.DefineScript("page-preview-bar")
.SetUrl("~/OrchardCoreContrib.ContentPreview/Scripts/PagePreviewBar.js")
.SetVersion("1.0.0");

_manifest
.DefineStyle("page-preview-bar")
.SetUrl("~/OrchardCoreContrib.ContentPreview/Styles/PagePreviewBar.css")
.SetVersion("1.0.0");
}
_manifest
.DefineStyle("page-preview-bar")
.SetUrl("~/OrchardCoreContrib.ContentPreview/Styles/PagePreviewBar.css")
.SetVersion("1.0.0");
}

/// <inheritdoc/>
public void Configure(ResourceManagementOptions options)
{
options.ResourceManifests.Add(_manifest);
}
/// <inheritdoc/>
public void Configure(ResourceManagementOptions options)
{
options.ResourceManifests.Add(_manifest);
}
}
Loading
Loading