kill UmbracoWebsiteSecurityFactory and UmbracoRoutedRequest, simplify all this.

This commit is contained in:
Shannon
2021-03-04 15:12:58 +11:00
parent ed5116664f
commit efb84409f5
6 changed files with 5 additions and 89 deletions

View File

@@ -1,33 +0,0 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Events
{
/// <summary>
/// Notification raised when Umbraco routes a front-end request.
/// </summary>
public class UmbracoRoutedRequest : INotification
{
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoRequestBegin"/> class.
/// </summary>
public UmbracoRoutedRequest(IUmbracoContext umbracoContext)
{
if (!umbracoContext.IsFrontEndUmbracoRequest())
{
throw new InvalidOperationException($"{nameof(UmbracoRoutedRequest)} is only valid for Umbraco front-end requests");
}
UmbracoContext = umbracoContext;
}
/// <summary>
/// Gets the <see cref="IUmbracoContext"/>
/// </summary>
public IUmbracoContext UmbracoContext { get; }
}
}

View File

@@ -1,7 +1,7 @@
namespace Umbraco.Cms.Core.Security
namespace Umbraco.Cms.Core.Security
{
public interface IUmbracoWebsiteSecurityAccessor
{
IUmbracoWebsiteSecurity WebsiteSecurity { get; set; }
IUmbracoWebsiteSecurity WebsiteSecurity { get; }
}
}

View File

@@ -266,7 +266,6 @@ namespace Umbraco.Extensions
builder.Services.AddUnique<IUmbracoContextFactory, UmbracoContextFactory>();
builder.Services.AddUnique<IBackOfficeSecurityAccessor, BackOfficeSecurityAccessor>();
builder.AddNotificationHandler<UmbracoRoutedRequest, UmbracoWebsiteSecurityFactory>();
builder.Services.AddUnique<IUmbracoWebsiteSecurityAccessor, UmbracoWebsiteSecurityAccessor>();
var umbracoApiControllerTypes = builder.TypeLoader.GetUmbracoApiControllers().ToList();
@@ -289,6 +288,7 @@ namespace Umbraco.Extensions
builder.Services.AddScoped<UmbracoHelper>();
builder.Services.AddScoped<RequestLock>();
builder.Services.AddScoped<IBackOfficeSecurity, BackOfficeSecurity>();
builder.Services.AddScoped<IUmbracoWebsiteSecurity, UmbracoWebsiteSecurity>();
builder.AddHttpClients();

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Security;
namespace Umbraco.Cms.Web.Common.Security
@@ -17,9 +18,6 @@ namespace Umbraco.Cms.Web.Common.Security
/// Gets or sets the <see cref="IUmbracoWebsiteSecurity"/> object.
/// </summary>
public IUmbracoWebsiteSecurity WebsiteSecurity
{
get => _httpContextAccessor.HttpContext?.Features.Get<IUmbracoWebsiteSecurity>();
set => _httpContextAccessor.HttpContext?.Features.Set(value);
}
=> _httpContextAccessor.HttpContext?.RequestServices.GetService<IUmbracoWebsiteSecurity>();
}
}

View File

@@ -1,46 +0,0 @@
using Microsoft.AspNetCore.Http;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
namespace Umbraco.Cms.Web.Common.Security
{
/// <summary>
/// Ensures that the <see cref="IUmbracoWebsiteSecurity"/> is populated on a front-end request
/// </summary>
internal sealed class UmbracoWebsiteSecurityFactory : INotificationHandler<UmbracoRoutedRequest>
{
private readonly IUmbracoWebsiteSecurityAccessor _umbracoWebsiteSecurityAccessor;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IMemberService _memberService;
private readonly IMemberTypeService _memberTypeService;
private readonly IShortStringHelper _shortStringHelper;
public UmbracoWebsiteSecurityFactory(
IUmbracoWebsiteSecurityAccessor umbracoWebsiteSecurityAccessor,
IHttpContextAccessor httpContextAccessor,
IMemberService memberService,
IMemberTypeService memberTypeService,
IShortStringHelper shortStringHelper)
{
_umbracoWebsiteSecurityAccessor = umbracoWebsiteSecurityAccessor;
_httpContextAccessor = httpContextAccessor;
_memberService = memberService;
_memberTypeService = memberTypeService;
_shortStringHelper = shortStringHelper;
}
public void Handle(UmbracoRoutedRequest notification)
{
if (_umbracoWebsiteSecurityAccessor.WebsiteSecurity is null)
{
_umbracoWebsiteSecurityAccessor.WebsiteSecurity = new UmbracoWebsiteSecurity(
_httpContextAccessor,
_memberService,
_memberTypeService,
_shortStringHelper);
}
}
}
}

View File

@@ -121,9 +121,6 @@ namespace Umbraco.Cms.Web.Website.Routing
// Store the route values as a httpcontext feature
httpContext.Features.Set(umbracoRouteValues);
// publish an event that we've routed a request
await _eventAggregator.PublishAsync(new UmbracoRoutedRequest(_umbracoContextAccessor.UmbracoContext));
// Need to check if there is form data being posted back to an Umbraco URL
PostedDataProxyInfo postedInfo = GetFormInfo(httpContext, values);
if (postedInfo != null)