2020-03-24 18:18:25 +01:00
|
|
|
using System;
|
2020-12-16 16:31:23 +11:00
|
|
|
using System.IO;
|
2022-01-06 13:35:24 +01:00
|
|
|
using Dazinator.Extensions.FileProviders.PrependBasePath;
|
2020-03-24 18:18:25 +01:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2022-01-06 13:35:24 +01:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-05-08 17:30:30 +10:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-12-16 16:31:23 +11:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-05-12 15:18:55 +10:00
|
|
|
using Serilog.Context;
|
2020-03-29 22:35:52 +02:00
|
|
|
using StackExchange.Profiling;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2021-02-12 11:38:50 +01:00
|
|
|
using Umbraco.Cms.Core.Logging.Serilog.Enrichers;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Services;
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
using Umbraco.Cms.Web.Common.ApplicationBuilder;
|
2021-02-10 11:42:04 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Middleware;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Plugins;
|
2022-01-06 13:35:24 +01:00
|
|
|
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
|
2020-03-24 18:18:25 +01:00
|
|
|
|
2020-05-07 10:08:23 +02:00
|
|
|
namespace Umbraco.Extensions
|
2020-03-24 18:18:25 +01:00
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="IApplicationBuilder"/> extensions for Umbraco
|
|
|
|
|
/// </summary>
|
2020-05-12 10:21:40 +10:00
|
|
|
public static class ApplicationBuilderExtensions
|
2020-03-24 18:18:25 +01:00
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Configures and use services required for using Umbraco
|
|
|
|
|
/// </summary>
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
public static IUmbracoApplicationBuilder UseUmbraco(this IApplicationBuilder app)
|
2021-07-16 16:04:14 -06:00
|
|
|
=> new UmbracoApplicationBuilder(app);
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
|
2020-05-12 10:21:40 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if Umbraco <see cref="IRuntimeState"/> is greater than <see cref="RuntimeLevel.BootFailed"/>
|
|
|
|
|
/// </summary>
|
2020-05-08 17:36:59 +10:00
|
|
|
public static bool UmbracoCanBoot(this IApplicationBuilder app)
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
=> app.ApplicationServices.GetRequiredService<IRuntimeState>().UmbracoCanBoot();
|
2020-05-08 17:36:59 +10:00
|
|
|
|
2020-05-12 15:18:55 +10:00
|
|
|
/// <summary>
|
2020-12-21 16:44:50 +11:00
|
|
|
/// Enables core Umbraco functionality
|
2020-05-12 15:18:55 +10:00
|
|
|
/// </summary>
|
|
|
|
|
public static IApplicationBuilder UseUmbracoCore(this IApplicationBuilder app)
|
|
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
if (app == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(app));
|
|
|
|
|
}
|
2020-05-12 15:18:55 +10:00
|
|
|
|
2020-12-08 16:33:50 +11:00
|
|
|
if (!app.UmbracoCanBoot())
|
|
|
|
|
{
|
|
|
|
|
return app;
|
|
|
|
|
}
|
2020-05-12 15:18:55 +10:00
|
|
|
|
2020-05-14 22:50:27 +10:00
|
|
|
// Register our global threadabort enricher for logging
|
2020-12-08 16:33:50 +11:00
|
|
|
ThreadAbortExceptionEnricher threadAbortEnricher = app.ApplicationServices.GetRequiredService<ThreadAbortExceptionEnricher>();
|
2020-05-14 22:50:27 +10:00
|
|
|
LogContext.Push(threadAbortEnricher); // NOTE: We are not in a using clause because we are not removing it, it is on the global context
|
2020-11-10 08:50:47 +00:00
|
|
|
|
2020-05-12 15:18:55 +10:00
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 10:21:40 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Enables middlewares required to run Umbraco
|
|
|
|
|
/// </summary>
|
2020-12-08 16:33:50 +11:00
|
|
|
/// <remarks>
|
|
|
|
|
/// Must occur before UseRouting
|
|
|
|
|
/// </remarks>
|
2020-05-12 10:21:40 +10:00
|
|
|
public static IApplicationBuilder UseUmbracoRouting(this IApplicationBuilder app)
|
2020-03-24 18:18:25 +01:00
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
// TODO: This method could be internal or part of another call - this is a required system so should't be 'opt-in'
|
|
|
|
|
if (app == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(app));
|
|
|
|
|
}
|
2020-03-24 18:18:25 +01:00
|
|
|
|
2020-05-14 22:50:27 +10:00
|
|
|
if (!app.UmbracoCanBoot())
|
|
|
|
|
{
|
2021-03-08 10:03:36 +01:00
|
|
|
app.UseStaticFiles(); // We need static files to show the nice error page.
|
2020-05-19 14:51:05 +10:00
|
|
|
app.UseMiddleware<BootFailedMiddleware>();
|
2020-05-14 22:50:27 +10:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-19 11:45:58 +01:00
|
|
|
app.UseMiddleware<PreviewAuthenticationMiddleware>();
|
2020-05-14 22:50:27 +10:00
|
|
|
app.UseMiddleware<UmbracoRequestMiddleware>();
|
|
|
|
|
app.UseMiddleware<MiniProfilerMiddleware>();
|
|
|
|
|
}
|
2020-05-19 14:51:05 +10:00
|
|
|
|
2020-03-24 18:18:25 +01:00
|
|
|
return app;
|
|
|
|
|
}
|
2020-05-12 15:18:55 +10:00
|
|
|
|
2020-05-14 22:50:27 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds request based serilog enrichers to the LogContext for each request
|
|
|
|
|
/// </summary>
|
2020-05-12 15:18:55 +10:00
|
|
|
public static IApplicationBuilder UseUmbracoRequestLogging(this IApplicationBuilder app)
|
|
|
|
|
{
|
2020-12-08 16:33:50 +11:00
|
|
|
if (app == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(app));
|
|
|
|
|
}
|
2020-05-12 15:18:55 +10:00
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
if (!app.UmbracoCanBoot())
|
|
|
|
|
return app;
|
2020-05-12 15:18:55 +10:00
|
|
|
|
|
|
|
|
app.UseMiddleware<UmbracoRequestLoggingMiddleware>();
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 09:49:56 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Allow static file access for App_Plugins folders
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IApplicationBuilder UseUmbracoPluginsStaticFiles(this IApplicationBuilder app)
|
2020-12-16 16:31:23 +11:00
|
|
|
{
|
|
|
|
|
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
|
|
|
|
|
|
|
|
|
|
var pluginFolder = hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.AppPlugins);
|
2022-01-06 13:35:24 +01:00
|
|
|
if (Directory.Exists(pluginFolder))
|
|
|
|
|
{
|
2022-01-18 11:31:03 +00:00
|
|
|
var umbracoPluginSettings = app.ApplicationServices.GetRequiredService<IOptionsMonitor<UmbracoPluginSettings>>();
|
2020-12-16 16:31:23 +11:00
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
var pluginFileProvider = new UmbracoPluginPhysicalFileProvider(
|
|
|
|
|
pluginFolder,
|
|
|
|
|
umbracoPluginSettings);
|
2020-12-16 16:31:23 +11:00
|
|
|
|
2022-03-29 13:44:21 +02:00
|
|
|
IWebHostEnvironment? webHostEnvironment = app.ApplicationServices.GetService<IWebHostEnvironment>();
|
|
|
|
|
|
|
|
|
|
if (webHostEnvironment is not null)
|
|
|
|
|
{
|
|
|
|
|
webHostEnvironment.WebRootFileProvider = webHostEnvironment.WebRootFileProvider.ConcatComposite(new PrependBasePathFileProvider(Constants.SystemDirectories.AppPlugins, pluginFileProvider));
|
|
|
|
|
}
|
2022-01-06 13:35:24 +01:00
|
|
|
}
|
2020-12-16 16:31:23 +11:00
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
2020-03-24 18:18:25 +01:00
|
|
|
}
|
|
|
|
|
}
|