From b0f4f9f440ede17904ee88c6fd02b3628d6b3b84 Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Tue, 12 Jan 2021 14:00:14 +0100 Subject: [PATCH] Refactoring the use of ValidationErrorResult with the build-in types for status response results --- .../Controllers/AuthenticationController.cs | 2 +- .../Controllers/CodeFileController.cs | 5 +-- .../Controllers/ContentController.cs | 15 ++++---- .../Controllers/ContentTypeController.cs | 13 +++---- .../Controllers/ContentTypeControllerBase.cs | 9 ++--- .../Controllers/DataTypeController.cs | 16 ++++---- .../Controllers/EntityController.cs | 38 +++++++++---------- .../Controllers/MacroRenderingController.cs | 6 +-- .../Controllers/MacrosController.cs | 2 +- .../Controllers/MediaController.cs | 15 ++++---- .../Controllers/MediaTypeController.cs | 16 ++++---- .../Controllers/MemberController.cs | 8 ++-- .../Controllers/MemberGroupController.cs | 14 +++---- .../Controllers/MemberTypeController.cs | 16 ++++---- .../Controllers/PackageController.cs | 5 +-- .../Controllers/RelationTypeController.cs | 10 ++--- .../Controllers/TemplateController.cs | 16 ++++---- .../Controllers/UsersController.cs | 32 ++++++++-------- 18 files changed, 108 insertions(+), 130 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs index ef12f98120..1463c19bb5 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs @@ -349,7 +349,7 @@ namespace Umbraco.Web.BackOffice.Controllers // by our angular helper because it thinks that we need to re-perform the request once we are // authorized and we don't want to return a 403 because angular will show a warning message indicating // that the user doesn't have access to perform this function, we just want to return a normal invalid message. - return new ValidationErrorResult(null); + return BadRequest(); } /// diff --git a/src/Umbraco.Web.BackOffice/Controllers/CodeFileController.cs b/src/Umbraco.Web.BackOffice/Controllers/CodeFileController.cs index a945ad3b2b..e2ec2fecd7 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/CodeFileController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/CodeFileController.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Net; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Umbraco.Core; @@ -258,10 +257,10 @@ namespace Umbraco.Web.BackOffice.Controllers snippets = _fileService.GetPartialViewSnippetNames(); break; default: - return new ValidationErrorResult(type, StatusCodes.Status404NotFound); + return NotFound(); } - return snippets.Select(snippet => new SnippetDisplay() {Name = snippet.SplitPascalCasing(_shortStringHelper).ToFirstUpperInvariant(), FileName = snippet}).ToList(); + return snippets.Select(snippet => new SnippetDisplay() { Name = snippet.SplitPascalCasing(_shortStringHelper).ToFirstUpperInvariant(), FileName = snippet }).ToList(); } /// diff --git a/src/Umbraco.Web.BackOffice/Controllers/ContentController.cs b/src/Umbraco.Web.BackOffice/Controllers/ContentController.cs index 5f1d283b14..cc9a787ee9 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/ContentController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/ContentController.cs @@ -6,7 +6,6 @@ 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; @@ -384,7 +383,7 @@ namespace Umbraco.Web.BackOffice.Controllers return GetById(guidUdi.Guid); } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); } /// @@ -399,7 +398,7 @@ namespace Umbraco.Web.BackOffice.Controllers var contentType = _contentTypeService.Get(contentTypeAlias); if (contentType == null) { - return new ValidationErrorResult(contentType, StatusCodes.Status404NotFound); + return NotFound(); } return GetEmpty(contentType, parentId); @@ -417,7 +416,7 @@ namespace Umbraco.Web.BackOffice.Controllers var contentType = _contentTypeService.Get(contentTypeKey); if (contentType == null) { - return new ValidationErrorResult(contentType, StatusCodes.Status404NotFound); + return NotFound(); } return GetEmpty(contentType, parentId); @@ -446,7 +445,7 @@ namespace Umbraco.Web.BackOffice.Controllers var blueprint = _contentService.GetBlueprintById(blueprintId); if (blueprint == null) { - return new ValidationErrorResult(blueprint, StatusCodes.Status404NotFound); + return NotFound(); } blueprint.Id = 0; @@ -2022,14 +2021,14 @@ namespace Umbraco.Web.BackOffice.Controllers { if (model == null) { - return new ValidationErrorResult(model, StatusCodes.Status404NotFound); + return NotFound(); } var contentService = _contentService; var toMove = contentService.GetById(model.Id); if (toMove == null) { - return new ValidationErrorResult(toMove, StatusCodes.Status404NotFound); + return NotFound(); } if (model.ParentId < 0) { @@ -2045,7 +2044,7 @@ namespace Umbraco.Web.BackOffice.Controllers var parent = contentService.GetById(model.ParentId); if (parent == null) { - return new ValidationErrorResult(parent, StatusCodes.Status404NotFound); + return NotFound(); } var parentContentType = _contentTypeService.Get(parent.ContentTypeId); diff --git a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs index 864a3e7369..67e6a6332c 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Net.Mime; using System.Text; using System.Xml; @@ -121,7 +120,7 @@ namespace Umbraco.Web.BackOffice.Controllers var ct = _contentTypeService.Get(id); if (ct == null) { - return new ValidationErrorResult(ct, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(ct); @@ -138,7 +137,7 @@ namespace Umbraco.Web.BackOffice.Controllers var contentType = _contentTypeService.Get(id); if (contentType == null) { - return new ValidationErrorResult(contentType, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(contentType); @@ -154,12 +153,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var contentType = _contentTypeService.Get(guidUdi.Guid); if (contentType == null) { - return new ValidationErrorResult(contentType, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(contentType); @@ -177,7 +176,7 @@ namespace Umbraco.Web.BackOffice.Controllers var foundType = _contentTypeService.Get(id); if (foundType == null) { - return new ValidationErrorResult(foundType, StatusCodes.Status404NotFound); + return NotFound(); } _contentTypeService.Delete(foundType, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id); @@ -254,7 +253,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (dataTypeDiff == null) { - return new ValidationErrorResult(dataTypeDiff, StatusCodes.Status404NotFound); + return NotFound(); } var configuration = _dataTypeService.GetDataType(id).Configuration; diff --git a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs index 2e1f711984..9b70d2fb49 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Net; using System.Net.Mime; using System.Text; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Umbraco.Core; @@ -92,7 +91,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (contentTypeId > 0) { source = ContentTypeService.Get(contentTypeId); - if (source == null) return new ValidationErrorResult(source, StatusCodes.Status404NotFound); + if (source == null) return NotFound(); } allContentTypes = ContentTypeService.GetAll().Cast().ToArray(); break; @@ -101,7 +100,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (contentTypeId > 0) { source =MediaTypeService.Get(contentTypeId); - if (source == null) return new ValidationErrorResult(source, StatusCodes.Status404NotFound); + if (source == null) return NotFound(); } allContentTypes =MediaTypeService.GetAll().Cast().ToArray(); break; @@ -110,7 +109,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (contentTypeId > 0) { source = MemberTypeService.Get(contentTypeId); - if (source == null) return new ValidationErrorResult(source, StatusCodes.Status404NotFound); + if (source == null) return NotFound(); } allContentTypes = MemberTypeService.GetAll().Cast().ToArray(); break; @@ -205,7 +204,7 @@ namespace Umbraco.Web.BackOffice.Controllers } if (source == null) - return new ValidationErrorResult(source, StatusCodes.Status404NotFound); + return NotFound(); id = source.Id; } diff --git a/src/Umbraco.Web.BackOffice/Controllers/DataTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/DataTypeController.cs index a099953a08..452ff8b5e0 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/DataTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/DataTypeController.cs @@ -2,11 +2,9 @@ using System; using System.Collections.Generic; using System.Data; using System.Linq; -using System.Net; using System.Net.Mime; using System.Text; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Umbraco.Core; @@ -97,7 +95,7 @@ namespace Umbraco.Web.BackOffice.Controllers var dataType = _dataTypeService.GetDataType(id); if (dataType == null) { - return new ValidationErrorResult(dataType, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(dataType); @@ -114,7 +112,7 @@ namespace Umbraco.Web.BackOffice.Controllers var dataType = _dataTypeService.GetDataType(id); if (dataType == null) { - return new ValidationErrorResult(dataType, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(dataType); @@ -130,12 +128,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var dataType = _dataTypeService.GetDataType(guidUdi.Guid); if (dataType == null) { - return new ValidationErrorResult(dataType, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(dataType); @@ -153,7 +151,7 @@ namespace Umbraco.Web.BackOffice.Controllers var foundType = _dataTypeService.GetDataType(id); if (foundType == null) { - return new ValidationErrorResult(foundType, StatusCodes.Status404NotFound); + return NotFound(); } var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser; _dataTypeService.Delete(foundType, currentUser.Id); @@ -179,7 +177,7 @@ namespace Umbraco.Web.BackOffice.Controllers var dt = _dataTypeService.GetDataType(Constants.Conventions.DataTypes.ListViewPrefix + contentTypeAlias); if (dt == null) { - return new ValidationErrorResult(dt, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(dt); @@ -229,7 +227,7 @@ namespace Umbraco.Web.BackOffice.Controllers var dataType = _dataTypeService.GetDataType(dataTypeId); if (dataType == null) { - return new ValidationErrorResult(dataType, StatusCodes.Status404NotFound); + return NotFound(); } //now, lets check if the data type has the current editor selected, if that is true diff --git a/src/Umbraco.Web.BackOffice/Controllers/EntityController.cs b/src/Umbraco.Web.BackOffice/Controllers/EntityController.cs index 600134df8e..34031a5dba 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/EntityController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/EntityController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -17,7 +17,6 @@ using Umbraco.Core.Trees; using Umbraco.Core.Xml; using Umbraco.Extensions; using Umbraco.Web.BackOffice.ModelBinders; -using Umbraco.Web.Common.ActionsResults; using Umbraco.Web.Common.Attributes; using Umbraco.Web.Common.ModelBinders; using Umbraco.Web.Models; @@ -237,7 +236,8 @@ namespace Umbraco.Web.BackOffice.Controllers { return new ActionResult>(GetPath(guidUdi.Guid, type)); } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + + return NotFound(); } /// @@ -251,7 +251,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var intId = _entityService.GetId(udi); if (!intId.Success) - return new ValidationErrorResult(intId.Result, StatusCodes.Status404NotFound); + return NotFound(); UmbracoEntityTypes entityType; switch (udi.EntityType) { @@ -265,7 +265,7 @@ namespace Umbraco.Web.BackOffice.Controllers entityType = UmbracoEntityTypes.Member; break; default: - return new ValidationErrorResult(udi.EntityType, StatusCodes.Status404NotFound); + return NotFound(); } return GetUrl(intId.Result, entityType, culture); } @@ -357,7 +357,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var intId = _entityService.GetId(id); if (!intId.Success) - return new ValidationErrorResult(intId.Result, StatusCodes.Status404NotFound); + return NotFound(); return GetUrlAndAnchors(intId.Result, culture); } @@ -422,7 +422,7 @@ namespace Umbraco.Web.BackOffice.Controllers return GetResultForKey(guidUdi.Guid, type); } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); } #endregion @@ -443,7 +443,7 @@ namespace Umbraco.Web.BackOffice.Controllers { if (ids == null) { - return new ValidationErrorResult(ids, StatusCodes.Status404NotFound); + return NotFound(); } return new ActionResult>(GetResultForIds(ids, type)); @@ -465,7 +465,7 @@ namespace Umbraco.Web.BackOffice.Controllers { if (ids == null) { - return new ValidationErrorResult(ids, StatusCodes.Status404NotFound); + return NotFound(); } return new ActionResult>(GetResultForKeys(ids, type)); @@ -489,7 +489,7 @@ namespace Umbraco.Web.BackOffice.Controllers { if (ids == null) { - return new ValidationErrorResult(ids, StatusCodes.Status404NotFound); + return NotFound(); } if (ids.Length == 0) @@ -506,7 +506,7 @@ namespace Umbraco.Web.BackOffice.Controllers return new ActionResult>(GetResultForKeys(ids.Select(x => ((GuidUdi)x).Guid).ToArray(), type)); } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); } #endregion @@ -580,13 +580,13 @@ namespace Umbraco.Web.BackOffice.Controllers if (Guid.TryParse(id, out _)) { //Not supported currently - return new ValidationErrorResult(id, StatusCodes.Status404NotFound); + return NotFound(); } if (UdiParser.TryParse(id, out _)) { //Not supported currently - return new ValidationErrorResult(id, StatusCodes.Status404NotFound); + return NotFound(); } //so we don't have an INT, GUID or UDI, it's just a string, so now need to check if it's a special id or a member type @@ -635,9 +635,9 @@ namespace Umbraco.Web.BackOffice.Controllers Guid? dataTypeKey = null) { if (pageNumber <= 0) - return new ValidationErrorResult(pageNumber, StatusCodes.Status404NotFound); + return NotFound(); if (pageSize <= 0) - return new ValidationErrorResult(pageSize, StatusCodes.Status404NotFound); + return NotFound(); var objectType = ConvertToObjectType(type); if (objectType.HasValue) @@ -735,9 +735,9 @@ namespace Umbraco.Web.BackOffice.Controllers Guid? dataTypeKey = null) { if (pageNumber <= 0) - return new ValidationErrorResult(pageNumber, StatusCodes.Status404NotFound); + return NotFound(); if (pageSize <= 0) - return new ValidationErrorResult(pageSize, StatusCodes.Status404NotFound); + return NotFound(); // re-normalize since NULL can be passed in filter = filter ?? string.Empty; @@ -980,7 +980,7 @@ namespace Umbraco.Web.BackOffice.Controllers var found = _entityService.Get(key, objectType.Value); if (found == null) { - return new ValidationErrorResult(found, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(found); } @@ -1012,7 +1012,7 @@ namespace Umbraco.Web.BackOffice.Controllers var found = _entityService.Get(id, objectType.Value); if (found == null) { - return new ValidationErrorResult(found, StatusCodes.Status404NotFound); + return NotFound(); } return MapEntity(found); } diff --git a/src/Umbraco.Web.BackOffice/Controllers/MacroRenderingController.cs b/src/Umbraco.Web.BackOffice/Controllers/MacroRenderingController.cs index b3d0eefad4..739ef9942e 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MacroRenderingController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MacroRenderingController.cs @@ -2,10 +2,8 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Net; using System.Text; using System.Threading; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Umbraco.Web.Models.ContentEditing; using Umbraco.Core; @@ -68,7 +66,7 @@ namespace Umbraco.Web.BackOffice.Controllers var macro = _macroService.GetById(macroId); if (macro == null) { - return new ValidationErrorResult(macro, StatusCodes.Status404NotFound); + return NotFound(); } return new ActionResult>(_umbracoMapper.Map>(macro).OrderBy(x => x.SortOrder)); @@ -116,7 +114,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var m = _macroService.GetByAlias(macroAlias); if (m == null) - return new ValidationErrorResult(m, StatusCodes.Status404NotFound); + return NotFound(); var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); var publishedContent = umbracoContext.Content.GetById(true, pageId); diff --git a/src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs b/src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs index 598fe15bf4..1ad7442289 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs @@ -160,7 +160,7 @@ namespace Umbraco.Web.BackOffice.Controllers } [HttpPost] - public ActionResult DeleteById(int id) + public IActionResult DeleteById(int id) { var macro = _macroService.GetById(id); diff --git a/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs b/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs index e43b40f97e..49f16f41ac 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MediaController.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Net.Mime; using System.Text; using System.Threading.Tasks; @@ -126,7 +125,7 @@ namespace Umbraco.Web.BackOffice.Controllers var contentType = _mediaTypeService.Get(contentTypeAlias); if (contentType == null) { - return new ValidationErrorResult(contentType, StatusCodes.Status404NotFound); + return NotFound(); } var emptyContent = _mediaService.CreateMedia("", parentId, contentType.Alias, _backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().ResultOr(Constants.Security.SuperUserId)); @@ -221,7 +220,7 @@ namespace Umbraco.Web.BackOffice.Controllers return GetById(guidUdi.Guid); } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); } /// @@ -392,7 +391,7 @@ namespace Umbraco.Web.BackOffice.Controllers return GetChildren(entity.Id, pageNumber, pageSize, orderBy, orderDirection, orderBySystemField, filter); } - return new ValidationErrorResult(entity, StatusCodes.Status404NotFound); + return NotFound(); } /// @@ -426,7 +425,7 @@ namespace Umbraco.Web.BackOffice.Controllers } } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); } #endregion @@ -906,14 +905,14 @@ namespace Umbraco.Web.BackOffice.Controllers { if (model == null) { - return new ValidationErrorResult(model, StatusCodes.Status404NotFound); + return NotFound(); } var toMove = _mediaService.GetById(model.Id); if (toMove == null) { - return new ValidationErrorResult(toMove, StatusCodes.Status404NotFound); + return NotFound(); } if (model.ParentId < 0) { @@ -932,7 +931,7 @@ namespace Umbraco.Web.BackOffice.Controllers var parent = _mediaService.GetById(model.ParentId); if (parent == null) { - return new ValidationErrorResult(parent, StatusCodes.Status404NotFound); + return NotFound(); } //check if the item is allowed under this one diff --git a/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs index 2446d480ef..1010615368 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Umbraco.Core; using Umbraco.Core.Dictionary; @@ -83,7 +81,7 @@ namespace Umbraco.Web.BackOffice.Controllers var ct = _mediaTypeService.Get(id); if (ct == null) { - return new ValidationErrorResult(ct, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(ct); @@ -102,7 +100,7 @@ namespace Umbraco.Web.BackOffice.Controllers var mediaType = _mediaTypeService.Get(id); if (mediaType == null) { - return new ValidationErrorResult(mediaType, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(mediaType); @@ -120,12 +118,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var mediaType = _mediaTypeService.Get(guidUdi.Guid); if (mediaType == null) { - return new ValidationErrorResult(mediaType, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(mediaType); @@ -145,7 +143,7 @@ namespace Umbraco.Web.BackOffice.Controllers var foundType = _mediaTypeService.Get(id); if (foundType == null) { - return new ValidationErrorResult(foundType, StatusCodes.Status404NotFound); + return NotFound(); } _mediaTypeService.Delete(foundType, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id); @@ -383,7 +381,7 @@ namespace Umbraco.Web.BackOffice.Controllers return new ActionResult>(GetAllowedChildren(entity.Id)); } - return new ValidationErrorResult(entity, StatusCodes.Status404NotFound); + return NotFound(); } /// @@ -404,7 +402,7 @@ namespace Umbraco.Web.BackOffice.Controllers } } - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); } #endregion diff --git a/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs b/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs index 47487d6c5b..23e6e30d81 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MemberController.cs @@ -2,13 +2,11 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; -using System.Net; using System.Net.Http; 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; @@ -173,13 +171,13 @@ namespace Umbraco.Web.BackOffice.Controllers IMember emptyContent; if (contentTypeAlias == null) { - return new ValidationErrorResult(contentTypeAlias, StatusCodes.Status404NotFound); + return NotFound(); } var contentType = _memberTypeService.Get(contentTypeAlias); if (contentType == null) { - return new ValidationErrorResult(contentType, StatusCodes.Status404NotFound); + return NotFound(); } var passwordGenerator = new PasswordGenerator(_passwordConfig); @@ -241,7 +239,7 @@ namespace Umbraco.Web.BackOffice.Controllers break; default: //we don't support anything else for members - return new ValidationErrorResult(contentItem.Action, StatusCodes.Status404NotFound); + return NotFound(); } //TODO: There's 3 things saved here and we should do this all in one transaction, which we can do here by wrapping in a scope diff --git a/src/Umbraco.Web.BackOffice/Controllers/MemberGroupController.cs b/src/Umbraco.Web.BackOffice/Controllers/MemberGroupController.cs index 6e8c002b7d..43df969ef5 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MemberGroupController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MemberGroupController.cs @@ -2,13 +2,11 @@ using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Umbraco.Core; using Umbraco.Core.Mapping; using Umbraco.Core.Models; using Umbraco.Core.Services; -using Umbraco.Web.Common.ActionsResults; using Umbraco.Web.Common.Attributes; using Umbraco.Web.Common.Authorization; using Umbraco.Web.Models.ContentEditing; @@ -50,7 +48,7 @@ namespace Umbraco.Web.BackOffice.Controllers var memberGroup = _memberGroupService.GetById(id); if (memberGroup == null) { - return new ValidationErrorResult(memberGroup, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(memberGroup); @@ -69,7 +67,7 @@ namespace Umbraco.Web.BackOffice.Controllers var memberGroup = _memberGroupService.GetById(id); if (memberGroup == null) { - return new ValidationErrorResult(memberGroup, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(memberGroup); @@ -85,12 +83,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var memberGroup = _memberGroupService.GetById(guidUdi.Guid); if (memberGroup == null) { - return new ValidationErrorResult(memberGroup, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(memberGroup); @@ -109,7 +107,7 @@ namespace Umbraco.Web.BackOffice.Controllers var memberGroup = _memberGroupService.GetById(id); if (memberGroup == null) { - return new ValidationErrorResult(memberGroup, StatusCodes.Status404NotFound); + return NotFound(); } _memberGroupService.Delete(memberGroup); @@ -135,7 +133,7 @@ namespace Umbraco.Web.BackOffice.Controllers var memberGroup = id > 0 ? _memberGroupService.GetById(id) : new MemberGroup(); if (memberGroup == null) { - return new ValidationErrorResult(memberGroup, StatusCodes.Status404NotFound); + return NotFound(); } memberGroup.Name = saveModel.Name; diff --git a/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs index 9f01987470..6e95680110 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs @@ -14,8 +14,6 @@ using Umbraco.Core.Security; using Umbraco.Web.Common.Attributes; using Umbraco.Web.Editors; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Umbraco.Web.Common.ActionsResults; using Umbraco.Web.Common.Authorization; namespace Umbraco.Web.BackOffice.Controllers @@ -70,7 +68,7 @@ namespace Umbraco.Web.BackOffice.Controllers var mt = _memberTypeService.Get(id); if (mt == null) { - return new ValidationErrorResult(mt, StatusCodes.Status404NotFound); + return NotFound(); } var dto =_umbracoMapper.Map(mt); @@ -88,7 +86,7 @@ namespace Umbraco.Web.BackOffice.Controllers var memberType = _memberTypeService.Get(id); if (memberType == null) { - return new ValidationErrorResult(memberType, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(memberType); @@ -105,12 +103,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var memberType = _memberTypeService.Get(guidUdi.Guid); if (memberType == null) { - return new ValidationErrorResult(memberType, StatusCodes.Status404NotFound); + return NotFound(); } var dto = _umbracoMapper.Map(memberType); @@ -129,7 +127,7 @@ namespace Umbraco.Web.BackOffice.Controllers var foundType = _memberTypeService.Get(id); if (foundType == null) { - return new ValidationErrorResult(foundType, StatusCodes.Status404NotFound); + return NotFound(); } _memberTypeService.Delete(foundType, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id); @@ -212,7 +210,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (ct.IsSensitiveProperty(foundOnContentType.Alias) && prop.IsSensitiveData == false) { //if these don't match, then we cannot continue, this user is not allowed to change this value - return new ValidationErrorResult(ct, StatusCodes.Status403Forbidden); + return Forbid(); } } } @@ -221,7 +219,7 @@ namespace Umbraco.Web.BackOffice.Controllers //if it is new, then we can just verify if any property has sensitive data turned on which is not allowed if (props.Any(prop => prop.IsSensitiveData)) { - return new ValidationErrorResult(props, StatusCodes.Status403Forbidden); + return Forbid(); } } } diff --git a/src/Umbraco.Web.BackOffice/Controllers/PackageController.cs b/src/Umbraco.Web.BackOffice/Controllers/PackageController.cs index 47944428d7..57f49bdc23 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/PackageController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/PackageController.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Net; using System.Text; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; using Semver; @@ -50,7 +49,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var package = _packagingService.GetCreatedPackageById(id); if (package == null) - return new ValidationErrorResult(package, StatusCodes.Status404NotFound); + return NotFound(); return package; } @@ -130,7 +129,7 @@ namespace Umbraco.Web.BackOffice.Controllers public ActionResult GetInstalledPackageById(int id) { var pack = _packagingService.GetInstalledPackageById(id); - if (pack == null) return new ValidationErrorResult(pack, StatusCodes.Status404NotFound); + if (pack == null) return NotFound(); return pack; } diff --git a/src/Umbraco.Web.BackOffice/Controllers/RelationTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/RelationTypeController.cs index 2a8a148555..0a6d174bdd 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/RelationTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/RelationTypeController.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; using System.Net.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -14,7 +13,6 @@ using Constants = Umbraco.Core.Constants; using Umbraco.Core.Mapping; using Umbraco.Web.Common.Attributes; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Umbraco.Web.Common.ActionsResults; using Umbraco.Web.Common.Authorization; @@ -56,7 +54,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (relationType == null) { - return new ValidationErrorResult(relationType, StatusCodes.Status404NotFound); + return NotFound(); } var display = _umbracoMapper.Map(relationType); @@ -74,7 +72,7 @@ namespace Umbraco.Web.BackOffice.Controllers var relationType = _relationService.GetRelationTypeById(id); if (relationType == null) { - return new ValidationErrorResult(relationType, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(relationType); } @@ -89,12 +87,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var relationType = _relationService.GetRelationTypeById(guidUdi.Guid); if (relationType == null) { - return new ValidationErrorResult(relationType, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(relationType); } diff --git a/src/Umbraco.Web.BackOffice/Controllers/TemplateController.cs b/src/Umbraco.Web.BackOffice/Controllers/TemplateController.cs index 4ba9306bd0..560e3bc78c 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/TemplateController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/TemplateController.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Umbraco.Core; using Umbraco.Core.IO; @@ -10,7 +9,6 @@ using Umbraco.Core.Mapping; using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Core.Strings; -using Umbraco.Web.Common.ActionsResults; using Umbraco.Web.Common.Attributes; using Umbraco.Web.Common.Authorization; using Umbraco.Web.Models.ContentEditing; @@ -66,7 +64,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var template = _fileService.GetTemplate(id); if (template == null) - return new ValidationErrorResult(template, StatusCodes.Status404NotFound); + return NotFound(); return _umbracoMapper.Map(template); } @@ -82,7 +80,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var template = _fileService.GetTemplate(id); if (template == null) - return new ValidationErrorResult(template, StatusCodes.Status404NotFound); + return NotFound(); return _umbracoMapper.Map(template); } @@ -97,12 +95,12 @@ namespace Umbraco.Web.BackOffice.Controllers { var guidUdi = id as GuidUdi; if (guidUdi == null) - return new ValidationErrorResult(guidUdi, StatusCodes.Status404NotFound); + return NotFound(); var template = _fileService.GetTemplate(guidUdi.Guid); if (template == null) { - return new ValidationErrorResult(template, StatusCodes.Status404NotFound); + return NotFound(); } return _umbracoMapper.Map(template); @@ -119,7 +117,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var template = _fileService.GetTemplate(id); if (template == null) - return new ValidationErrorResult(template, StatusCodes.Status404NotFound); + return NotFound(); _fileService.DeleteTemplate(template.Alias); return Ok(); @@ -166,7 +164,7 @@ namespace Umbraco.Web.BackOffice.Controllers // update var template = _fileService.GetTemplate(display.Id); if (template == null) - return new ValidationErrorResult(template, StatusCodes.Status404NotFound); + return NotFound(); var changeMaster = template.MasterTemplateAlias != display.MasterTemplateAlias; var changeAlias = template.Alias != display.Alias; @@ -238,7 +236,7 @@ namespace Umbraco.Web.BackOffice.Controllers { master = _fileService.GetTemplate(display.MasterTemplateAlias); if (master == null) - return new ValidationErrorResult(master, StatusCodes.Status404NotFound); + return NotFound(); } // we need to pass the template name as alias to keep the template file casing consistent with templates created with content diff --git a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs index 8475773caa..a8be6bd125 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs @@ -125,7 +125,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var urls = _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.GetUserAvatarUrls(_appCaches.RuntimeCache, _mediaFileSystem, _imageUrlGenerator); if (urls == null) - return new ValidationErrorResult("Could not access Gravatar endpoint"); + return BadRequest("Could not access Gravatar endpoint"); return urls; } @@ -141,7 +141,7 @@ namespace Umbraco.Web.BackOffice.Controllers { if (files is null) { - return new ValidationErrorResult(files, StatusCodes.Status415UnsupportedMediaType); + return new UnsupportedMediaTypeResult(); } var root = hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.TempFileUploads); @@ -229,7 +229,7 @@ namespace Umbraco.Web.BackOffice.Controllers var user = _userService.GetUserById(id); if (user == null) { - return new ValidationErrorResult(user, StatusCodes.Status404NotFound); + return NotFound(); } var result = _umbracoMapper.Map(user); return result; @@ -246,7 +246,7 @@ namespace Umbraco.Web.BackOffice.Controllers { if (ids == null) { - return new ValidationErrorResult(ids, StatusCodes.Status404NotFound); + return NotFound(); } if (ids.Length == 0) @@ -255,7 +255,7 @@ namespace Umbraco.Web.BackOffice.Controllers var users = _userService.GetUsersById(ids); if (users == null) { - return new ValidationErrorResult(users, StatusCodes.Status404NotFound); + return NotFound(); } var result = _umbracoMapper.MapEnumerable(users); @@ -342,7 +342,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (ModelState.IsValid == false) { - return new ValidationErrorResult(ModelState); + return BadRequest(ModelState); } if (_securitySettings.UsernameIsEmail) @@ -361,7 +361,7 @@ namespace Umbraco.Web.BackOffice.Controllers var canSaveUser = _userEditorAuthorizationHelper.IsAuthorized(_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser, null, null, null, userSave.UserGroups); if (canSaveUser == false) { - return new ValidationErrorResult(canSaveUser.Result, StatusCodes.Status401Unauthorized); + return Unauthorized(canSaveUser.Result); } //we want to create the user with the UserManager, this ensures the 'empty' (special) password @@ -419,7 +419,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (ModelState.IsValid == false) { - return new ValidationErrorResult(ModelState); + return BadRequest(ModelState); } IUser user; @@ -518,7 +518,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (user != null && (extraCheck == null || extraCheck(user))) { ModelState.AddModelError("Email", "A user with the email already exists"); - return new ValidationErrorResult(ModelState); + return BadRequest(ModelState); } return new ActionResult(user); @@ -532,7 +532,7 @@ namespace Umbraco.Web.BackOffice.Controllers ModelState.AddModelError( _securitySettings.UsernameIsEmail ? "Email" : "Username", "A user with the username already exists"); - return new ValidationErrorResult(ModelState); + return BadRequest(ModelState); } return new ActionResult(user); @@ -594,13 +594,13 @@ namespace Umbraco.Web.BackOffice.Controllers var found = _userService.GetUserById(intId.Result); if (found == null) - return new ValidationErrorResult(found, StatusCodes.Status404NotFound); + return NotFound(); //Perform authorization here to see if the current user can actually save this user with the info being requested var canSaveUser = _userEditorAuthorizationHelper.IsAuthorized(_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser, found, userSave.StartContentIds, userSave.StartMediaIds, userSave.UserGroups); if (canSaveUser == false) { - return new ValidationErrorResult(canSaveUser.Result, StatusCodes.Status401Unauthorized); + return Unauthorized(canSaveUser.Result); } var hasErrors = false; @@ -647,7 +647,7 @@ namespace Umbraco.Web.BackOffice.Controllers } if (hasErrors) - return new ValidationErrorResult(ModelState); + return BadRequest(ModelState); //merge the save data onto the user var user = _umbracoMapper.Map(userSave, found); @@ -671,19 +671,19 @@ namespace Umbraco.Web.BackOffice.Controllers if (ModelState.IsValid == false) { - return new ValidationErrorResult(ModelState); + return BadRequest(ModelState); } var intId = changingPasswordModel.Id.TryConvertTo(); if (intId.Success == false) { - return new ValidationErrorResult(intId, StatusCodes.Status404NotFound); + return NotFound(); } var found = _userService.GetUserById(intId.Result); if (found == null) { - return new ValidationErrorResult(found, StatusCodes.Status404NotFound); + return NotFound(); } // TODO: Why don't we inject this? Then we can just inject a logger