# Conflicts: # build/NuSpecs/UmbracoCms.Web.nuspec # src/SolutionInfo.cs # src/Umbraco.Core/Cache/MediaCacheRefresher.cs # src/Umbraco.Core/Composing/ComponentCollection.cs # src/Umbraco.Core/Composing/Composers.cs # src/Umbraco.Core/Composing/TypeFinder.cs # src/Umbraco.Core/Composing/TypeLoader.cs # src/Umbraco.Core/Configuration/Grid/GridEditorsConfig.cs # src/Umbraco.Core/Constants-SvgSanitizer.cs # src/Umbraco.Core/ContentApps/ContentAppFactoryCollection.cs # src/Umbraco.Core/Extensions/PublishedContentExtensions.cs # src/Umbraco.Core/Extensions/PublishedPropertyExtension.cs # src/Umbraco.Core/Extensions/StringExtensions.cs # src/Umbraco.Core/HealthChecks/Checks/Security/ExcessiveHeadersCheck.cs # src/Umbraco.Core/HealthChecks/HealthCheckResults.cs # src/Umbraco.Core/IO/FileSystems.cs # src/Umbraco.Core/IO/IOHelper.cs # src/Umbraco.Core/IO/MediaFileSystem.cs # src/Umbraco.Core/IO/PhysicalFileSystem.cs # src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs # src/Umbraco.Core/Logging/DisposableTimer.cs # src/Umbraco.Core/Logging/ILogger.cs # src/Umbraco.Core/Logging/LogProfiler.cs # src/Umbraco.Core/Logging/OwinLogger.cs # src/Umbraco.Core/Manifest/ManifestWatcher.cs # src/Umbraco.Core/Mapping/UmbracoMapper.cs # src/Umbraco.Core/Media/UploadAutoFillProperties.cs # src/Umbraco.Core/Migrations/Install/DatabaseDataCreator.cs # src/Umbraco.Core/Migrations/Upgrade/V_8_0_0/DataTypes/PreValueMigratorCollection.cs # src/Umbraco.Core/Models/Mapping/ContentPropertyBasicMapper.cs # src/Umbraco.Core/Models/Mapping/DataTypeMapDefinition.cs # src/Umbraco.Core/Models/Mapping/MacroMapDefinition.cs # src/Umbraco.Core/Models/Member.cs # src/Umbraco.Core/Packaging/PackageActionRunner.cs # src/Umbraco.Core/PropertyEditors/DataValueEditor.cs # src/Umbraco.Core/PropertyEditors/Validators/EyeDropperColorPickerConfigurationEditor.cs # src/Umbraco.Core/PropertyEditors/Validators/EyeDropperColorPickerPropertyEditor.cs # src/Umbraco.Core/Routing/DefaultUrlProvider.cs # src/Umbraco.Core/Runtime/CoreRuntime.cs # src/Umbraco.Core/Runtime/MainDom.cs # src/Umbraco.Core/RuntimeState.cs # src/Umbraco.Core/Scoping/ScopeProvider.cs # src/Umbraco.Core/Sync/DatabaseServerMessenger.cs # src/Umbraco.Core/Templates/HtmlUrlParser.cs # src/Umbraco.Core/UriExtensions.cs # src/Umbraco.Examine.Lucene/UmbracoContentIndex.cs # src/Umbraco.Examine.Lucene/UmbracoExamineIndex.cs # src/Umbraco.Infrastructure/Examine/IndexRebuilder.cs # src/Umbraco.Infrastructure/Manifest/DataEditorConverter.cs # src/Umbraco.Infrastructure/Manifest/ManifestParser.cs # src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs # src/Umbraco.Infrastructure/Migrations/Install/DatabaseSchemaCreator.cs # src/Umbraco.Infrastructure/Migrations/MigrationPlan.cs # src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/DataTypeMigration.cs # src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/DropDownPropertyEditorsMigration.cs # src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/MergeDateAndDateTimePropertyEditor.cs # src/Umbraco.Infrastructure/Migrations/Upgrade/V_8_0_0/RadioAndCheckboxPropertyEditorsMigration.cs # src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeRepository.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MediaRepository.cs # src/Umbraco.Infrastructure/Persistence/UmbracoDatabase.cs # src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs # src/Umbraco.Infrastructure/PropertyEditors/BlockEditorPropertyEditor.cs # src/Umbraco.Infrastructure/PropertyEditors/ImageCropperPropertyEditor.cs # src/Umbraco.Infrastructure/PropertyEditors/MultiUrlPickerValueEditor.cs # src/Umbraco.Infrastructure/PropertyEditors/NestedContentPropertyEditor.cs # src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs # src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/GridValueConverter.cs # src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs # src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/JsonValueConverter.cs # src/Umbraco.Infrastructure/PublishedCache/PublishedContentTypeCache.cs # src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs # src/Umbraco.Infrastructure/Scoping/Scope.cs # src/Umbraco.Infrastructure/Search/ExamineNotificationHandler.cs # src/Umbraco.Infrastructure/Services/Implement/ContentService.cs # src/Umbraco.Infrastructure/Services/Implement/LocalizedTextService.cs # src/Umbraco.Infrastructure/Services/Implement/LocalizedTextServiceFileSources.cs # src/Umbraco.Infrastructure/Services/Implement/MediaService.cs # src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs # src/Umbraco.Infrastructure/Sync/ServerMessengerBase.cs # src/Umbraco.ModelsBuilder.Embedded/Compose/ModelsBuilderComposer.cs # src/Umbraco.ModelsBuilder.Embedded/LiveModelsProvider.cs # src/Umbraco.PublishedCache.NuCache/ContentStore.cs # src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEventsTests.cs # src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Mapping/MappingTests.cs # src/Umbraco.Tests.UnitTests/Umbraco.ModelsBuilder.Embedded/BuilderTests.cs # src/Umbraco.Tests/Composing/TypeLoaderTests.cs # src/Umbraco.Tests/LegacyXmlPublishedCache/DictionaryPublishedContent.cs # src/Umbraco.Tests/LegacyXmlPublishedCache/LegacyBackgroundTask/BackgroundTaskRunner.cs # src/Umbraco.Tests/LegacyXmlPublishedCache/PreviewContent.cs # src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedMediaCache.cs # src/Umbraco.Tests/LegacyXmlPublishedCache/XmlStore.cs # src/Umbraco.Tests/LegacyXmlPublishedCache/XmlStoreFilePersister.cs # src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs # src/Umbraco.Tests/Services/PerformanceTests.cs # src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs # src/Umbraco.Tests/Testing/TestingTests/MockTests.cs # src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs # src/Umbraco.Web.BackOffice/Controllers/ContentController.cs # src/Umbraco.Web.BackOffice/Controllers/DashboardController.cs # src/Umbraco.Web.BackOffice/Controllers/ExamineManagementController.cs # src/Umbraco.Web.BackOffice/HealthChecks/HealthCheckController.cs # src/Umbraco.Web.BackOffice/PropertyEditors/RteEmbedController.cs # src/Umbraco.Web.BackOffice/Services/IconService.cs # src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs # src/Umbraco.Web.Common/Extensions/FormCollectionExtensions.cs # src/Umbraco.Web.Common/Install/InstallApiController.cs # src/Umbraco.Web.Common/Macros/MacroRenderer.cs # src/Umbraco.Web.Common/ModelsBuilder/PureLiveModelFactory.cs # src/Umbraco.Web.UI.Client/package-lock.json # src/Umbraco.Web.UI.Client/src/views/memberTypes/copy.controller.js # src/Umbraco.Web.UI.Client/src/views/memberTypes/copy.html # src/Umbraco.Web.UI.NetCore/umbraco/UmbracoBackOffice/Default.cshtml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/da.xml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en.xml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en_us.xml # src/Umbraco.Web.UI/config/splashes/noNodes.aspx # src/Umbraco.Web/AspNet/AspNetHttpContextAccessor.cs # src/Umbraco.Web/Cache/DistributedCacheBinder.cs # src/Umbraco.Web/Cache/DistributedCacheBinder_Handlers.cs # src/Umbraco.Web/Editors/AuthenticationController.cs # src/Umbraco.Web/Editors/BackOfficeController.cs # src/Umbraco.Web/Editors/Binders/ContentModelBinderHelper.cs # src/Umbraco.Web/Editors/ContentControllerBase.cs # src/Umbraco.Web/Editors/ContentTypeController.cs # src/Umbraco.Web/Editors/DictionaryController.cs # src/Umbraco.Web/Editors/MemberTypeController.cs # src/Umbraco.Web/Editors/PasswordChanger.cs # src/Umbraco.Web/Editors/RelationTypeController.cs # src/Umbraco.Web/Editors/TinyMceController.cs # src/Umbraco.Web/HealthCheck/Checks/Security/HttpsCheck.cs # src/Umbraco.Web/HtmlHelperRenderExtensions.cs # src/Umbraco.Web/HttpUrlHelperExtensions.cs # src/Umbraco.Web/HybridEventMessagesAccessor.cs # src/Umbraco.Web/ImageCropperTemplateExtensions.cs # src/Umbraco.Web/JavaScript/ClientDependencyConfiguration.cs # src/Umbraco.Web/Mvc/RenderMvcController.cs # src/Umbraco.Web/PublishedCache/NuCache/DataSource/DatabaseDataSource.cs # src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshotService.cs # src/Umbraco.Web/Routing/ContentFinderByConfigured404.cs # src/Umbraco.Web/Routing/ContentFinderByIdPath.cs # src/Umbraco.Web/Routing/ContentFinderByRedirectUrl.cs # src/Umbraco.Web/Routing/ContentFinderByUrl.cs # src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs # src/Umbraco.Web/Routing/ContentFinderByUrlAndTemplate.cs # src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs # src/Umbraco.Web/Routing/PublishedRouter.cs # src/Umbraco.Web/Runtime/WebInitialComposer.cs # src/Umbraco.Web/Scheduling/KeepAlive.cs # src/Umbraco.Web/Scheduling/ScheduledPublishing.cs # src/Umbraco.Web/Scheduling/TempFileCleanup.cs # src/Umbraco.Web/Security/MembershipHelper.cs # src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs # src/Umbraco.Web/Trees/MemberTreeController.cs # src/Umbraco.Web/Trees/MemberTypeAndGroupTreeControllerBase.cs # src/Umbraco.Web/Trees/MemberTypeTreeController.cs # src/Umbraco.Web/UmbracoApplicationBase.cs # src/Umbraco.Web/UmbracoInjectedModule.cs # src/Umbraco.Web/UmbracoModule.cs # src/Umbraco.Web/WebApi/AngularJsonMediaTypeFormatter.cs # src/Umbraco.Web/WebApi/Filters/FileUploadCleanupFilterAttribute.cs # src/Umbraco.Web/WebApi/UnhandledExceptionLogger.cs
250 lines
9.2 KiB
C#
250 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.IO;
|
|
using Umbraco.Cms.Core.Mapping;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
using Umbraco.Cms.Core.Services;
|
|
using Umbraco.Cms.Core.Strings;
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
|
{
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
|
[Authorize(Policy = AuthorizationPolicies.TreeAccessTemplates)]
|
|
[ParameterSwapControllerActionSelector(nameof(GetById), "id", typeof(int), typeof(Guid), typeof(Udi))]
|
|
public class TemplateController : BackOfficeNotificationsController
|
|
{
|
|
private readonly IFileService _fileService;
|
|
private readonly IUmbracoMapper _umbracoMapper;
|
|
private readonly IShortStringHelper _shortStringHelper;
|
|
|
|
public TemplateController(
|
|
IFileService fileService,
|
|
IUmbracoMapper umbracoMapper,
|
|
IShortStringHelper shortStringHelper)
|
|
{
|
|
_fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
|
|
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
|
_shortStringHelper = shortStringHelper ?? throw new ArgumentNullException(nameof(shortStringHelper));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets data type by alias
|
|
/// </summary>
|
|
/// <param name="alias"></param>
|
|
/// <returns></returns>
|
|
public TemplateDisplay GetByAlias(string alias)
|
|
{
|
|
var template = _fileService.GetTemplate(alias);
|
|
return template == null ? null : _umbracoMapper.Map<ITemplate, TemplateDisplay>(template);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all templates
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerable<EntityBasic> GetAll()
|
|
{
|
|
return _fileService.GetTemplates().Select(_umbracoMapper.Map<ITemplate, EntityBasic>);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the template json for the template id
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult<TemplateDisplay> GetById(int id)
|
|
{
|
|
var template = _fileService.GetTemplate(id);
|
|
if (template == null)
|
|
return NotFound();
|
|
|
|
return _umbracoMapper.Map<ITemplate, TemplateDisplay>(template);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the template json for the template guid
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult<TemplateDisplay> GetById(Guid id)
|
|
{
|
|
var template = _fileService.GetTemplate(id);
|
|
if (template == null)
|
|
return NotFound();
|
|
|
|
return _umbracoMapper.Map<ITemplate, TemplateDisplay>(template);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the template json for the template udi
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ActionResult<TemplateDisplay> GetById(Udi id)
|
|
{
|
|
var guidUdi = id as GuidUdi;
|
|
if (guidUdi == null)
|
|
return NotFound();
|
|
|
|
var template = _fileService.GetTemplate(guidUdi.Guid);
|
|
if (template == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return _umbracoMapper.Map<ITemplate, TemplateDisplay>(template);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a template with a given ID
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete]
|
|
[HttpPost]
|
|
public IActionResult DeleteById(int id)
|
|
{
|
|
var template = _fileService.GetTemplate(id);
|
|
if (template == null)
|
|
return NotFound();
|
|
|
|
_fileService.DeleteTemplate(template.Alias);
|
|
return Ok();
|
|
}
|
|
|
|
public TemplateDisplay GetScaffold(int id)
|
|
{
|
|
//empty default
|
|
var dt = new Template(_shortStringHelper, string.Empty, string.Empty);
|
|
dt.Path = "-1";
|
|
|
|
if (id > 0)
|
|
{
|
|
var master = _fileService.GetTemplate(id);
|
|
if(master != null)
|
|
{
|
|
dt.SetMasterTemplate(master);
|
|
}
|
|
}
|
|
|
|
var content = ViewHelper.GetDefaultFileContent( layoutPageAlias: dt.MasterTemplateAlias );
|
|
var scaffold = _umbracoMapper.Map<ITemplate, TemplateDisplay>(dt);
|
|
|
|
scaffold.Content = content + "\r\n\r\n@* the fun starts here *@\r\n\r\n";
|
|
return scaffold;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves the data type
|
|
/// </summary>
|
|
/// <param name="display"></param>
|
|
/// <returns></returns>
|
|
public ActionResult<TemplateDisplay> PostSave(TemplateDisplay display)
|
|
{
|
|
|
|
//Checking the submitted is valid with the Required attributes decorated on the ViewModel
|
|
if (ModelState.IsValid == false)
|
|
{
|
|
return ValidationProblem(ModelState);
|
|
}
|
|
|
|
if (display.Id > 0)
|
|
{
|
|
// update
|
|
var template = _fileService.GetTemplate(display.Id);
|
|
if (template == null)
|
|
return NotFound();
|
|
|
|
var changeMaster = template.MasterTemplateAlias != display.MasterTemplateAlias;
|
|
var changeAlias = template.Alias != display.Alias;
|
|
|
|
_umbracoMapper.Map(display, template);
|
|
|
|
if (changeMaster)
|
|
{
|
|
if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false)
|
|
{
|
|
|
|
var master = _fileService.GetTemplate(display.MasterTemplateAlias);
|
|
if(master == null || master.Id == display.Id)
|
|
{
|
|
template.SetMasterTemplate(null);
|
|
}else
|
|
{
|
|
template.SetMasterTemplate(master);
|
|
|
|
//After updating the master - ensure we update the path property if it has any children already assigned
|
|
var templateHasChildren = _fileService.GetTemplateDescendants(display.Id);
|
|
|
|
foreach (var childTemplate in templateHasChildren)
|
|
{
|
|
//template ID to find
|
|
var templateIdInPath = "," + display.Id + ",";
|
|
|
|
if (string.IsNullOrEmpty(childTemplate.Path))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//Find position in current comma separate string path (so we get the correct children path)
|
|
var positionInPath = childTemplate.Path.IndexOf(templateIdInPath) + templateIdInPath.Length;
|
|
|
|
//Get the substring of the child & any children (descendants it may have too)
|
|
var childTemplatePath = childTemplate.Path.Substring(positionInPath);
|
|
|
|
//As we are updating the template to be a child of a master
|
|
//Set the path to the master's path + its current template id + the current child path substring
|
|
childTemplate.Path = master.Path + "," + display.Id + "," + childTemplatePath;
|
|
|
|
//Save the children with the updated path
|
|
_fileService.SaveTemplate(childTemplate);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//remove the master
|
|
template.SetMasterTemplate(null);
|
|
}
|
|
}
|
|
|
|
_fileService.SaveTemplate(template);
|
|
|
|
if (changeAlias)
|
|
{
|
|
template = _fileService.GetTemplate(template.Id);
|
|
}
|
|
|
|
_umbracoMapper.Map(template, display);
|
|
}
|
|
else
|
|
{
|
|
//create
|
|
ITemplate master = null;
|
|
if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false)
|
|
{
|
|
master = _fileService.GetTemplate(display.MasterTemplateAlias);
|
|
if (master == null)
|
|
return NotFound();
|
|
}
|
|
|
|
// we need to pass the template name as alias to keep the template file casing consistent with templates created with content
|
|
// - see comment in FileService.CreateTemplateForContentType for additional details
|
|
var template = _fileService.CreateTemplateWithIdentity(display.Name, display.Name, display.Content, master);
|
|
_umbracoMapper.Map(template, display);
|
|
}
|
|
|
|
return display;
|
|
}
|
|
}
|
|
}
|