Replace static editor model events events with IEventAggregator notifications
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
using System;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
|
||||
namespace Umbraco.Cms.Core.Editors
|
||||
{
|
||||
public sealed class EditorModelEventArgs<T> : EditorModelEventArgs
|
||||
{
|
||||
private readonly EditorModelEventArgs _baseArgs;
|
||||
private T _model;
|
||||
|
||||
public EditorModelEventArgs(EditorModelEventArgs baseArgs)
|
||||
: base(baseArgs.Model, baseArgs.UmbracoContext)
|
||||
{
|
||||
_baseArgs = baseArgs;
|
||||
Model = (T)baseArgs.Model;
|
||||
}
|
||||
|
||||
public EditorModelEventArgs(T model, IUmbracoContext umbracoContext)
|
||||
: base(model, umbracoContext)
|
||||
{
|
||||
Model = model;
|
||||
}
|
||||
|
||||
public new T Model
|
||||
{
|
||||
get => _model;
|
||||
set
|
||||
{
|
||||
_model = value;
|
||||
if (_baseArgs != null)
|
||||
_baseArgs.Model = _model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class EditorModelEventArgs : EventArgs
|
||||
{
|
||||
public EditorModelEventArgs(object model, IUmbracoContext umbracoContext)
|
||||
{
|
||||
Model = model;
|
||||
UmbracoContext = umbracoContext;
|
||||
}
|
||||
|
||||
public object Model { get; set; }
|
||||
public IUmbracoContext UmbracoContext { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Umbraco.Cms.Core.Dashboards;
|
||||
using Umbraco.Cms.Core.Editors;
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to emit events for editor models in the back office
|
||||
/// </summary>
|
||||
public sealed class EditorModelEventManager
|
||||
{
|
||||
public static event TypedEventHandler<ActionExecutedContext, EditorModelEventArgs<ContentItemDisplay>> SendingContentModel;
|
||||
public static event TypedEventHandler<ActionExecutedContext, EditorModelEventArgs<MediaItemDisplay>> SendingMediaModel;
|
||||
public static event TypedEventHandler<ActionExecutedContext, EditorModelEventArgs<MemberDisplay>> SendingMemberModel;
|
||||
public static event TypedEventHandler<ActionExecutedContext, EditorModelEventArgs<UserDisplay>> SendingUserModel;
|
||||
|
||||
public static event TypedEventHandler<ActionExecutedContext, EditorModelEventArgs<IEnumerable<Tab<IDashboardSlim>>>> SendingDashboardSlimModel;
|
||||
|
||||
private static void OnSendingDashboardModel(ActionExecutedContext sender, EditorModelEventArgs<IEnumerable<Tab<IDashboardSlim>>> e)
|
||||
{
|
||||
var handler = SendingDashboardSlimModel;
|
||||
handler?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
private static void OnSendingUserModel(ActionExecutedContext sender, EditorModelEventArgs<UserDisplay> e)
|
||||
{
|
||||
var handler = SendingUserModel;
|
||||
handler?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
private static void OnSendingContentModel(ActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e)
|
||||
{
|
||||
var handler = SendingContentModel;
|
||||
handler?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
private static void OnSendingMediaModel(ActionExecutedContext sender, EditorModelEventArgs<MediaItemDisplay> e)
|
||||
{
|
||||
var handler = SendingMediaModel;
|
||||
handler?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
private static void OnSendingMemberModel(ActionExecutedContext sender, EditorModelEventArgs<MemberDisplay> e)
|
||||
{
|
||||
var handler = SendingMemberModel;
|
||||
handler?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Based on the type, emit's a specific event
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
internal static void EmitEvent(ActionExecutedContext sender, EditorModelEventArgs e)
|
||||
{
|
||||
if (e.Model is ContentItemDisplay)
|
||||
OnSendingContentModel(sender, new EditorModelEventArgs<ContentItemDisplay>(e));
|
||||
|
||||
if (e.Model is MediaItemDisplay)
|
||||
OnSendingMediaModel(sender, new EditorModelEventArgs<MediaItemDisplay>(e));
|
||||
|
||||
if (e.Model is MemberDisplay)
|
||||
OnSendingMemberModel(sender, new EditorModelEventArgs<MemberDisplay>(e));
|
||||
|
||||
if (e.Model is UserDisplay)
|
||||
OnSendingUserModel(sender, new EditorModelEventArgs<UserDisplay>(e));
|
||||
|
||||
if (e.Model is IEnumerable<Tab<IDashboardSlim>>)
|
||||
OnSendingDashboardModel(sender, new EditorModelEventArgs<IEnumerable<Tab<IDashboardSlim>>>(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Umbraco.Cms.Core.Dashboards;
|
||||
using Umbraco.Cms.Core.Editors;
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
using Umbraco.Cms.Core.Security;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Extensions;
|
||||
@@ -25,14 +29,18 @@ namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
|
||||
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
||||
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
|
||||
public OutgoingEditorModelEventFilter(
|
||||
IUmbracoContextAccessor umbracoContextAccessor,
|
||||
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
||||
IBackOfficeSecurityAccessor backOfficeSecurityAccessor, IEventAggregator eventAggregator)
|
||||
{
|
||||
_umbracoContextAccessor = umbracoContextAccessor
|
||||
?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
|
||||
_backOfficeSecurityAccessor = backOfficeSecurityAccessor
|
||||
?? throw new ArgumentNullException(nameof(backOfficeSecurityAccessor));
|
||||
_eventAggregator = eventAggregator
|
||||
?? throw new ArgumentNullException(nameof(eventAggregator));
|
||||
}
|
||||
|
||||
public void OnActionExecuted(ActionExecutedContext context)
|
||||
@@ -40,18 +48,32 @@ namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
if (context.Result == null) return;
|
||||
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
var user = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser;
|
||||
if (user == null) return;
|
||||
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser;
|
||||
if (currentUser == null) return;
|
||||
|
||||
if (context.Result is ObjectResult objectContent)
|
||||
{
|
||||
var model = objectContent.Value;
|
||||
|
||||
if (model != null)
|
||||
if (model is ContentItemDisplay content)
|
||||
{
|
||||
var args = new EditorModelEventArgs(model, umbracoContext);
|
||||
EditorModelEventManager.EmitEvent(context, args);
|
||||
objectContent.Value = args.Model;
|
||||
_eventAggregator.Publish(new SendingContentNotification(content, umbracoContext));
|
||||
}
|
||||
else if (model is MediaItemDisplay media)
|
||||
{
|
||||
_eventAggregator.Publish(new SendingMediaNotification(media, umbracoContext));
|
||||
}
|
||||
else if (model is MemberDisplay member)
|
||||
{
|
||||
_eventAggregator.Publish(new SendingMemberNotification(member, umbracoContext));
|
||||
}
|
||||
else if (model is UserDisplay user)
|
||||
{
|
||||
_eventAggregator.Publish(new SendingUserNotification(user, umbracoContext));
|
||||
}
|
||||
else if (model is IEnumerable<Tab<IDashboardSlim>> dashboards)
|
||||
{
|
||||
_eventAggregator.Publish(new SendingDashboardsNotification(dashboards, umbracoContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
{
|
||||
public class SendingContentNotification : INotification
|
||||
{
|
||||
public IUmbracoContext UmbracoContext { get; }
|
||||
|
||||
public ContentItemDisplay Content { get; }
|
||||
|
||||
public SendingContentNotification(ContentItemDisplay content, IUmbracoContext umbracoContext)
|
||||
{
|
||||
Content = content;
|
||||
UmbracoContext = umbracoContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Cms.Core.Dashboards;
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
{
|
||||
public class SendingDashboardsNotification : INotification
|
||||
{
|
||||
public IUmbracoContext UmbracoContext { get; }
|
||||
|
||||
public IEnumerable<Tab<IDashboardSlim>> Dashboards { get; }
|
||||
|
||||
public SendingDashboardsNotification(IEnumerable<Tab<IDashboardSlim>> dashboards, IUmbracoContext umbracoContext)
|
||||
{
|
||||
Dashboards = dashboards;
|
||||
UmbracoContext = umbracoContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
{
|
||||
public class SendingMediaNotification : INotification
|
||||
{
|
||||
public IUmbracoContext UmbracoContext { get; }
|
||||
|
||||
public MediaItemDisplay Media { get; }
|
||||
|
||||
public SendingMediaNotification(MediaItemDisplay media, IUmbracoContext umbracoContext)
|
||||
{
|
||||
Media = media;
|
||||
UmbracoContext = umbracoContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
{
|
||||
public class SendingMemberNotification : INotification
|
||||
{
|
||||
public IUmbracoContext UmbracoContext { get; }
|
||||
|
||||
public MemberDisplay Member { get; }
|
||||
|
||||
public SendingMemberNotification(MemberDisplay member, IUmbracoContext umbracoContext)
|
||||
{
|
||||
Member = member;
|
||||
UmbracoContext = umbracoContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
|
||||
namespace Umbraco.Cms.Web.BackOffice.Filters
|
||||
{
|
||||
public class SendingUserNotification : INotification
|
||||
{
|
||||
public IUmbracoContext UmbracoContext { get; }
|
||||
|
||||
public UserDisplay User { get; }
|
||||
|
||||
public SendingUserNotification(UserDisplay user, IUmbracoContext umbracoContext)
|
||||
{
|
||||
User = user;
|
||||
UmbracoContext = umbracoContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user