2020-05-08 13:23:44 +02:00
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
2021-05-11 14:33:49 +02:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Routing;
|
|
|
|
|
using Umbraco.Extensions;
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.ModelBinders;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maps view models, supporting mapping to and from any <see cref="IPublishedContent" /> or
|
|
|
|
|
/// <see cref="IContentModel" />.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ContentModelBinder : IModelBinder
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2021-02-01 15:37:41 +11:00
|
|
|
|
2020-05-08 13:23:44 +02:00
|
|
|
/// <summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
/// Initializes a new instance of the <see cref="ContentModelBinder" /> class.
|
2020-05-08 13:23:44 +02:00
|
|
|
/// </summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
public ContentModelBinder(IEventAggregator eventAggregator) => _eventAggregator = eventAggregator;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task BindModelAsync(ModelBindingContext bindingContext)
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
// Although this model binder is built to work both ways between IPublishedContent and IContentModel in reality
|
|
|
|
|
// only IPublishedContent will ever exist in the request so when this model binder is used as an IModelBinder
|
|
|
|
|
// in the aspnet pipeline it will really only support converting from IPublishedContent which is contained
|
|
|
|
|
// in the UmbracoRouteValues --> IContentModel
|
|
|
|
|
UmbracoRouteValues? umbracoRouteValues = bindingContext.HttpContext.Features.Get<UmbracoRouteValues>();
|
|
|
|
|
if (umbracoRouteValues is null)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2021-02-01 15:37:41 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
BindModel(bindingContext, umbracoRouteValues.PublishedRequest.PublishedContent, bindingContext.ModelType);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2021-01-15 18:17:13 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// source is the model that we have
|
|
|
|
|
// modelType is the type of the model that we need to bind to
|
|
|
|
|
//
|
|
|
|
|
// create a model object of the modelType by mapping:
|
|
|
|
|
// { ContentModel, ContentModel<TContent>, IPublishedContent }
|
|
|
|
|
// to
|
|
|
|
|
// { ContentModel, ContentModel<TContent>, IPublishedContent }
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to bind the model
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void BindModel(ModelBindingContext bindingContext, object? source, Type modelType)
|
|
|
|
|
{
|
|
|
|
|
// Null model, return
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2020-10-19 14:03:51 +02:00
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// If types already match, return
|
|
|
|
|
Type sourceType = source.GetType();
|
|
|
|
|
if (sourceType.Inherits(modelType))
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
bindingContext.Result = ModelBindingResult.Success(source);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// Try to grab the content
|
|
|
|
|
var sourceContent = source as IPublishedContent; // check if what we have is an IPublishedContent
|
|
|
|
|
if (sourceContent == null && sourceType.Implements<IContentModel>())
|
|
|
|
|
{
|
|
|
|
|
// else check if it's an IContentModel, and get the content
|
|
|
|
|
sourceContent = ((IContentModel)source).Content;
|
|
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
if (sourceContent == null)
|
|
|
|
|
{
|
|
|
|
|
// else check if we can convert it to a content
|
|
|
|
|
Attempt<IPublishedContent> attempt1 = source.TryConvertTo<IPublishedContent>();
|
|
|
|
|
if (attempt1.Success)
|
2020-12-11 14:55:19 +11:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
sourceContent = attempt1.Result;
|
2020-12-11 14:55:19 +11:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
2020-12-11 14:55:19 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// If we have a content
|
|
|
|
|
if (sourceContent != null)
|
|
|
|
|
{
|
|
|
|
|
// If model is IPublishedContent, check content type and return
|
|
|
|
|
if (modelType.Implements<IPublishedContent>())
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
if (sourceContent.GetType().Inherits(modelType) == false)
|
2020-12-11 14:55:19 +11:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
ThrowModelBindingException(true, false, sourceContent.GetType(), modelType);
|
2020-12-11 14:55:19 +11:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
|
|
|
|
|
bindingContext.Result = ModelBindingResult.Success(sourceContent);
|
|
|
|
|
return;
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// If model is ContentModel, create and return
|
|
|
|
|
if (modelType == typeof(ContentModel))
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
bindingContext.Result = ModelBindingResult.Success(new ContentModel(sourceContent));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// If model is ContentModel<TContent>, check content type, then create and return
|
|
|
|
|
if (modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(ContentModel<>))
|
|
|
|
|
{
|
|
|
|
|
Type targetContentType = modelType.GetGenericArguments()[0];
|
|
|
|
|
if (sourceContent.GetType().Inherits(targetContentType) == false)
|
2020-05-08 13:23:44 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
ThrowModelBindingException(true, true, sourceContent.GetType(), targetContentType);
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
bindingContext.Result = ModelBindingResult.Success(Activator.CreateInstance(modelType, sourceContent));
|
2020-12-11 14:55:19 +11:00
|
|
|
return;
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// Last chance : try to convert
|
|
|
|
|
Attempt<object?> attempt2 = source.TryConvertTo(modelType);
|
|
|
|
|
if (attempt2.Success)
|
|
|
|
|
{
|
|
|
|
|
bindingContext.Result = ModelBindingResult.Success(attempt2.Result);
|
2020-12-11 14:55:19 +11:00
|
|
|
return;
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// Fail
|
|
|
|
|
ThrowModelBindingException(false, false, sourceType, modelType);
|
|
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
private void ThrowModelBindingException(bool sourceContent, bool modelContent, Type sourceType, Type modelType)
|
|
|
|
|
{
|
|
|
|
|
var msg = new StringBuilder();
|
2020-12-11 14:55:19 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// prepare message
|
|
|
|
|
msg.Append("Cannot bind source");
|
|
|
|
|
if (sourceContent)
|
|
|
|
|
{
|
|
|
|
|
msg.Append(" content");
|
|
|
|
|
}
|
2020-12-11 14:55:19 +11:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
msg.Append(" type ");
|
|
|
|
|
msg.Append(sourceType.FullName);
|
|
|
|
|
msg.Append(" to model");
|
|
|
|
|
if (modelContent)
|
|
|
|
|
{
|
|
|
|
|
msg.Append(" content");
|
|
|
|
|
}
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
msg.Append(" type ");
|
|
|
|
|
msg.Append(modelType.FullName);
|
|
|
|
|
msg.Append(".");
|
2020-05-08 13:23:44 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// raise event, to give model factories a chance at reporting
|
|
|
|
|
// the error with more details, and optionally request that
|
|
|
|
|
// the application restarts.
|
|
|
|
|
var args = new ModelBindingErrorNotification(sourceType, modelType, msg);
|
|
|
|
|
_eventAggregator.Publish(args);
|
|
|
|
|
|
|
|
|
|
throw new ModelBindingException(msg.ToString());
|
2020-05-08 13:23:44 +02:00
|
|
|
}
|
|
|
|
|
}
|