From 772041bb8fdfcd276647dad913b2e7ede4cb6e17 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Wed, 3 Jun 2020 20:39:54 +0200 Subject: [PATCH] Migrated MacrosController.cs --- .../Controllers/BackOfficeServerVariables.cs | 8 +- .../Controllers}/MacrosController.cs | 207 ++++++++---------- .../Editors/BackOfficeServerVariables.cs | 6 +- src/Umbraco.Web/Umbraco.Web.csproj | 1 - 4 files changed, 95 insertions(+), 127 deletions(-) rename src/{Umbraco.Web/Editors => Umbraco.Web.BackOffice/Controllers}/MacrosController.cs (59%) diff --git a/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs b/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs index 32ee733cd2..0f4c1a1040 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs @@ -204,10 +204,10 @@ namespace Umbraco.Web.BackOffice.Controllers // "macroRenderingApiBaseUrl", _linkGenerator.GetUmbracoApiServiceBaseUrl( // controller => controller.GetMacroParameters(0)) // }, - // { - // "macroApiBaseUrl", _linkGenerator.GetUmbracoApiServiceBaseUrl( - // controller => controller.Create(null)) - // }, + { + "macroApiBaseUrl", _linkGenerator.GetUmbracoApiServiceBaseUrl( + controller => controller.Create(null)) + }, { "authenticationApiBaseUrl", _linkGenerator.GetUmbracoApiServiceBaseUrl( controller => controller.PostLogin(null)) diff --git a/src/Umbraco.Web/Editors/MacrosController.cs b/src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs similarity index 59% rename from src/Umbraco.Web/Editors/MacrosController.cs rename to src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs index babeb921fa..5b61b91c66 100644 --- a/src/Umbraco.Web/Editors/MacrosController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs @@ -3,28 +3,23 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Net.Http; -using System.Web.Http; -using Umbraco.Core; -using Umbraco.Core.Cache; -using Umbraco.Core.Configuration; +using Microsoft.AspNetCore.Mvc; +using Umbraco.Core.Hosting; using Umbraco.Core.Logging; using Umbraco.Core.Models; -using Umbraco.Core.Persistence; using Umbraco.Core.Strings; using Umbraco.Web.Models.ContentEditing; -using Umbraco.Web.Mvc; -using Umbraco.Web.WebApi; -using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; -using Umbraco.Core.Mapping; -using System.Web.Http.Controllers; -using Umbraco.Core.IO; using Umbraco.Core.PropertyEditors; -using Umbraco.Web.Routing; +using Umbraco.Web.BackOffice.Filters; +using Umbraco.Web.Common.Attributes; +using Umbraco.Web.Common.Exceptions; +using Umbraco.Web.Security; +using Umbraco.Core; +using Umbraco.Core.Mapping; -namespace Umbraco.Web.Editors +namespace Umbraco.Web.BackOffice.Controllers { /// @@ -32,45 +27,35 @@ namespace Umbraco.Web.Editors /// [PluginController("UmbracoApi")] [UmbracoTreeAuthorize(Constants.Trees.Macros)] - [MacrosControllerConfiguration] public class MacrosController : BackOfficeNotificationsController { private readonly ParameterEditorCollection _parameterEditorCollection; - private readonly IIOHelper _ioHelper; private readonly IMacroService _macroService; + private readonly IShortStringHelper _shortStringHelper; + private readonly IWebSecurity _webSecurity; + private readonly ILogger _logger; + private readonly IHostingEnvironment _hostingEnvironment; + private readonly UmbracoMapper _umbracoMapper; public MacrosController( - IGlobalSettings globalSettings, - IUmbracoContextAccessor umbracoContextAccessor, - ISqlContext sqlContext, - ServiceContext services, - AppCaches appCaches, - IProfilingLogger logger, - IRuntimeState runtimeState, - IShortStringHelper shortStringHelper, - UmbracoMapper umbracoMapper, ParameterEditorCollection parameterEditorCollection, - IIOHelper ioHelper, - IPublishedUrlProvider publishedUrlProvider) - : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, shortStringHelper, umbracoMapper, publishedUrlProvider) + IMacroService macroService, + IShortStringHelper shortStringHelper, + IWebSecurity webSecurity, + ILogger logger, + IHostingEnvironment hostingEnvironment, + UmbracoMapper umbracoMapper + ) { - _parameterEditorCollection = parameterEditorCollection; - _ioHelper = ioHelper; - _macroService = Services.MacroService; + _parameterEditorCollection = parameterEditorCollection ?? throw new ArgumentNullException(nameof(parameterEditorCollection)); + _macroService = macroService ?? throw new ArgumentNullException(nameof(macroService)); + _shortStringHelper = shortStringHelper ?? throw new ArgumentNullException(nameof(shortStringHelper)); + _webSecurity = webSecurity ?? throw new ArgumentNullException(nameof(webSecurity)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment)); + _umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper)); } - /// - /// Configures this controller with a custom action selector - /// - private class MacrosControllerConfigurationAttribute : Attribute, IControllerConfiguration - { - public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) - { - controllerSettings.Services.Replace(typeof(IHttpActionSelector), new ParameterSwapControllerActionSelector( - new ParameterSwapControllerActionSelector.ParameterSwapInfo("GetById", "id", typeof(int), typeof(Guid), typeof(Udi)) - )); - } - } /// /// Creates a new macro @@ -79,128 +64,133 @@ namespace Umbraco.Web.Editors /// The name. /// /// - /// The . + /// The id of the created macro /// [HttpPost] - public HttpResponseMessage Create(string name) + public ActionResult Create(string name) { if (string.IsNullOrWhiteSpace(name)) { - return this.ReturnErrorResponse("Name can not be empty"); + throw HttpResponseException.CreateNotificationValidationErrorResponse("Name can not be empty"); } - var alias = name.ToSafeAlias(ShortStringHelper); + var alias = name.ToSafeAlias(_shortStringHelper); if (_macroService.GetByAlias(alias) != null) { - return this.ReturnErrorResponse("Macro with this alias already exists"); + throw HttpResponseException.CreateNotificationValidationErrorResponse("Macro with this alias already exists"); } if (name == null || name.Length > 255) { - return this.ReturnErrorResponse("Name cannnot be more than 255 characters in length."); + throw HttpResponseException.CreateNotificationValidationErrorResponse("Name cannnot be more than 255 characters in length."); } try { - var macro = new Macro(ShortStringHelper) + var macro = new Macro(_shortStringHelper) { Alias = alias, Name = name, MacroSource = string.Empty }; - _macroService.Save(macro, this.Security.CurrentUser.Id); + _macroService.Save(macro, _webSecurity.CurrentUser.Id); - return this.Request.CreateResponse(HttpStatusCode.OK, macro.Id); + return macro.Id; } catch (Exception exception) { - return this.ReturnErrorResponse("Error creating macro", true, exception); + const string errorMessage = "Error creating macro"; + _logger.Error(exception, errorMessage); + throw HttpResponseException.CreateNotificationValidationErrorResponse(errorMessage); } } [HttpGet] - public HttpResponseMessage GetById(int id) + [DetermineAmbiguousActionByPassingParameters] + public MacroDisplay GetById(int id) { var macro = _macroService.GetById(id); if (macro == null) { - return this.ReturnErrorResponse($"Macro with id {id} does not exist"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with id {id} does not exist"); } var macroDisplay = MapToDisplay(macro); - return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); + return macroDisplay; } [HttpGet] - public HttpResponseMessage GetById(Guid id) + [DetermineAmbiguousActionByPassingParameters] + public MacroDisplay GetById(Guid id) { var macro = _macroService.GetById(id); if (macro == null) { - return this.ReturnErrorResponse($"Macro with id {id} does not exist"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with id {id} does not exist"); } var macroDisplay = MapToDisplay(macro); - return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); + return macroDisplay; } [HttpGet] - public HttpResponseMessage GetById(Udi id) + [DetermineAmbiguousActionByPassingParameters] + public MacroDisplay GetById(Udi id) { var guidUdi = id as GuidUdi; if (guidUdi == null) - this.ReturnErrorResponse($"Macro with id {id} does not exist"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with id {id} does not exist"); var macro = _macroService.GetById(guidUdi.Guid); if (macro == null) { - return this.ReturnErrorResponse($"Macro with id {id} does not exist"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with id {id} does not exist"); } var macroDisplay = MapToDisplay(macro); - return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); + return macroDisplay; } [HttpPost] - public HttpResponseMessage DeleteById(int id) + public OkResult DeleteById(int id) { var macro = _macroService.GetById(id); if (macro == null) { - return this.ReturnErrorResponse($"Macro with id {id} does not exist"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with id {id} does not exist"); } _macroService.Delete(macro); - return Request.CreateResponse(HttpStatusCode.OK); + return Ok(); } [HttpPost] - public HttpResponseMessage Save(MacroDisplay macroDisplay) + public MacroDisplay Save(MacroDisplay macroDisplay) { if (macroDisplay == null) { - return this.ReturnErrorResponse($"No macro data found in request"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"No macro data found in request"); } if (macroDisplay.Name == null || macroDisplay.Name.Length > 255) { - return this.ReturnErrorResponse("Name cannnot be more than 255 characters in length."); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Name cannnot be more than 255 characters in length."); } var macro = _macroService.GetById(int.Parse(macroDisplay.Id.ToString())); if (macro == null) { - return this.ReturnErrorResponse($"Macro with id {macroDisplay.Id} does not exist"); + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with id {macroDisplay.Id} does not exist"); } if (macroDisplay.Alias != macro.Alias) @@ -209,8 +199,8 @@ namespace Umbraco.Web.Editors if (macroByAlias != null) { - return this.ReturnErrorResponse("Macro with this alias already exists"); - } + throw HttpResponseException.CreateNotificationValidationErrorResponse($"Macro with this alias already exists"); + } } macro.Alias = macroDisplay.Alias; @@ -225,17 +215,19 @@ namespace Umbraco.Web.Editors try { - _macroService.Save(macro, this.Security.CurrentUser.Id); + _macroService.Save(macro, _webSecurity.CurrentUser.Id); macroDisplay.Notifications.Clear(); macroDisplay.Notifications.Add(new BackOfficeNotification("Success", "Macro saved", NotificationStyle.Success)); - return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); + return macroDisplay; } catch (Exception exception) { - return this.ReturnErrorResponse("Error creating macro", true, exception); + const string errorMessage = "Error creating macro"; + _logger.Error(exception, errorMessage); + throw HttpResponseException.CreateNotificationValidationErrorResponse(errorMessage); } } @@ -245,13 +237,13 @@ namespace Umbraco.Web.Editors /// /// The . /// - public HttpResponseMessage GetPartialViews() + public List GetPartialViews() { var views = new List(); views.AddRange(this.FindPartialViewsFiles()); - return this.Request.CreateResponse(HttpStatusCode.OK, views); + return views; } /// @@ -260,9 +252,9 @@ namespace Umbraco.Web.Editors /// /// The . /// - public HttpResponseMessage GetParameterEditors() + public ParameterEditorCollection GetParameterEditors() { - return this.Request.CreateResponse(HttpStatusCode.OK, _parameterEditorCollection); + return _parameterEditorCollection; } /// @@ -271,7 +263,7 @@ namespace Umbraco.Web.Editors /// /// The . /// - public HttpResponseMessage GetGroupedParameterEditors() + public IDictionary> GetGroupedParameterEditors() { var parameterEditors = _parameterEditorCollection.ToArray(); @@ -280,7 +272,7 @@ namespace Umbraco.Web.Editors .OrderBy(x => x.Key) .ToDictionary(group => group.Key, group => group.OrderBy(d => d.Name).AsEnumerable()); - return this.Request.CreateResponse(HttpStatusCode.OK, grouped); + return grouped; } /// @@ -289,39 +281,16 @@ namespace Umbraco.Web.Editors /// /// The . /// - public HttpResponseMessage GetParameterEditorByAlias(string alias) + public IDataEditor GetParameterEditorByAlias(string alias) { var parameterEditors = _parameterEditorCollection.ToArray(); var parameterEditor = parameterEditors.FirstOrDefault(x => x.Alias.InvariantEquals(alias)); - return this.Request.CreateResponse(HttpStatusCode.OK, parameterEditor); + return parameterEditor; } - /// - /// Returns a error response and optionally logs it - /// - /// - /// The error message. - /// - /// - /// Value to indicate if the error needs to be logged - /// - /// - /// The exception to log - /// - /// - /// The . - /// - private HttpResponseMessage ReturnErrorResponse(string message, bool logError = false, Exception exception = null) - { - if (logError && exception != null) - { - this.Logger.Error(exception, message); - } - return this.Request.CreateNotificationValidationErrorResponse(message); - } /// /// Finds all the macro partials @@ -347,7 +316,7 @@ namespace Umbraco.Web.Editors /// private IEnumerable FindPartialViewFilesInViewsFolder() { - var partialsDir = _ioHelper.MapPath(Constants.SystemDirectories.MacroPartials); + var partialsDir = _hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.MacroPartials); return this.FindPartialViewFilesInFolder( partialsDir, @@ -364,7 +333,7 @@ namespace Umbraco.Web.Editors { var files = new List(); - var appPluginsFolder = new DirectoryInfo(_ioHelper.MapPath(Constants.SystemDirectories.AppPlugins)); + var appPluginsFolder = new DirectoryInfo(_hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.AppPlugins)); if (!appPluginsFolder.Exists) { @@ -407,17 +376,21 @@ namespace Umbraco.Web.Editors var files = new List(); var dirInfo = new DirectoryInfo(path); - foreach (var dir in dirInfo.GetDirectories()) + if (dirInfo.Exists) { - files.AddRange(this.FindPartialViewFilesInFolder(orgPath, path + "/" + dir.Name, prefixVirtualPath)); + foreach (var dir in dirInfo.GetDirectories()) + { + files.AddRange(this.FindPartialViewFilesInFolder(orgPath, path + "/" + dir.Name, prefixVirtualPath)); + } + + var fileInfo = dirInfo.GetFiles("*.*"); + + files.AddRange( + fileInfo.Select(file => + prefixVirtualPath.TrimEnd('/') + "/" + (path.Replace(orgPath, string.Empty).Trim('/') + "/" + file.Name).Trim('/'))); + } - var fileInfo = dirInfo.GetFiles("*.*"); - - files.AddRange( - fileInfo.Select(file => - prefixVirtualPath.TrimEnd('/') + "/" + (path.Replace(orgPath, string.Empty).Trim('/') + "/" + file.Name).Trim('/'))); - return files; } @@ -428,7 +401,7 @@ namespace Umbraco.Web.Editors /// private MacroDisplay MapToDisplay(IMacro macro) { - var display = Mapper.Map(macro); + var display = _umbracoMapper.Map(macro); var parameters = macro.Properties.Values .OrderBy(x => x.SortOrder) diff --git a/src/Umbraco.Web/Editors/BackOfficeServerVariables.cs b/src/Umbraco.Web/Editors/BackOfficeServerVariables.cs index 38458c932b..e30467f2b9 100644 --- a/src/Umbraco.Web/Editors/BackOfficeServerVariables.cs +++ b/src/Umbraco.Web/Editors/BackOfficeServerVariables.cs @@ -192,11 +192,7 @@ namespace Umbraco.Web.Editors "macroRenderingApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl( controller => controller.GetMacroParameters(0)) }, - { - "macroApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl( - controller => controller.Create(null)) - }, - + { "currentUserApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl( controller => controller.PostChangePassword(null)) diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index ecee9c0bb8..e010a68ca8 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -157,7 +157,6 @@ -