2020-11-20 15:32:36 +11:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-02-10 11:11:18 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2021-04-15 09:52:55 +02:00
|
|
|
using Umbraco.Cms.Core;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Editors;
|
|
|
|
|
using Umbraco.Cms.Core.Features;
|
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
2022-06-20 08:37:17 +02:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
2024-05-17 08:37:51 +02:00
|
|
|
using Umbraco.Cms.Core.Routing;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
|
|
|
|
using Umbraco.Cms.Core.WebAssets;
|
2021-02-15 13:07:12 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.WebAssets;
|
2021-02-10 11:11:18 +01:00
|
|
|
using Umbraco.Cms.Web.BackOffice.ActionResults;
|
|
|
|
|
using Umbraco.Cms.Web.BackOffice.Filters;
|
2021-02-10 11:42:04 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Filters;
|
2021-02-10 11:11:18 +01:00
|
|
|
using Umbraco.Extensions;
|
2018-02-27 15:47:47 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers;
|
|
|
|
|
|
|
|
|
|
[DisableBrowserCache]
|
|
|
|
|
[Area(Constants.Web.Mvc.BackOfficeArea)]
|
|
|
|
|
public class PreviewController : Controller
|
2018-02-27 15:47:47 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
|
|
|
|
|
private readonly ICookieManager _cookieManager;
|
|
|
|
|
private readonly UmbracoFeatures _features;
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
|
|
|
|
private readonly ILocalizationService _localizationService;
|
|
|
|
|
private readonly IPublishedSnapshotService _publishedSnapshotService;
|
|
|
|
|
private readonly IRuntimeMinifier _runtimeMinifier;
|
|
|
|
|
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
|
|
|
|
private readonly ICompositeViewEngine _viewEngines;
|
|
|
|
|
|
|
|
|
|
public PreviewController(
|
|
|
|
|
UmbracoFeatures features,
|
|
|
|
|
IOptionsSnapshot<GlobalSettings> globalSettings,
|
|
|
|
|
IPublishedSnapshotService publishedSnapshotService,
|
|
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor,
|
|
|
|
|
ILocalizationService localizationService,
|
|
|
|
|
IHostingEnvironment hostingEnvironment,
|
|
|
|
|
ICookieManager cookieManager,
|
|
|
|
|
IRuntimeMinifier runtimeMinifier,
|
|
|
|
|
ICompositeViewEngine viewEngines,
|
|
|
|
|
IUmbracoContextAccessor umbracoContextAccessor)
|
2018-02-27 15:47:47 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
_features = features;
|
|
|
|
|
_globalSettings = globalSettings.Value;
|
|
|
|
|
_publishedSnapshotService = publishedSnapshotService;
|
|
|
|
|
_backofficeSecurityAccessor = backofficeSecurityAccessor;
|
|
|
|
|
_localizationService = localizationService;
|
|
|
|
|
_hostingEnvironment = hostingEnvironment;
|
|
|
|
|
_cookieManager = cookieManager;
|
|
|
|
|
_runtimeMinifier = runtimeMinifier;
|
|
|
|
|
_viewEngines = viewEngines;
|
|
|
|
|
_umbracoContextAccessor = umbracoContextAccessor;
|
|
|
|
|
}
|
2018-04-05 12:50:00 +02:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccessWithoutApproval)]
|
|
|
|
|
[DisableBrowserCache]
|
|
|
|
|
public ActionResult Index(int? id = null)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<ILanguage> availableLanguages = _localizationService.GetAllLanguages();
|
|
|
|
|
if (id.HasValue)
|
2018-02-27 15:47:47 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
|
|
|
|
IPublishedContent? content = umbracoContext.Content?.GetById(true, id.Value);
|
|
|
|
|
if (content is null)
|
2021-01-18 15:40:22 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
return NotFound();
|
2021-01-18 15:40:22 +01:00
|
|
|
}
|
2018-03-14 13:56:49 +11:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
availableLanguages = availableLanguages.Where(language => content.Cultures.ContainsKey(language.IsoCode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var model = new BackOfficePreviewModel(_features, availableLanguages);
|
|
|
|
|
|
|
|
|
|
if (model.PreviewExtendedHeaderView.IsNullOrWhiteSpace() == false)
|
|
|
|
|
{
|
|
|
|
|
ViewEngineResult viewEngineResult =
|
|
|
|
|
_viewEngines.FindView(ControllerContext, model.PreviewExtendedHeaderView!, false);
|
|
|
|
|
if (viewEngineResult.View == null)
|
2018-03-14 13:56:49 +11:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
throw new InvalidOperationException("Could not find the view " + model.PreviewExtendedHeaderView +
|
|
|
|
|
", the following locations were searched: " + Environment.NewLine +
|
|
|
|
|
string.Join(Environment.NewLine,
|
|
|
|
|
viewEngineResult.SearchedLocations));
|
2018-02-28 13:41:49 +01:00
|
|
|
}
|
2022-06-20 08:37:17 +02:00
|
|
|
}
|
2018-03-14 13:56:49 +11:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
var viewPath = Path.Combine(
|
2020-09-01 18:10:12 +02:00
|
|
|
_globalSettings.UmbracoPath,
|
2020-08-07 00:48:32 +10:00
|
|
|
Constants.Web.Mvc.BackOfficeArea,
|
|
|
|
|
ControllerExtensions.GetControllerName<PreviewController>() + ".cshtml")
|
2022-06-20 08:37:17 +02:00
|
|
|
.Replace("\\", "/"); // convert to forward slashes since it's a virtual path
|
2018-02-27 15:47:47 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
return View(viewPath, model);
|
|
|
|
|
}
|
2021-01-18 15:40:22 +01:00
|
|
|
|
2018-09-14 00:14:03 +10:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the JavaScript file for preview
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[MinifyJavaScriptResult(Order = 0)]
|
|
|
|
|
// TODO: Replace this with response caching https://docs.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-3.1
|
|
|
|
|
//[OutputCache(Order = 1, VaryByParam = "none", Location = OutputCacheLocation.Server, Duration = 5000)]
|
|
|
|
|
public async Task<JavaScriptResult> Application()
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<string> files =
|
|
|
|
|
await _runtimeMinifier.GetJsAssetPathsAsync(BackOfficeWebAssets.UmbracoPreviewJsBundleName);
|
|
|
|
|
var result =
|
|
|
|
|
BackOfficeJavaScriptInitializer.GetJavascriptInitialization(files, "umbraco.preview", _globalSettings,
|
|
|
|
|
_hostingEnvironment);
|
2018-09-14 00:14:03 +10:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
return new JavaScriptResult(result);
|
|
|
|
|
}
|
2018-09-10 11:34:31 +10:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// The endpoint that is loaded within the preview iframe
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
|
|
|
|
|
public ActionResult Frame(int id, string culture)
|
|
|
|
|
{
|
|
|
|
|
EnterPreview(id);
|
2018-09-10 11:34:31 +10:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
// use a numeric URL because content may not be in cache and so .Url would fail
|
|
|
|
|
var query = culture.IsNullOrWhiteSpace() ? string.Empty : $"?culture={culture}";
|
2020-12-14 17:04:02 +11:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
return RedirectPermanent($"../../{id}{query}");
|
|
|
|
|
}
|
2020-10-08 16:36:25 +02:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
public ActionResult? EnterPreview(int id)
|
|
|
|
|
{
|
|
|
|
|
IUser? user = _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
|
|
|
|
|
_cookieManager.SetCookieValue(Constants.Web.PreviewCookieName, "preview");
|
2020-12-14 17:04:02 +11:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
return new EmptyResult();
|
|
|
|
|
}
|
2018-12-14 13:11:36 +00:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
public ActionResult End(string? redir = null)
|
|
|
|
|
{
|
|
|
|
|
_cookieManager.ExpireCookie(Constants.Web.PreviewCookieName);
|
2018-12-14 13:11:36 +00:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
// Expire Client-side cookie that determines whether the user has accepted to be in Preview Mode when visiting the website.
|
|
|
|
|
_cookieManager.ExpireCookie(Constants.Web.AcceptPreviewCookieName);
|
2018-12-14 13:11:36 +00:00
|
|
|
|
2024-05-17 08:37:51 +02:00
|
|
|
if (WebPath.IsWellFormedWebPath(redir, UriKind.Relative)
|
2022-06-20 08:37:17 +02:00
|
|
|
&& Uri.TryCreate(redir, UriKind.Relative, out Uri? url))
|
|
|
|
|
{
|
|
|
|
|
return Redirect(url.ToString());
|
2018-12-14 13:11:36 +00:00
|
|
|
}
|
2022-06-20 08:37:17 +02:00
|
|
|
|
|
|
|
|
return Redirect("/");
|
2018-02-27 15:47:47 +01:00
|
|
|
}
|
|
|
|
|
}
|