Other special cases

This commit is contained in:
Elitsa Marinovska
2021-01-13 16:25:21 +01:00
parent ded3a06170
commit 680f8c4d96
2 changed files with 31 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@@ -606,14 +607,16 @@ namespace Umbraco.Web.BackOffice.Controllers
return notificationModel;
}
private void EnsureUniqueName(string name, IContent content, string modelName)
private bool EnsureUniqueName(string name, IContent content, string modelName)
{
var existing = _contentService.GetBlueprintsForContentTypes(content.ContentTypeId);
if (existing.Any(x => x.Name == name && x.Id != content.Id))
{
ModelState.AddModelError(modelName, _localizedTextService.Localize("blueprints/duplicateBlueprintMessage"));
throw HttpResponseException.CreateValidationErrorResponse(ModelState);
return false;
}
return true;
}
/// <summary>
@@ -627,7 +630,10 @@ namespace Umbraco.Web.BackOffice.Controllers
contentItem,
content =>
{
EnsureUniqueName(content.Name, content, "Name");
if (!EnsureUniqueName(content.Name, content, "Name"))
{
return OperationResult.Cancel(new EventMessages());
}
_contentService.SaveBlueprint(contentItem.PersistedContent, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
@@ -839,9 +845,15 @@ namespace Umbraco.Web.BackOffice.Controllers
v.Notifications.AddRange(n.Notifications);
}
//lastly, if it is not valid, add the model state to the outgoing object and throw a 400
HandleInvalidModelState(display, cultureForInvariantErrors);
//lastly, if it is not valid, add the model state to the outgoing object and throw a 400
if (!ModelState.IsValid)
{
display.Errors = ModelState.ToErrorDictionary();
return new ValidationErrorResult(display);
}
if (wasCancelled)
{
AddCancelMessage(display);
@@ -1915,9 +1927,6 @@ namespace Umbraco.Web.BackOffice.Controllers
AddVariantValidationError(culture, segment, "speechBubbles/contentCultureValidationError");
}
}
base.HandleInvalidModelState(display);
}
/// <summary>

View File

@@ -282,7 +282,8 @@ namespace Umbraco.Web.BackOffice.Controllers
if (ModelState.IsValid == false)
{
throw CreateModelStateValidationException<TContentTypeSave, TContentTypeDisplay>(ctId, contentTypeSave, ct);
var err = CreateModelStateValidationEror<TContentTypeSave, TContentTypeDisplay>(ctId, contentTypeSave, ct);
return new ValidationErrorResult(err);
}
//filter out empty properties
@@ -304,11 +305,11 @@ namespace Umbraco.Web.BackOffice.Controllers
catch (Exception ex)
{
var responseEx = CreateInvalidCompositionResponseException<TContentTypeDisplay, TContentTypeSave, TPropertyType>(ex, contentTypeSave, ct, ctId);
if (responseEx != null) throw responseEx;
if (responseEx != null) return new ValidationErrorResult(responseEx);
}
var exResult = CreateCompositionValidationExceptionIfInvalid<TContentTypeSave, TPropertyType, TContentTypeDisplay>(contentTypeSave, ct);
if (exResult != null) throw exResult;
if (exResult != null) return new ValidationErrorResult(exResult);
saveContentType(ct);
@@ -344,11 +345,14 @@ namespace Umbraco.Web.BackOffice.Controllers
catch (Exception ex)
{
var responseEx = CreateInvalidCompositionResponseException<TContentTypeDisplay, TContentTypeSave, TPropertyType>(ex, contentTypeSave, ct, ctId);
throw responseEx ?? ex;
if (responseEx is null)
throw ex;
return new ValidationErrorResult(responseEx);
}
var exResult = CreateCompositionValidationExceptionIfInvalid<TContentTypeSave, TPropertyType, TContentTypeDisplay>(contentTypeSave, newCt);
if (exResult != null) throw exResult;
if (exResult != null) return new ValidationErrorResult(exResult);
//set id to null to ensure its handled as a new type
contentTypeSave.Id = null;
@@ -472,7 +476,7 @@ namespace Umbraco.Web.BackOffice.Controllers
/// <param name="contentTypeSave"></param>
/// <param name="composition"></param>
/// <returns></returns>
private HttpResponseException CreateCompositionValidationExceptionIfInvalid<TContentTypeSave, TPropertyType, TContentTypeDisplay>(TContentTypeSave contentTypeSave, TContentType composition)
private TContentTypeDisplay CreateCompositionValidationExceptionIfInvalid<TContentTypeSave, TPropertyType, TContentTypeDisplay>(TContentTypeSave contentTypeSave, TContentType composition)
where TContentTypeSave : ContentTypeSave<TPropertyType>
where TPropertyType : PropertyTypeBasic
where TContentTypeDisplay : ContentTypeCompositionDisplay
@@ -490,7 +494,7 @@ namespace Umbraco.Web.BackOffice.Controllers
//map the 'save' data on top
display = UmbracoMapper.Map(contentTypeSave, display);
display.Errors = ModelState.ToErrorDictionary();
throw HttpResponseException.CreateValidationErrorResponse(display);
return display;
}
return null;
}
@@ -539,7 +543,7 @@ namespace Umbraco.Web.BackOffice.Controllers
/// <param name="ct"></param>
/// <param name="ctId"></param>
/// <returns></returns>
private HttpResponseException CreateInvalidCompositionResponseException<TContentTypeDisplay, TContentTypeSave, TPropertyType>(
private TContentTypeDisplay CreateInvalidCompositionResponseException<TContentTypeDisplay, TContentTypeSave, TPropertyType>(
Exception ex, TContentTypeSave contentTypeSave, TContentType ct, int ctId)
where TContentTypeDisplay : ContentTypeCompositionDisplay
where TContentTypeSave : ContentTypeSave<TPropertyType>
@@ -557,7 +561,7 @@ namespace Umbraco.Web.BackOffice.Controllers
if (invalidCompositionException != null)
{
AddCompositionValidationErrors<TContentTypeSave, TPropertyType>(contentTypeSave, invalidCompositionException.PropertyTypeAliases);
return CreateModelStateValidationException<TContentTypeSave, TContentTypeDisplay>(ctId, contentTypeSave, ct);
return CreateModelStateValidationEror<TContentTypeSave, TContentTypeDisplay>(ctId, contentTypeSave, ct);
}
return null;
}
@@ -570,7 +574,7 @@ namespace Umbraco.Web.BackOffice.Controllers
/// <param name="ctId"></param>
/// <param name="contentTypeSave"></param>
/// <param name="ct"></param>
private HttpResponseException CreateModelStateValidationException<TContentTypeSave, TContentTypeDisplay>(int ctId, TContentTypeSave contentTypeSave, TContentType ct)
private TContentTypeDisplay CreateModelStateValidationEror<TContentTypeSave, TContentTypeDisplay>(int ctId, TContentTypeSave contentTypeSave, TContentType ct)
where TContentTypeDisplay : ContentTypeCompositionDisplay
where TContentTypeSave : ContentTypeSave
{
@@ -589,7 +593,7 @@ namespace Umbraco.Web.BackOffice.Controllers
}
forDisplay.Errors = ModelState.ToErrorDictionary();
return HttpResponseException.CreateValidationErrorResponse(forDisplay);
return forDisplay;
}
}
}