U4-2670 Add event to modify the model before being sent to render

This commit is contained in:
Shannon
2016-01-27 13:20:13 +01:00
parent 02e5056987
commit e8dfa056b0
7 changed files with 133 additions and 14 deletions

View File

@@ -81,7 +81,8 @@ namespace Umbraco.Web.Editors
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[EnsureUserPermissionForContent("id")]
[OutgoingEditorModelEvent]
[EnsureUserPermissionForContent("id")]
public ContentItemDisplay GetById(int id)
{
var foundContent = GetObjectFromRequest(() => Services.ContentService.GetById(id));
@@ -116,6 +117,7 @@ namespace Umbraco.Web.Editors
/// If this is a container type, we'll remove the umbContainerView tab for a new item since
/// it cannot actually list children if it doesn't exist yet.
/// </returns>
[OutgoingEditorModelEvent]
public ContentItemDisplay GetEmpty(string contentTypeAlias, int parentId)
{
var contentType = Services.ContentTypeService.GetContentType(contentTypeAlias);

View File

@@ -0,0 +1,85 @@
using System;
using System.Web.Http.Filters;
using Umbraco.Core.Events;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Editors
{
public abstract class EditorModelEventArgs : EventArgs
{
protected EditorModelEventArgs(object model, UmbracoContext umbracoContext)
{
Model = model;
UmbracoContext = umbracoContext;
}
public object Model { get; private set; }
public UmbracoContext UmbracoContext { get; private set; }
}
public sealed class EditorModelEventArgs<T> : EditorModelEventArgs
{
public EditorModelEventArgs(T model, UmbracoContext umbracoContext)
: base(model, umbracoContext)
{
Model = model;
}
public new T Model { get; private set; }
}
/// <summary>
/// Used to emit events for editor models in the back office
/// </summary>
public sealed class EditorModelEventManager
{
public static event TypedEventHandler<HttpActionExecutedContext, EditorModelEventArgs<ContentItemDisplay>> SendingContentModel;
public static event TypedEventHandler<HttpActionExecutedContext, EditorModelEventArgs<MediaItemDisplay>> SendingMediaModel;
public static event TypedEventHandler<HttpActionExecutedContext, EditorModelEventArgs<MemberDisplay>> SendingMemberModel;
private static void OnSendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e)
{
var handler = SendingContentModel;
if (handler != null) handler(sender, e);
}
private static void OnSendingMediaModel(HttpActionExecutedContext sender, EditorModelEventArgs<MediaItemDisplay> e)
{
var handler = SendingMediaModel;
if (handler != null) handler(sender, e);
}
private static void OnSendingMemberModel(HttpActionExecutedContext sender, EditorModelEventArgs<MemberDisplay> e)
{
var handler = SendingMemberModel;
if (handler != null) handler(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(HttpActionExecutedContext sender, EditorModelEventArgs e)
{
var contentItemDisplay = e.Model as ContentItemDisplay;
if (contentItemDisplay != null)
{
OnSendingContentModel(sender, (EditorModelEventArgs<ContentItemDisplay>) e);
}
var mediaItemDisplay = e.Model as MediaItemDisplay;
if (mediaItemDisplay != null)
{
OnSendingMediaModel(sender, (EditorModelEventArgs<MediaItemDisplay>)e);
}
var memberItemDisplay = e.Model as MemberDisplay;
if (memberItemDisplay != null)
{
OnSendingMemberModel(sender, (EditorModelEventArgs<MemberDisplay>)e);
}
}
}
}

View File

@@ -63,13 +63,14 @@ namespace Umbraco.Web.Editors
: base(umbracoContext)
{
}
/// <summary>
/// Gets an empty content item for the
/// </summary>
/// <param name="contentTypeAlias"></param>
/// <param name="parentId"></param>
/// <returns></returns>
[OutgoingEditorModelEvent]
public MediaItemDisplay GetEmpty(string contentTypeAlias, int parentId)
{
var contentType = Services.ContentTypeService.GetMediaType(contentTypeAlias);
@@ -92,6 +93,7 @@ namespace Umbraco.Web.Editors
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[OutgoingEditorModelEvent]
[EnsureUserPermissionForMedia("id")]
public MediaItemDisplay GetById(int id)
{

View File

@@ -145,6 +145,7 @@ namespace Umbraco.Web.Editors
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[OutgoingEditorModelEvent]
public MemberDisplay GetByKey(Guid key)
{
MembershipUser foundMembershipMember;
@@ -196,6 +197,7 @@ namespace Umbraco.Web.Editors
/// </summary>
/// <param name="contentTypeAlias"></param>
/// <returns></returns>
[OutgoingEditorModelEvent]
public MemberDisplay GetEmpty(string contentTypeAlias = null)
{
IMember emptyContent;

View File

@@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Umbraco.Web.PublishedContentModels
{
class Empty
{
}
}

View File

@@ -319,6 +319,7 @@
<Compile Include="Editors\EditorValidationResolver.cs" />
<Compile Include="Editors\EditorValidator.cs" />
<Compile Include="Editors\IEditorValidator.cs" />
<Compile Include="Editors\EditorModelEventManager.cs" />
<Compile Include="HtmlHelperBackOfficeExtensions.cs" />
<Compile Include="Install\InstallSteps\Version73FileCleanup.cs" />
<Compile Include="Models\ContentEditing\DocumentTypeDisplay.cs" />
@@ -686,6 +687,7 @@
<Compile Include="WebApi\Filters\EnableOverrideAuthorizationAttribute.cs" />
<Compile Include="WebApi\Filters\FilterGrouping.cs" />
<Compile Include="WebApi\Filters\LegacyTreeAuthorizeAttribute.cs" />
<Compile Include="WebApi\Filters\OutgoingEditorModelEventAttribute.cs" />
<Compile Include="WebApi\Filters\OutgoingNoHyphenGuidFormatAttribute.cs" />
<Compile Include="WebApi\Filters\OverridableAuthorizationAttribute.cs" />
<Compile Include="WebApi\Filters\SetAngularAntiForgeryTokensAttribute.cs" />

View File

@@ -0,0 +1,38 @@
using System;
using System.Net.Http;
using System.Web.Http.Filters;
using Umbraco.Core;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.WebApi.Filters
{
/// <summary>
/// Used to emit outgoing editor model events
/// </summary>
internal sealed class OutgoingEditorModelEventAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response == null) return;
var user = UmbracoContext.Current.Security.CurrentUser;
if (user == null) return;
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
var model = objectContent.Value;
if (model != null)
{
EditorModelEventManager.EmitEvent(actionExecutedContext, new EditorModelEventArgs<ContentItemDisplay>(
(dynamic)model,
UmbracoContext.Current));
}
}
base.OnActionExecuted(actionExecutedContext);
}
}
}