From 0b9f02630f012ba9a64e01973cd5bce718ab4a3e Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Tue, 12 Jan 2021 16:24:50 +0100 Subject: [PATCH] Continue replacing HttpResponseException --- .../Controllers/ContentTypeController.cs | 2 +- .../Controllers/ContentTypeControllerBase.cs | 4 ++-- .../Controllers/MediaTypeController.cs | 2 +- .../Controllers/MemberTypeController.cs | 2 +- .../Controllers/SectionController.cs | 4 ++-- .../Controllers/UserGroupsController.cs | 17 ++++++++-------- .../Trees/ApplicationTreeController.cs | 20 +++++++++---------- .../Trees/ContentTreeController.cs | 7 ++++--- .../Trees/ContentTreeControllerBase.cs | 8 ++++---- 9 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs index 67e6a6332c..e27968face 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs @@ -404,7 +404,7 @@ namespace Umbraco.Web.BackOffice.Controllers } }); - var display = _umbracoMapper.Map(savedCt); + var display = _umbracoMapper.Map(savedCt.Value); display.AddSuccessNotification( _localizedTextService.Localize("speechBubbles/contentTypeSavedHeader"), diff --git a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs index 90b4329d68..965059b83f 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/ContentTypeControllerBase.cs @@ -253,7 +253,7 @@ namespace Umbraco.Web.BackOffice.Controllers return CultureDictionary[text].IfNullOrWhiteSpace(text); } - protected TContentType PerformPostSave( + protected ActionResult PerformPostSave( TContentTypeSave contentTypeSave, Func getContentType, Action saveContentType, @@ -264,7 +264,7 @@ namespace Umbraco.Web.BackOffice.Controllers { var ctId = Convert.ToInt32(contentTypeSave.Id); var ct = ctId > 0 ? getContentType(ctId) : null; - if (ctId > 0 && ct == null) throw new HttpResponseException(HttpStatusCode.NotFound); + if (ctId > 0 && ct == null) return NotFound(); //Validate that there's no other ct with the same alias // it in fact cannot be the same as any content type alias (member, content or media) because diff --git a/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs index 1010615368..deab42db8d 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs @@ -280,7 +280,7 @@ namespace Umbraco.Web.BackOffice.Controllers i => _mediaTypeService.Get(i), type => _mediaTypeService.Save(type)); - var display = _umbracoMapper.Map(savedCt); + var display = _umbracoMapper.Map(savedCt.Value); display.AddSuccessNotification( _localizedTextService.Localize("speechBubbles/mediaTypeSavedHeader"), diff --git a/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs b/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs index 6e95680110..7598c0d449 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MemberTypeController.cs @@ -230,7 +230,7 @@ namespace Umbraco.Web.BackOffice.Controllers getContentType: i => ct, saveContentType: type => _memberTypeService.Save(type)); - var display =_umbracoMapper.Map(savedCt); + var display =_umbracoMapper.Map(savedCt.Value); display.AddSuccessNotification( _localizedTextService.Localize("speechBubbles/memberTypeSavedHeader"), diff --git a/src/Umbraco.Web.BackOffice/Controllers/SectionController.cs b/src/Umbraco.Web.BackOffice/Controllers/SectionController.cs index 097b5a3310..5b7119f754 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/SectionController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/SectionController.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Infrastructure; @@ -75,7 +75,7 @@ namespace Umbraco.Web.BackOffice.Controllers // get the first tree in the section and get its root node route path var sectionRoot = appTreeController.GetApplicationTrees(section.Alias, null, null).Result; - section.RoutePath = GetRoutePathForFirstTree(sectionRoot); + section.RoutePath = GetRoutePathForFirstTree(sectionRoot.Value); } return sectionModels; diff --git a/src/Umbraco.Web.BackOffice/Controllers/UserGroupsController.cs b/src/Umbraco.Web.BackOffice/Controllers/UserGroupsController.cs index 64aef74257..ff5ade53c1 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/UserGroupsController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/UserGroupsController.cs @@ -1,7 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Umbraco.Core.Mapping; using Umbraco.Core.Models; @@ -9,15 +10,13 @@ using Umbraco.Core.Models.Membership; using Umbraco.Core.Security; using Umbraco.Core.Services; using Umbraco.Core.Strings; -using Umbraco.Web.BackOffice.Filters; using Umbraco.Web.BackOffice.ActionResults; +using Umbraco.Web.BackOffice.Filters; using Umbraco.Web.Common.Attributes; +using Umbraco.Web.Common.Authorization; using Umbraco.Web.Common.Exceptions; using Umbraco.Web.Models.ContentEditing; -using Umbraco.Web.Security; using Constants = Umbraco.Core.Constants; -using Microsoft.AspNetCore.Authorization; -using Umbraco.Web.Common.Authorization; namespace Umbraco.Web.BackOffice.Controllers { @@ -52,7 +51,7 @@ namespace Umbraco.Web.BackOffice.Controllers } [UserGroupValidate] - public UserGroupDisplay PostSaveUserGroup(UserGroupSave userGroupSave) + public ActionResult PostSaveUserGroup(UserGroupSave userGroupSave) { if (userGroupSave == null) throw new ArgumentNullException(nameof(userGroupSave)); @@ -62,14 +61,14 @@ namespace Umbraco.Web.BackOffice.Controllers var isAuthorized = authHelper.AuthorizeGroupAccess(_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser, userGroupSave.Alias); if (isAuthorized == false) - throw new HttpResponseException(HttpStatusCode.Unauthorized, isAuthorized.Result); + return Unauthorized(isAuthorized.Result); //if sections were added we need to check that the current user has access to that section isAuthorized = authHelper.AuthorizeSectionChanges(_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser, userGroupSave.PersistedUserGroup.AllowedSections, userGroupSave.Sections); if (isAuthorized == false) - throw new HttpResponseException(HttpStatusCode.Unauthorized, isAuthorized.Result); + return Unauthorized(isAuthorized.Result); //if start nodes were changed we need to check that the current user has access to them isAuthorized = authHelper.AuthorizeStartNodeChanges(_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser, @@ -78,7 +77,7 @@ namespace Umbraco.Web.BackOffice.Controllers userGroupSave.PersistedUserGroup.StartMediaId, userGroupSave.StartMediaId); if (isAuthorized == false) - throw new HttpResponseException(HttpStatusCode.Unauthorized, isAuthorized.Result); + return Unauthorized(isAuthorized.Result); //need to ensure current user is in a group if not an admin to avoid a 401 EnsureNonAdminUserIsInSavedUserGroup(userGroupSave); diff --git a/src/Umbraco.Web.BackOffice/Trees/ApplicationTreeController.cs b/src/Umbraco.Web.BackOffice/Trees/ApplicationTreeController.cs index 000740e27e..c19229b1e6 100644 --- a/src/Umbraco.Web.BackOffice/Trees/ApplicationTreeController.cs +++ b/src/Umbraco.Web.BackOffice/Trees/ApplicationTreeController.cs @@ -62,16 +62,16 @@ namespace Umbraco.Web.BackOffice.Trees /// /// Tree use. /// - public async Task GetApplicationTrees(string application, string tree, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormCollection queryStrings, TreeUse use = TreeUse.Main) + public async Task> GetApplicationTrees(string application, string tree, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormCollection queryStrings, TreeUse use = TreeUse.Main) { application = application.CleanForXss(); if (string.IsNullOrEmpty(application)) - throw new HttpResponseException(HttpStatusCode.NotFound); + return NotFound(); var section = _sectionService.GetByAlias(application); if (section == null) - throw new HttpResponseException(HttpStatusCode.NotFound); + return NotFound(); //find all tree definitions that have the current application alias var groupedTrees = _treeService.GetBySectionGrouped(application, use); @@ -93,13 +93,13 @@ namespace Umbraco.Web.BackOffice.Trees : allTrees.FirstOrDefault(x => x.TreeAlias == tree); if (t == null) - throw new HttpResponseException(HttpStatusCode.NotFound); + return NotFound(); var treeRootNode = await GetTreeRootNode(t, Constants.System.Root, queryStrings); if (treeRootNode != null) return treeRootNode; - throw new HttpResponseException(HttpStatusCode.NotFound); + return NotFound(); } // handle requests for all trees @@ -219,7 +219,7 @@ namespace Umbraco.Web.BackOffice.Trees if (tree == null) throw new ArgumentNullException(nameof(tree)); - var controller = (TreeControllerBase)await GetApiControllerProxy(tree.TreeControllerType, "GetRootNode", querystring); + var controller = (TreeControllerBase)(await GetApiControllerProxy(tree.TreeControllerType, "GetRootNode", querystring)).Value; var rootNode = controller.GetRootNode(querystring); if (rootNode == null) throw new InvalidOperationException($"Failed to get root node for tree \"{tree.TreeAlias}\"."); @@ -241,7 +241,7 @@ namespace Umbraco.Web.BackOffice.Trees d["id"] = StringValues.Empty; var proxyQuerystring = new FormCollection(d); - var controller = (TreeControllerBase)await GetApiControllerProxy(tree.TreeControllerType, "GetNodes", proxyQuerystring); + var controller = (TreeControllerBase)(await GetApiControllerProxy(tree.TreeControllerType, "GetNodes", proxyQuerystring)).Value; return controller.GetNodes(id.ToInvariantString(), querystring); } @@ -257,7 +257,7 @@ namespace Umbraco.Web.BackOffice.Trees /// and context etc. so it can execute the specified . Runs the authorization /// filters for that action, to ensure that the user has permission to execute it. /// - private async Task GetApiControllerProxy(Type controllerType, string action, FormCollection querystring) + private async Task> GetApiControllerProxy(Type controllerType, string action, FormCollection querystring) { // note: this is all required in order to execute the auth-filters for the sub request, we // need to "trick" mvc into thinking that it is actually executing the proxied controller. @@ -289,11 +289,9 @@ namespace Umbraco.Web.BackOffice.Trees var isAllowed = await controller.ControllerContext.InvokeAuthorizationFiltersForRequest(actionContext); if (!isAllowed) - throw new HttpResponseException(HttpStatusCode.Forbidden); + return Forbid(); return controller; } - - } } diff --git a/src/Umbraco.Web.BackOffice/Trees/ContentTreeController.cs b/src/Umbraco.Web.BackOffice/Trees/ContentTreeController.cs index 404ebfdb3a..5cdd356296 100644 --- a/src/Umbraco.Web.BackOffice/Trees/ContentTreeController.cs +++ b/src/Umbraco.Web.BackOffice/Trees/ContentTreeController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -21,6 +21,7 @@ using Umbraco.Core.Configuration.Models; using Microsoft.Extensions.Options; using Umbraco.Web.Trees; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using Umbraco.Web.Common.Authorization; using Umbraco.Core.Trees; @@ -236,7 +237,7 @@ namespace Umbraco.Web.BackOffice.Trees return HasPathAccess(entity, queryStrings); } - protected override IEnumerable GetChildEntities(string id, FormCollection queryStrings) + protected override ActionResult> GetChildEntities(string id, FormCollection queryStrings) { var result = base.GetChildEntities(id, queryStrings); var culture = queryStrings["culture"].TryConvertTo(); @@ -245,7 +246,7 @@ namespace Umbraco.Web.BackOffice.Trees var cultureVal = (culture.Success ? culture.Result : null) ?? _localizationService.GetDefaultLanguageIsoCode(); // set names according to variations - foreach (var entity in result) + foreach (var entity in result.Value) { EnsureName(entity, cultureVal); } diff --git a/src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs b/src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs index 53a6f02a79..047778720b 100644 --- a/src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs +++ b/src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -211,7 +211,7 @@ namespace Umbraco.Web.BackOffice.Trees // get child entities - if id is root, but user's start nodes do not contain the // root node, this returns the start nodes instead of root's children - var entities = GetChildEntities(id, queryStrings).ToList(); + var entities = GetChildEntities(id, queryStrings).Value.ToList(); //get the current user start node/paths GetUserStartNodes(out var userStartNodes, out var userStartNodePaths); @@ -257,7 +257,7 @@ namespace Umbraco.Web.BackOffice.Trees protected abstract UmbracoObjectTypes UmbracoObjectType { get; } - protected virtual IEnumerable GetChildEntities(string id, FormCollection queryStrings) + protected virtual ActionResult> GetChildEntities(string id, FormCollection queryStrings) { // try to parse id as an integer else use GetEntityFromId // which will grok Guids, Udis, etc and let use obtain the id @@ -265,7 +265,7 @@ namespace Umbraco.Web.BackOffice.Trees { var entity = GetEntityFromId(id); if (entity == null) - throw new HttpResponseException(HttpStatusCode.NotFound); + return NotFound(); entityId = entity.Id; }