From 3a519653583397308134241073de1a37bf45a47b Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Wed, 13 Jan 2021 16:14:20 +0100 Subject: [PATCH] Placing the code for checking if a ModelState is invalid directly --- .../Controllers/ContentControllerBase.cs | 16 ---------------- .../Controllers/MediaController.cs | 6 +++++- .../Controllers/MemberController.cs | 7 ++++++- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/Controllers/ContentControllerBase.cs b/src/Umbraco.Web.BackOffice/Controllers/ContentControllerBase.cs index 50012c7921..28047ea119 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/ContentControllerBase.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/ContentControllerBase.cs @@ -1,22 +1,16 @@ using System; using System.Linq; -using System.Net; -using System.Net.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Umbraco.Core; -using Umbraco.Core.Cache; -using Umbraco.Core.Configuration; using Umbraco.Core.Dictionary; using Umbraco.Core.Events; -using Umbraco.Core.Mapping; using Umbraco.Core.Models; using Umbraco.Core.Models.Editors; using Umbraco.Core.PropertyEditors; using Umbraco.Core.Serialization; using Umbraco.Core.Services; using Umbraco.Core.Strings; -using Umbraco.Extensions; using Umbraco.Web.Common.Exceptions; using Umbraco.Web.Common.Filters; using Umbraco.Web.Models.ContentEditing; @@ -151,16 +145,6 @@ namespace Umbraco.Web.BackOffice.Controllers } } - protected virtual void HandleInvalidModelState(IErrorModel display) - { - //lastly, if it is not valid, add the model state to the outgoing object and throw a 403 - if (!ModelState.IsValid) - { - display.Errors = ModelState.ToErrorDictionary(); - throw HttpResponseException.CreateValidationErrorResponse(display); - } - } - /// /// A helper method to attempt to get the instance from the request storage if it can be found there, /// otherwise gets it from the callback specified diff --git a/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs b/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs index 8e5d1307fc..3af2d7da2a 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs @@ -570,7 +570,11 @@ namespace Umbraco.Web.BackOffice.Controllers var display = _umbracoMapper.Map(contentItem.PersistedContent); //lastly, if it is not valid, add the model state to the outgoing object and throw a 403 - HandleInvalidModelState(display); + if (!ModelState.IsValid) + { + display.Errors = ModelState.ToErrorDictionary(); + return new ValidationErrorResult(display, StatusCodes.Status403Forbidden); + } //put the correct msgs in switch (contentItem.Action) diff --git a/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs b/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs index 23e6e30d81..ba0e22b2bf 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs @@ -7,6 +7,7 @@ using System.Net.Mime; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -266,7 +267,11 @@ namespace Umbraco.Web.BackOffice.Controllers var display = _umbracoMapper.Map(contentItem.PersistedContent); //lastly, if it is not valid, add the model state to the outgoing object and throw a 403 - HandleInvalidModelState(display); + if (!ModelState.IsValid) + { + display.Errors = ModelState.ToErrorDictionary(); + return new ValidationErrorResult(display, StatusCodes.Status403Forbidden); + } var localizedTextService = _localizedTextService; //put the correct messages in